closestPointOnLine() private method

private closestPointOnLine ( Vector2 lineA, Vector2 lineB, Vector2 closestTo ) : Vector2
lineA Microsoft.Xna.Framework.Vector2
lineB Microsoft.Xna.Framework.Vector2
closestTo Microsoft.Xna.Framework.Vector2
return Microsoft.Xna.Framework.Vector2
Exemplo n.º 1
0
        /// <summary>
        /// iterates all the edges of the polygon and gets the closest point on any edge to point. Returns via out the squared distance
        /// to the closest point and the normal of the edge it is on.
        /// </summary>
        /// <returns>The closest point on polygon to point.</returns>
        /// <param name="point">Point.</param>
        /// <param name="distanceSquared">Distance squared.</param>
        /// <param name="edgeNormal">Edge normal.</param>
        public Vector2 getClosestPointOnPolygonToPoint(Vector2 point, out float distanceSquared, out Vector2 edgeNormal)
        {
            distanceSquared = float.MaxValue;
            edgeNormal      = Vector2.Zero;
            var   closestPoint = Vector2.Zero;
            float tempDistanceSquared;

            for (var i = 0; i < points.Length; i++)
            {
                var j = i + 1;
                if (j == points.Length)
                {
                    j = 0;
                }

                var closest = ShapeCollisions.closestPointOnLine(points[i], points[j], point);
                Vector2.DistanceSquared(ref point, ref closest, out tempDistanceSquared);

                if (tempDistanceSquared < distanceSquared)
                {
                    distanceSquared = tempDistanceSquared;
                    closestPoint    = closest;

                    // get the normal of the line
                    var line = points[j] - points[i];
                    edgeNormal.X = line.Y;
                    edgeNormal.Y = -line.X;
                }
            }

            return(closestPoint);
        }