while technically not a ray (rays are just start and direction) it does double duty as both a line and a ray.
Пример #1
0
        public bool rayIntersects(ref Ray2D ray, out float distance)
        {
            distance = 0f;
            var maxValue = float.MaxValue;

            if (Math.Abs(ray.direction.X) < 1E-06f)
            {
                if ((ray.start.X < x) || (ray.start.X > x + width))
                {
                    return(false);
                }
            }
            else
            {
                var num11 = 1f / ray.direction.X;
                var num8  = (x - ray.start.X) * num11;
                var num7  = (x + width - ray.start.X) * num11;
                if (num8 > num7)
                {
                    var num14 = num8;
                    num8 = num7;
                    num7 = num14;
                }

                distance = MathHelper.Max(num8, distance);
                maxValue = MathHelper.Min(num7, maxValue);
                if (distance > maxValue)
                {
                    return(false);
                }
            }

            if (Math.Abs(ray.direction.Y) < 1E-06f)
            {
                if ((ray.start.Y < y) || (ray.start.Y > y + height))
                {
                    return(false);
                }
            }
            else
            {
                var num10 = 1f / ray.direction.Y;
                var num6  = (y - ray.start.Y) * num10;
                var num5  = (y + height - ray.start.Y) * num10;
                if (num6 > num5)
                {
                    var num13 = num6;
                    num6 = num5;
                    num5 = num13;
                }

                distance = MathHelper.Max(num6, distance);
                maxValue = MathHelper.Min(num5, maxValue);
                if (distance > maxValue)
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #2
0
        public static bool rayIntersects( ref Rectangle rect, ref Ray2D ray, out float distance )
        {
            distance = 0f;
            var maxValue = float.MaxValue;

            if( Math.Abs( ray.direction.X ) < 1E-06f )
            {
                if( ( ray.start.X < rect.X ) || ( ray.start.X > rect.X + rect.Width ) )
                    return false;
            }
            else
            {
                var num11 = 1f / ray.direction.X;
                var num8 = ( rect.X - ray.start.X ) * num11;
                var num7 = ( rect.X + rect.Width - ray.start.X ) * num11;
                if( num8 > num7 )
                {
                    var num14 = num8;
                    num8 = num7;
                    num7 = num14;
                }

                distance = MathHelper.Max( num8, distance );
                maxValue = MathHelper.Min( num7, maxValue );
                if( distance > maxValue )
                    return false;
            }

            if( Math.Abs( ray.direction.Y ) < 1E-06f )
            {
                if( ( ray.start.Y < rect.Y ) || ( ray.start.Y > rect.Y + rect.Height ) )
                {
                    return false;
                }
            }
            else
            {
                var num10 = 1f / ray.direction.Y;
                var num6 = ( rect.Y - ray.start.Y ) * num10;
                var num5 = ( rect.Y + rect.Height - ray.start.Y ) * num10;
                if( num6 > num5 )
                {
                    var num13 = num6;
                    num6 = num5;
                    num5 = num13;
                }

                distance = MathHelper.Max( num6, distance );
                maxValue = MathHelper.Min( num5, maxValue );
                if( distance > maxValue )
                    return false;
            }

            return true;
        }
Пример #3
0
        public static bool RayIntersects(ref Rectangle rect, ref Ray2D ray, out float distance)
        {
            distance = 0f;
            var maxValue = float.MaxValue;

            if (Math.Abs(ray.Direction.X) < 1E-06f)
            {
                if ((ray.Start.X < rect.X) || (ray.Start.X > rect.X + rect.Width))
                {
                    return(false);
                }
            }
            else
            {
                var num11 = 1f / ray.Direction.X;
                var num8  = (rect.X - ray.Start.X) * num11;
                var num7  = (rect.X + rect.Width - ray.Start.X) * num11;
                if (num8 > num7)
                {
                    var num14 = num8;
                    num8 = num7;
                    num7 = num14;
                }

                distance = MathHelper.Max(num8, distance);
                maxValue = MathHelper.Min(num7, maxValue);
                if (distance > maxValue)
                {
                    return(false);
                }
            }

            if (Math.Abs(ray.Direction.Y) < 1E-06f)
            {
                if ((ray.Start.Y < rect.Y) || (ray.Start.Y > rect.Y + rect.Height))
                {
                    return(false);
                }
            }
            else
            {
                var num10 = 1f / ray.Direction.Y;
                var num6  = (rect.Y - ray.Start.Y) * num10;
                var num5  = (rect.Y + rect.Height - ray.Start.Y) * num10;
                if (num6 > num5)
                {
                    var num13 = num6;
                    num6 = num5;
                    num5 = num13;
                }

                distance = MathHelper.Max(num6, distance);
                maxValue = MathHelper.Min(num5, maxValue);
                if (distance > maxValue)
                {
                    return(false);
                }
            }

            return(true);
        }