Exemplo n.º 1
0
 /// <summary>
 /// Calculates the intersection geometry between this segment and a ray.
 /// </summary>
 /// <param name="ray">The ray to find the intersection with.</param>
 /// <returns>The intersection geometry or <c>null</c> for no intersection.</returns>
 public IPlanarGeometry Intersection(Ray2 ray)
 {
     if (ray == null)
     {
         return(null);
     }
     return(ray.Intersection(this));
 }
Exemplo n.º 2
0
 /// <summary>
 /// Determines if this ray is geometrically equal to another.
 /// </summary>
 /// <param name="other">Another ray</param>
 /// <returns><c>true</c> if the rays are spatially equal.</returns>
 public bool SpatiallyEqual(Ray2 other)
 {
     return(!ReferenceEquals(null, other) &&
            P.Equals(other.P) &&
            (
                Direction.Equals(other.Direction) ||
                Direction.GetNormalized().Equals(other.Direction.GetNormalized())
            ));
 }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Constructs a ray identical to the given <paramref name="ray"/>.
 /// </summary>
 /// <param name="ray">A ray.</param>
 public Ray2(Ray2 ray)
 {
     if (null == ray)
     {
         throw new ArgumentNullException("ray");
     }
     Contract.EndContractBlock();
     P         = ray.P;
     Direction = ray.Direction;
 }
Exemplo n.º 5
0
        /// <inheritdoc/>
        public IPlanarGeometry Intersection(Ray2 other)
        {
            if (ReferenceEquals(null, other) || Count == 0)
            {
                return(null);
            }
            var intersectedPoints = new MultiPoint2(this.Where(other.Intersects));

            return(FixToProperPlanerGeometryResult(intersectedPoints));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Calculates the intersection geometry between this ray and another.
        /// </summary>
        /// <param name="ray">The ray to find the intersection with.</param>
        /// <returns>The intersection geometry or <c>null</c> for no intersection.</returns>
        public IPlanarGeometry Intersection(Ray2 ray)
        {
            if (ray == null)
            {
                return(null);
            }
            if (ray == this || P.Equals(ray.P) && Direction.Equals(ray.Direction))
            {
                return(ray); // NOTE: requires ray to be immutable
            }
            Point2  a, c;
            Vector2 d0, d1;
            // next order the segments
            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
                    ? IntersectionRayParallel(d0, d1, e, a, c)
                    : null); // no intersection
            }

            // not parallel

            var t = tNumerator / cross;

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

            if (s < 0.0)
            {
                return(null); // not intersecting on this ray
            }
            if (s == 0.0)
            {
                return(a);
            }
            if (t == 0.0)
            {
                return(c);
            }
            return(a + d0.GetScaled(s)); // it must intersect at a point, so find where
        }
Exemplo n.º 7
0
 /// <summary>
 /// Indicates whether the current object is equal to another object of the same type.
 /// </summary>
 /// <returns>
 /// <c>true</c> if the current object is equal to the <paramref name="other"/> parameter; otherwise, <c>false</c>.
 /// </returns>
 /// <param name="other">An object to compare with this object.</param>
 public bool Equals(Ray2 other)
 {
     return(!ReferenceEquals(null, other) &&
            P.Equals(other.P) &&
            Direction.Equals(other.Direction));
 }
Exemplo n.º 8
0
 /// <summary>
 /// Determines if this segment 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)
 {
     return(ray != null && ray.Intersects(this));
 }
Exemplo n.º 9
0
 /// <inheritdoc/>
 public bool Intersects(Ray2 other)
 {
     return(!ReferenceEquals(null, other) &&
            Count > 0 &&
            this.Any(other.Intersects));
 }