public void Intersects(ref BoundingCircle circle, out bool result)
        {
            float distSq;

            Vectors2.DistanceSq(ref Center, ref circle.Center, out distSq);
            result = distSq <= (Radius * Radius + circle.Radius * circle.Radius);
        }
        /// <summary>Returns current  <see cref="Vector2" /> with absolute values.</summary>
        /// <param name="current">The current  <see cref="Vector2" />.</param>
        /// <returns>Current  <see cref="Vector2" /> with absolute values.</returns>
        public static Vector2 Abs(this Vector2 current)
        {
            Vector2 result;

            Vectors2.Abs(ref current, out result);
            return(result);
        }
        public static void GetInertia(Vector2[] vertices, out float result)
        {
            Contract.Requires(vertices != null);
            Contract.Requires(vertices.Length > 0);

            if (vertices.Length == 1)
            {
                result = 0;
                return;
            }

            float   denom = 0;
            float   numer = 0;
            var     v1    = vertices[vertices.Length - 1];
            Vector2 v2;

            for (var index = 0; index < vertices.Length; index++, v1 = v2)
            {
                v2 = vertices[index];
                float a, b, c, d;
                Vector2.Dot(ref v2, ref v2, out a);
                Vector2.Dot(ref v2, ref v1, out b);
                Vector2.Dot(ref v1, ref v1, out c);
                Vectors2.ZCross(ref v1, ref v2, out d);
                d      = Math.Abs(d);
                numer += d;
                denom += (a + b + c) * d;
            }
            result = denom / (numer * 6);
        }
        /// <summary>
        ///     Determines the current angle in radians of the current <see cref="Vector2" /> and returns it.
        /// </summary>
        /// <param name="current">The current <see cref="Vector2" />.</param>
        /// <returns>The angle in radians of the current <see cref="Vector2" />.</returns>
        public static float ToAngle(this Vector2 current)
        {
            float result;

            Vectors2.Angle(ref current, out result);
            return(result);
        }
Beispiel #5
0
        public static void GetDistanceSq(ref Vector2 vertex1, ref Vector2 vertex2, ref Vector2 point,
                                         out float result)
        {
            float   edgeLength;
            Vector2 edge, local;

            Vector2.Subtract(ref point, ref vertex2, out local);
            Vector2.Subtract(ref vertex1, ref vertex2, out edge);
            Vectors2.Normalize(ref edge, out edgeLength, out edge);

            var nProj = local.Y * edge.X - local.X * edge.Y;
            var tProj = local.X * edge.X + local.Y * edge.Y;

            if (tProj < 0)
            {
                result = tProj * tProj + nProj * nProj;
            }
            else if (tProj > edgeLength)
            {
                tProj -= edgeLength;
                result = tProj * tProj + nProj * nProj;
            }
            else
            {
                result = nProj * nProj;
            }
        }
        public bool Equals(BoundingPolygon other)
        {
            if (ReferenceEquals(null, other))
            {
                return(false);
            }
            if (ReferenceEquals(this, other))
            {
                return(true);
            }
            if (Vertices == null || other.Vertices == null || Vertices.Length != other.Vertices.Length)
            {
                return(false);
            }

            for (var index = 0; index < Vertices.Length; index++)
            {
                var vertex1 = Vertices[index];
                var vertex2 = other.Vertices[index];
                if (!Vectors2.Equals(ref vertex1, ref vertex2))
                {
                    return(false);
                }
            }

            return(true);
        }
        public void Intersects(ref BoundingRectangle rect, out bool result)
        {
            // Find the closest point to the circle within the rectangle
            Vector2 closest;

            Vector2.Clamp(ref Center, ref rect.Min, ref rect.Max, out closest);
            // Calculate the distance between the circle's center and this closest point
            float distanceSquared;

            Vectors2.DistanceSq(ref Center, ref closest, out distanceSquared);
            // If the distance is less than the circle's radius, an intersection occurs
            result = (distanceSquared < Radius * Radius);
        }
        /// <summary>
        ///     Calculates the area of a polygon.
        /// </summary>
        /// <param name="vertices">The vertices of the polygon.</param>
        /// <param name="result">the area.</param>
        public static void GetArea(Vector2[] vertices, out float result)
        {
            Contract.Requires(vertices != null);
            Contract.Requires(vertices.Length > 2);

            float   area = 0;
            var     v1   = vertices[vertices.Length - 1];
            Vector2 v2;

            for (var index = 0; index < vertices.Length; ++index, v1 = v2)
            {
                v2 = vertices[index];
                float temp;
                Vectors2.ZCross(ref v1, ref v2, out temp);
                area += temp;
            }
            result = Math.Abs(area * .5f);
        }
        public static void FromVectors(Vector2[] vertices, out BoundingCircle result)
        {
            Contract.Requires(vertices != null);
            Contract.Requires(vertices.Length > 2);

            BoundingPolygon.GetCentroid(vertices, out result.Center);
            result.Radius = -1;
            for (var index = 0; index < vertices.Length; ++index)
            {
                float distSq;
                Vectors2.DistanceSq(ref result.Center, ref vertices[index], out distSq);
                if (result.Radius == -1 || (distSq < result.Radius))
                {
                    result.Radius = distSq;
                }
            }

            result.Radius = (float)Math.Sqrt(result.Radius);
        }
Beispiel #10
0
        public static bool Collide(BoundingCircle circle1, BoundingCircle circle2, out Vector2 contactPoint,
                                   out float distance, out Vector2 normal)
        {
            Vector2.Subtract(ref circle2.Center, ref circle1.Center, out normal);
            Vectors2.Normalize(ref normal, out distance, out normal);
            distance = distance - (circle1.Radius + circle2.Radius);
            bool collision = (distance <= 0);

            if (collision)
            {
                contactPoint.X = circle2.Center.X - normal.X * circle2.Radius;
                contactPoint.Y = circle2.Center.Y - normal.Y * circle2.Radius;
            }
            else
            {
                contactPoint = Vector2.Zero;
            }

            return(collision);
        }
Beispiel #11
0
        public static void Intersects(ref Vector2 vertex1, ref Vector2 vertex2, ref Ray2D ray, out float result,
                                      float tolerance = 0.0001f)
        {
            Vector2 tangent, normal;
            float   edgeMagnitude;

            Vector2.Subtract(ref vertex1, ref vertex2, out tangent);
            Vectors2.Normalize(ref tangent, out edgeMagnitude, out tangent);
            Vectors2.RightPerp(ref tangent, out normal);

            float dir;

            Vector2.Dot(ref normal, ref ray.Direction, out dir);
            if (Math.Abs(dir) >= tolerance)
            {
                Vector2 originDiff;
                Vector2.Subtract(ref ray.Origin, ref vertex2, out originDiff);
                float actualDistance;
                Vector2.Dot(ref normal, ref originDiff, out actualDistance);
                var distanceFromOrigin = -(actualDistance / dir);
                if (distanceFromOrigin >= 0)
                {
                    Vector2 intersectPos;
                    Vector2.Multiply(ref ray.Direction, distanceFromOrigin, out intersectPos);
                    Vector2.Add(ref intersectPos, ref originDiff, out intersectPos);

                    float distanceFromSecond;
                    Vector2.Dot(ref intersectPos, ref tangent, out distanceFromSecond);

                    if (distanceFromSecond >= 0 && distanceFromSecond <= edgeMagnitude)     // Hit!
                    {
                        result = distanceFromOrigin;
                        return;
                    }
                }
            }
            // No hit.
            result = -1;
        }
        /// <summary> Calculates the centroid of a polygon.</summary>
        /// <param name="vertices">The vertices of the polygon.</param>
        /// <param name="centroid">The centroid of a polygon.</param>
        /// <remarks>This is also known as center of gravity/mass.</remarks>
        public static void GetCentroid(Vector2[] vertices, out Vector2 centroid)
        {
            Contract.Requires(vertices != null);
            Contract.Requires(vertices.Length > 2);

            centroid = Vector2.Zero;
            float   temp;
            float   area = 0;
            var     v1   = vertices[vertices.Length - 1];
            Vector2 v2;

            for (var index = 0; index < vertices.Length; ++index, v1 = v2)
            {
                v2 = vertices[index];
                Vectors2.ZCross(ref v1, ref v2, out temp);
                area       += temp;
                centroid.X += ((v1.X + v2.X) * temp);
                centroid.Y += ((v1.Y + v2.Y) * temp);
            }

            temp        = 1 / (Math.Abs(area) * 3);
            centroid.X *= temp;
            centroid.Y *= temp;
        }
 public static bool Equals(ref BoundingRectangle rect1, ref BoundingRectangle rect2)
 {
     return(Vectors2.Equals(ref rect1.Min, ref rect2.Min) && Vectors2.Equals(ref rect1.Max, ref rect2.Max));
 }
 /// <summary>Clamps the specified point to be contained within the box.</summary>
 /// <param name="target">The target point.</param>
 /// <param name="clamped">The clamped point.</param>
 public void Clamp(ref Vector2 target, out Vector2 clamped)
 {
     Vectors2.Clamp(ref target, ref Min, ref Max, out clamped);
 }
Beispiel #15
0
 public static bool Equals(ref LineSegment line1, ref LineSegment line2)
 {
     return(Vectors2.Equals(ref line1.Start, ref line2.Start) &&
            Vectors2.Equals(ref line1.End, ref line2.End));
 }
Beispiel #16
0
 public static bool Equals(ref BoundingCircle circle1, ref BoundingCircle circle2)
 {
     return(Vectors2.Equals(ref circle1.Center, ref circle2.Center) &&
            circle1.Radius.NearlyEquals(circle2.Radius));
 }
Beispiel #17
0
 public static bool Equals(ref Line line1, ref Line line2)
 {
     return(Vectors2.Equals(ref line1.Normal, ref line2.Normal) && line1.D.NearlyEquals(line2.D));
 }
Beispiel #18
0
 public static bool Equals(ref Ray2D ray1, ref Ray2D ray2)
 {
     return(Vectors2.Equals(ref ray1.Origin, ref ray2.Origin) &&
            Vectors2.Equals(ref ray1.Direction, ref ray2.Direction));
 }