public void no_intersection_perpendicular()
        {
            var a = new Segment2(new Point2(0, 1), new Point2(1, 0));
            var b = new Segment2(new Point2(2, 2), new Point2(1, 1));

            var res = SegmentIntersectionOperation.IntersectionDetails(a, b);

            Assert.Null(res.Geometry);

            res = SegmentIntersectionOperation.IntersectionDetails(b, a);
            Assert.Null(res.Geometry);
        }
        public void intersection_overlap()
        {
            var a   = new Segment2(new Point2(0, 0), new Point2(1, 1));
            var b   = new Segment2(new Point2(.5, .5), new Point2(2, 2));
            var exp = new Segment2(new Point2(.5, .5), new Point2(1, 1));

            var res = SegmentIntersectionOperation.IntersectionDetails(a, b) as SegmentIntersectionOperation.SegmentResult;

            Assert.Equal(exp, res.S);
            Assert.Equal(new Vector2(.5, 0), res.A.Ratios);
            Assert.Equal(new Vector2(1, 1 / 3.0), res.B.Ratios);

            res = SegmentIntersectionOperation.IntersectionDetails(b, a) as SegmentIntersectionOperation.SegmentResult;
            Assert.Equal(exp, res.S);
            Assert.Equal(new Vector2(0, .5), res.A.Ratios);
            Assert.Equal(new Vector2(1 / 3.0, 1), res.B.Ratios);
        }
        public void intersection_cross_ends()
        {
            var a = new Segment2(new Point2(0, 0), new Point2(1, 1));
            var b = new Segment2(new Point2(0.5, 1.5), new Point2(1, 1));

            var res = (SegmentIntersectionOperation.PointResult)SegmentIntersectionOperation.IntersectionDetails(a, b);

            Assert.Equal(new Point2(1, 1), res.P);
            Assert.Equal(new Vector2(1, 1), res.Ratios);
            Assert.Equal(SegmentIntersectionOperation.SegmentIntersectionType.Head, res.TypeA);
            Assert.Equal(SegmentIntersectionOperation.SegmentIntersectionType.Head, res.TypeB);

            res = (SegmentIntersectionOperation.PointResult)SegmentIntersectionOperation.IntersectionDetails(b, a);
            Assert.Equal(new Point2(1, 1), res.P);
            Assert.Equal(new Vector2(1, 1), res.Ratios);
            Assert.Equal(SegmentIntersectionOperation.SegmentIntersectionType.Head, res.TypeA);
            Assert.Equal(SegmentIntersectionOperation.SegmentIntersectionType.Head, res.TypeB);
        }
        public void intersection_within()
        {
            var a = new Segment2(new Point2(0, 0), new Point2(1, 1));
            var b = new Segment2(new Point2(-.5, -.5), new Point2(2, 2));

            var res = SegmentIntersectionOperation.IntersectionDetails(a, b) as SegmentIntersectionOperation.SegmentResult;

            Assert.Equal(a, res.S);
            Assert.Equal(new Vector2(0, .2), res.A.Ratios);
            Assert.Equal(1, res.B.S);
            Assert.Equal(.6, res.B.T, 10);

            res = SegmentIntersectionOperation.IntersectionDetails(b, a) as SegmentIntersectionOperation.SegmentResult;
            Assert.Equal(a, res.S);
            Assert.Equal(new Vector2(.2, 0), res.A.Ratios);
            Assert.Equal(.6, res.B.S, 10);
            Assert.Equal(1, res.B.T);
        }
        public void intersection_same()
        {
            var a = new Segment2(new Point2(0, 0), new Point2(1, 1));
            var b = new Segment2(new Point2(1, 1), new Point2(0, 0));

            var res = SegmentIntersectionOperation.IntersectionDetails(a, a) as SegmentIntersectionOperation.SegmentResult;

            AreSpatiallyEqual(a, res.S);
            AreSpatiallyEqual(b, res.S);
            Assert.Equal(a.A, res.S.A);
            Assert.Equal(a.B, res.S.B);
            Assert.Equal(new Vector2(0, 0), res.A.Ratios);
            Assert.Equal(new Vector2(1, 1), res.B.Ratios);

            res = SegmentIntersectionOperation.IntersectionDetails(b, b) as SegmentIntersectionOperation.SegmentResult;
            AreSpatiallyEqual(a, res.S);
            AreSpatiallyEqual(b, res.S);
            Assert.Equal(b.A, res.S.A);
            Assert.Equal(b.B, res.S.B);
            Assert.Equal(new Vector2(0, 0), res.A.Ratios);
            Assert.Equal(new Vector2(1, 1), res.B.Ratios);

            res = SegmentIntersectionOperation.IntersectionDetails(a, b) as SegmentIntersectionOperation.SegmentResult;
            AreSpatiallyEqual(a, res.S);
            AreSpatiallyEqual(b, res.S);
            Assert.Equal(a.A, res.S.A);
            Assert.Equal(a.B, res.S.B);
            Assert.Equal(new Vector2(0, 1), res.A.Ratios);
            Assert.Equal(new Vector2(1, 0), res.B.Ratios);
            Assert.Equal(SegmentIntersectionOperation.SegmentIntersectionType.Butt, res.A.TypeA);
            Assert.Equal(SegmentIntersectionOperation.SegmentIntersectionType.Head, res.A.TypeB);
            Assert.Equal(SegmentIntersectionOperation.SegmentIntersectionType.Head, res.B.TypeA);
            Assert.Equal(SegmentIntersectionOperation.SegmentIntersectionType.Butt, res.B.TypeB);

            res = SegmentIntersectionOperation.IntersectionDetails(b, a) as SegmentIntersectionOperation.SegmentResult;
            AreSpatiallyEqual(a, res.S);
            AreSpatiallyEqual(b, res.S);
            Assert.Equal(b.A, res.S.A);
            Assert.Equal(b.B, res.S.B);
            Assert.Equal(new Vector2(0, 1), res.A.Ratios);
            Assert.Equal(new Vector2(1, 0), res.B.Ratios);
        }
        private static void AddPointCrossings(List <PolygonCrossing> results, Segment2Data segmentDataA, Segment2Data segmentDataB)
        {
            Contract.Requires(results != null);
            Contract.Ensures(results.Count >= Contract.OldValue(results).Count);

            Contract.Assume(null != segmentDataA.Segment);
            Contract.Assume(null != segmentDataB.Segment);

            var intersectionDetails = SegmentIntersectionOperation.IntersectionDetails(segmentDataA.Segment, segmentDataB.Segment);

            if (intersectionDetails is SegmentIntersectionOperation.PointResult)
            {
                var pointResult = (SegmentIntersectionOperation.PointResult)intersectionDetails;
                if (IsNotHead(pointResult))
                {
                    results.Add(CreatePolygonCrossing(pointResult, segmentDataA, segmentDataB));
                }
            }
            else if (intersectionDetails is SegmentIntersectionOperation.SegmentResult)
            {
                var segmentResult = intersectionDetails as SegmentIntersectionOperation.SegmentResult;
                var bIsNotHead    = IsNotHead(segmentResult.B);
                if (IsNotHead(segmentResult.A))
                {
                    var resultA = CreatePolygonCrossing(segmentResult.A, segmentDataA, segmentDataB);
                    if (bIsNotHead)
                    {
                        results.AddRange(new[] { resultA, CreatePolygonCrossing(segmentResult.B, segmentDataA, segmentDataB) });
                    }
                    else
                    {
                        results.Add(resultA);
                    }
                }
                else if (bIsNotHead)
                {
                    results.Add(CreatePolygonCrossing(segmentResult.B, segmentDataA, segmentDataB));
                }
            }
        }
Esempio n. 7
0
 /// <summary>
 /// Calculates the geometry resulting from the intersection of two segments.
 /// </summary>
 /// <param name="a">An end point on the first segment.</param>
 /// <param name="b">Another end point on the first segment.</param>
 /// <param name="c">An end point on the second segment.</param>
 /// <param name="d">Another end point on the second segment.</param>
 /// <returns>The resulting intersection geometry.</returns>
 public static IPlanarGeometry Intersection(Point2 a, Point2 b, Point2 c, Point2 d)
 {
     Point2.SegmentOrder(ref a, ref b, ref c, ref d);
     return(SegmentIntersectionOperation.Intersection(a, b, c, d));
 }