Example #1
0
        /// <summary>
        /// Determines if this ray intersect another <paramref name="ray"/>.
        /// </summary>
        /// <param name="ray">A ray.</param>
        /// <returns><c>true</c> when another object intersects this object.</returns>
        public bool Intersects(Ray2 ray)
        {
            if (ray == null)
            {
                return(false);
            }
            if (ray == this || P.Equals(ray.P) && Direction.Equals(ray.Direction))
            {
                return(true); // NOTE: requires ray to be immutable
            }
            Point2  a, c;
            Vector2 d0, d1;
            // next order the rays
            var compareResult = P.CompareTo(ray.P);

            if (0 < ((compareResult == 0) ? Direction.CompareTo(ray.Direction) : compareResult))
            {
                a  = ray.P;
                c  = P;
                d0 = ray.Direction;
                d1 = Direction;
            }
            else
            {
                a  = P;
                c  = ray.P;
                d0 = Direction;
                d1 = ray.Direction;
            }

            var e          = c - a;
            var tNumerator = (e.X * d0.Y) - (e.Y * d0.X);
            var cross      = (d0.X * d1.Y) - (d1.X * d0.Y);

            if (cross == 0.0)
            {
                // parallel
                return(tNumerator == 0.0 && IntersectsRayParallel(d0, d1, e, a, c));
            }

            // not parallel

            var t = tNumerator / cross;

            if (t < 0.0)
            {
                return(false); // not intersecting on other ray
            }
            var s = ((e.X * d1.Y) - (e.Y * d1.X)) / cross;

            if (s < 0.0)
            {
                return(false); // not intersecting on this ray
            }
            return(true);
        }
Example #2
0
        public static int Compare(Point2 a, Point2 b, Point2 c, Point2 d)
        {
            Point2 aSmall;
            Point2 aLarge;
            Point2 bSmall;
            Point2 bLarge;

            if (a.CompareTo(b) <= 0)
            {
                aSmall = a;
                aLarge = b;
            }
            else
            {
                aSmall = b;
                aLarge = a;
            }
            if (c.CompareTo(d) <= 0)
            {
                bSmall = c;
                bLarge = d;
            }
            else
            {
                bSmall = d;
                bLarge = c;
            }
            var compareResult = aSmall.CompareTo(bSmall);

            return(0 != compareResult
                ? compareResult
                : aLarge.CompareTo(bLarge));
        }
Example #3
0
 internal static void Order(ref Point2 a, ref Point2 b)
 {
     if (a.CompareTo(b) > 0)
     {
         var t = a;
         a = b;
         b = t;
     }
 }
Example #4
0
        /// <summary>
        /// Determines if this line intersect another line.
        /// </summary>
        /// <param name="other">A line.</param>
        /// <returns><c>true</c> when another object intersects this object.</returns>
        public bool Intersects(Line2 other)
        {
            if (other == null)
            {
                return(false);
            }
            if (other == this || P.Equals(other.P) && Direction.Equals(other.Direction))
            {
                return(true);
            }

            Point2  a, c;
            Vector2 d0, d1;
            // order the lines
            var compareResult = P.CompareTo(other.P);

            if (0 < ((compareResult == 0) ? Direction.CompareTo(other.Direction) : compareResult))
            {
                a  = other.P;
                c  = P;
                d0 = other.Direction;
                d1 = Direction;
            }
            else
            {
                a  = P;
                c  = other.P;
                d0 = Direction;
                d1 = other.Direction;
            }

            var cross = (d0.X * d1.Y) - (d1.X * d0.Y);

            if (cross == 0.0)
            {
                // parallel
                var e = c - a;
                return((e.X * d0.Y) - (e.Y * d0.X) == 0.0);
            }

            return(true); // not parallel
        }
Example #5
0
        internal static void SegmentOrder(ref Point2 a, ref Point2 b, ref Point2 c, ref Point2 d)
        {
            // first order the points in the segments
            Order(ref a, ref b);
            Order(ref c, ref d);
            // next order the segments
            var compareResult = a.CompareTo(c);

            if (compareResult == 0)
            {
                compareResult = b.CompareTo(d);
            }
            if (0 < compareResult)
            {
                var t = a;
                a = c;
                c = t;
                t = b;
                b = d;
                d = t;
            }
        }
Example #6
0
 /// <inheritdoc/>
 public override int GetHashCode()
 {
     return((A.CompareTo(B) < 0 ? A.GetHashCode() : B.GetHashCode()) ^ -3939399);
 }