public bool IntersectsLine2D(Point2D start, Point2D end)
        {
            // compute the euclidean distance between actor and aim
            var distanceActorAim = DistanceFunctions.GetEuclidDistance2D(start, end);

            // compute the direction vector D from Actor to aimvector
            var dx = (start.X - end.X) / distanceActorAim;
            var dy = (start.Y - end.Y) / distanceActorAim;

            // Now the line equation is x = dx*t + aimX, y = dy*t + aimY with 0 <= t <= 1.
            // compute the value t of the closest point to the circle center C(sphereCenterX, sphereCenterY)
            var t = dx * (CenterPoint.X - end.X) + dy * (CenterPoint.Y - end.Y);

            // This is the projection of C on the line from actor to aim.
            // compute the coordinates of the point E on line and closest to C
            var ex = t * dx + end.X;
            var ey = t * dy + end.Y;

            // compute the euclidean distance from E to C
            var distanceEC = DistanceFunctions.GetEuclidDistance2D(new Point2D((float)ex, (float)ey), new Point2D(CenterPoint.X, CenterPoint.Y));

            // test if the line intersects the circle
            if (distanceEC < Radius)
            {
                return(true);
            }
            else if (distanceEC == Radius) // line is tangent to circle
            {
                return(true);
            }
            else // line doesnt touch circle
            {
                return(false);
            }
        }
        public bool Intersects(Circle2D c)
        {
            if (DistanceFunctions.GetEuclidDistance2D(CenterPoint, c.CenterPoint) <= c.Radius + Radius)
            {
                return(true);
            }

            return(false);
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public Point2D?Rect2DIntersectsLine(Point2D start, Point2D end)
        {
            var r = this;
            // Build all lines of the Rectangle and test a line-line collision
            var los = new Line2D(start, end); //TODO: Ugly code
            var l1  = new Line2D(new Point2D(r.X, r.Y), new Point2D(r.X + r.Width, r.Y));
            var l2  = new Line2D(new Point2D((r.X + r.Width), r.Y), new Point2D(r.X + r.Width, r.Y + r.Height));
            var l3  = new Line2D(new Point2D((r.X + r.Width), (r.Y + r.Height)), new Point2D(r.X, r.Y + r.Height));
            var l4  = new Line2D(new Point2D(r.X, r.Y + r.Height), new Point2D(r.X, r.Y));

            var            i1 = (Point2D)l1.IntersectWith(los);
            var            i2 = (Point2D)l2.IntersectWith(los);
            var            i3 = (Point2D)l3.IntersectWith(los);
            var            i4 = (Point2D)l4.IntersectWith(los);
            List <Point2D> ps = new List <Point2D>();

            if (i1 != null)
            {
                ps.Add(i1);
            }
            if (i2 != null)
            {
                ps.Add(i2);
            }
            if (i3 != null)
            {
                ps.Add(i3);
            }
            if (i4 != null)
            {
                ps.Add(i4);
            }
            if (ps.Count == 0)
            {
                return(null);
            }
            if (ps.Count > 2)
            {
                throw new Exception("Too many collisions. Collisionpoints: " + String.Join(", ", ps));
            }
            return(ps.OrderBy(point => DistanceFunctions.GetEuclidDistance2D(start, point)).First()); // Return point with shortest distance to start
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sphereCenterX">Center of circle in X</param>
        /// <param name="sphereCenterY">Center of circle in Y</param>
        /// <param name="sphereRadius">Radius of the circle</param>
        /// <param name="actorpos">Position of a player</param>
        /// <param name="actorYaw">Sight direction of this player</param>
        /// <returns></returns>
        public bool IntersectsVector2D(Point2D actorpos, float actorYaw)
        {
            // Yaw has to be negated (csgo -> normal)
            var yawRad = Angle.FromDegrees(-actorYaw).Radians;
            var aimX   = (float)(actorpos.X + Math.Cos(yawRad)); // Aim vector from Yaw
            var aimY   = (float)(actorpos.Y + Math.Sin(yawRad));

            // compute the euclidean distance between actor and aim
            var distanceActorAim = DistanceFunctions.GetEuclidDistance2D(actorpos, new Point2D(aimX, aimY));

            // compute the direction vector D from Actor to aimvector
            var dx = (actorpos.X - aimX) / distanceActorAim;
            var dy = (actorpos.Y - aimY) / distanceActorAim;

            // Now the line equation is x = dx*t + aimX, y = dy*t + aimY with 0 <= t <= 1.
            // compute the value t of the closest point to the circle center C(sphereCenterX, sphereCenterY)
            var t = dx * (CenterPoint.X - aimX) + dy * (CenterPoint.Y - aimY);

            // This is the projection of C on the line from actor to aim.
            // compute the coordinates of the point E on line and closest to C
            var ex = t * dx + aimX;
            var ey = t * dy + aimY;

            // compute the euclidean distance from E to C
            var distanceEC = DistanceFunctions.GetEuclidDistance2D(new Point2D((float)ex, (float)ey), new Point2D(CenterPoint.X, CenterPoint.Y));

            // test if the line intersects the circle
            if (distanceEC < Radius)
            {
                return(true);
            }
            else if (distanceEC == Radius) // line is tangent to circle
            {
                return(true);
            }
            else // line doesnt touch circle
            {
                return(false);
            }
        }
 public double GetDistance(Point2DDataPoint t)
 {
     return(DistanceFunctions.GetEuclidDistance2D(t.clusterPoint, clusterPoint));
 }