Ejemplo n.º 1
0
        //**********************************************************
        //** methods:
        //**********************************************************

        public Vector2f?Intersects(FloatLine line)
        {
            var x1 = line.P1.X;
            var y1 = line.P1.Y;
            var x2 = line.P2.X;
            var y2 = line.P2.Y;

            var x3 = P1.X;
            var y3 = P1.Y;
            var x4 = P2.X;
            var y4 = P2.Y;

            var den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);

            if (den == 0)
            {
                return(null);
            }

            var t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / den;
            var u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / den;

            if (t > 0 && t < 1 && u > 0)
            {
                return(new Vector2f(
                           x1 + (t * (x2 - x1)),
                           y1 + (t * (y2 - y1))
                           ));
            }

            return(null);
        }
Ejemplo n.º 2
0
        private Vector2f[] IntersectionPoints(FloatLine ray, RectangleShape shape)
        {
            var l1 = new FloatLine(shape.Position, shape.Position + new Vector2f(shape.Size.X, 0));
            var l2 = new FloatLine(shape.Position, shape.Position + new Vector2f(0, shape.Size.Y));
            var l3 = new FloatLine(
                shape.Position.X,
                shape.Position.Y + shape.Size.Y,
                shape.Position.X + shape.Size.X,
                shape.Position.Y + shape.Size.Y
                );
            var l4 = new FloatLine(
                shape.Position.X + shape.Size.X,
                shape.Position.Y,
                shape.Position.X + shape.Size.X,
                shape.Position.Y + shape.Size.Y
                );

            var intersections = new List <Vector2f?>
            {
                ray.Intersects(l1),
                ray.Intersects(l2),
                ray.Intersects(l3),
                ray.Intersects(l4),
            };

            return(intersections
                   .Where(x => x != null)
                   .Cast <Vector2f>()
                   .ToArray());
        }
Ejemplo n.º 3
0
        private Vector2f[] IntersectionPoints(FloatLine ray, LineShape shape)
        {
            var line = new FloatLine(shape.P1, shape.P2);
            var intersectionPoint = ray.Intersects(line);

            return(intersectionPoint.HasValue ? new[] { intersectionPoint.Value } : new Vector2f[0]);
        }
Ejemplo n.º 4
0
        public static TwoPointForm GetTwoPointForm(FloatLine line)
        {
            var x1 = line.P1.X;
            var y1 = line.P1.Y;
            var x2 = line.P2.X;
            var y2 = line.P2.Y;
            var m  = Slope(x1, y1, x2, y2);

            return(new TwoPointForm {
                M = m, X1 = x1, Y1 = y1
            });
        }
Ejemplo n.º 5
0
        public static SlopeInterceptForm GetSlopeInterceptForm(FloatLine line)
        {
            var x1 = line.P1.X;
            var y1 = line.P1.Y;
            var x2 = line.P2.X;
            var y2 = line.P2.Y;
            var m  = Slope(x1, y1, x2, y2);
            var b  = -(m * x1) + y1;

            return(new SlopeInterceptForm {
                M = m, B = b
            });
        }
Ejemplo n.º 6
0
        private Vector2f[] IntersectionPoints(FloatLine ray, CircleShape shape)
        {
            var circle = new FloatCircle(shape.Position, shape.Radius);

            var circleDelta = (shape.Position - Position);
            var product     = Direction.Dot(circleDelta);

            if (product <= 0) // if ray is facing in the opposite direction of the circle
            {
                return(Array.Empty <Vector2f>());
            }

            return(ray.Intersects(circle));
        }
Ejemplo n.º 7
0
        private Vector2f[] IntersectionPoints(FloatLine ray, Shape shape)
        {
            switch (shape)
            {
            case LineShape line:
                return(IntersectionPoints(ray, line));

            case CircleShape circle:
                return(IntersectionPoints(ray, circle));

            case RectangleShape rect:
                return(IntersectionPoints(ray, rect));

            default:
                throw new NotImplementedException("Shape intersection not implemented");
            }
        }
Ejemplo n.º 8
0
        public Vector2f?Cast(Shape[] shapes)
        {
            var p1  = Position;
            var p2  = Position + (Direction * 100_000_000f);
            var ray = new FloatLine(p1, p2);

            var intersectionPoints
                = shapes.SelectMany(shape => IntersectionPoints(ray, shape));

            if (!intersectionPoints.Any())
            {
                return(null);
            }

            return(intersectionPoints
                   .Select(point => (point, (point - Position).SqrLength()))
                   .Aggregate((shortest, x) => x.Item2 < shortest.Item2 ? x : shortest).point);
        }
Ejemplo n.º 9
0
        public Vector2f Reflect(FloatLine mirror)
        {
            var n = mirror.NormalDirection();

            return(Direction().Reflect(n));
        }
Ejemplo n.º 10
0
 public static Vector2f Reflect(this Vector2f vec, FloatLine mirror)
 {
     return(vec.Reflect(mirror.NormalDirection()));
 }
Ejemplo n.º 11
0
 public static Vector2f NormalDirection(this FloatLine line)
 {
     return(NormalDirection(line.P1, line.P2));
 }
Ejemplo n.º 12
0
        public static FloatLine Normal(this FloatLine line)
        {
            var normal = Normal(line.P1, line.P2);

            return(new FloatLine(normal.Item1, normal.Item2));
        }