private static IPolygon BinaryOperation(IPolygon p, IPolygon q, ClipType operationType)
        {
            if (p.IsEmpty && q.IsEmpty)
            {
                return(Polygon.Empty);
            }

            var boundingBox = Box2.Hull(p.BoundingBox(), q.BoundingBox());

            var fixedP = ConvertToFixedPoint(p, boundingBox);
            var fixedQ = ConvertToFixedPoint(q, boundingBox);

            try
            {
                var clipper = new Clipper();
                clipper.AddPaths(fixedP, PolyType.ptSubject, closed: true);
                clipper.AddPaths(fixedQ, PolyType.ptClip, closed: true);

                var fixedAnswer = new ClipperPolygon();

                clipper.Execute(operationType, fixedAnswer);

                return(ConvertToFloatingPoint(fixedAnswer, boundingBox));
            }
            catch (Exception e)
            {
                Console.WriteLine("EXCEPTION: {0}", e);
                return(Polygon.Empty);
            }
        }
        public void MinkowskiDifference_of_two_boxes_amounts_to_subtracting_minima_and_maxima()
        {
            var box1 = new Box2(new Interval(0.123, 0.456), new Interval(2.718, 3.141));
            var box2 = new Box2(new Interval(0.0777, 0.0888), new Interval(-0.1, +0.1));

            var expectedAnswer = Box2.Hull(box1.MinCorner - box2.MaxCorner, box1.MaxCorner - box2.MinCorner);
            var actualAnswer   = PolygonUtils.MinkowskiDifference(box1, box2);

            ExpectPolygonsAreEqualWithinTolerance(actualAnswer, expectedAnswer, _tolerance);
        }
        private static IPolygon MultinaryOperation(IEnumerable <IPolygon> polygons, ClipType operationType)
        {
            var boundingBox = Box2.Hull(polygons.Select(p => p.BoundingBox()));

            if (boundingBox.IsEmpty)
            {
                return(Polygon.Empty);
            }

            var first = polygons.First();
            var rest  = polygons.Skip(1);

            if (!rest.Any())
            {
                return(first);
            }

            var fixedAnswer = ConvertToFixedPoint(first, boundingBox);

            foreach (var p in rest)
            {
                var fixedP = ConvertToFixedPoint(p, boundingBox);

                try
                {
                    var clipper = new Clipper();
                    clipper.AddPaths(fixedAnswer, PolyType.ptSubject, closed: true);
                    clipper.AddPaths(fixedP, PolyType.ptClip, closed: true);

                    var tempAnswer = new ClipperPolygon();

                    clipper.Execute(operationType, tempAnswer);

                    fixedAnswer = tempAnswer;
                }
                catch (Exception e)
                {
                    Console.WriteLine("EXCEPTION: {0}", e);
                }
            }

            return(ConvertToFloatingPoint(fixedAnswer, boundingBox));
        }
        public static IPolygon MinkowskiDifference(IPolygon p, IPolygon q)
        {
            if (p.IsEmpty || q.IsEmpty)
            {
                return(Polygon.Empty);
            }

            var boundingBox = Box2.Hull(p.BoundingBox(), q.BoundingBox());

            boundingBox = Box2.Hull(boundingBox, new Box2(-boundingBox.MaxCorner, boundingBox.Dimensions));

            var fixedAnswer = new ClipperPolygon();
            var fixedP      = ConvertToFixedPoint(p, boundingBox);

            foreach (var qContour in q.Contours)
            {
                var fixedQContour = MinusConvertToFixedPoint(qContour, boundingBox);

                try
                {
                    var clipper = new Clipper();
                    clipper.AddPaths(fixedAnswer, PolyType.ptSubject, closed: true);
                    clipper.AddPaths(Clipper.MinkowskiSum(fixedQContour, fixedP, pathIsClosed: true), PolyType.ptClip, closed: true);

                    var tempAnswer = new ClipperPolygon();

                    clipper.Execute(ClipType.ctUnion, tempAnswer);

                    fixedAnswer = tempAnswer;
                }
                catch (Exception e)
                {
                    Console.WriteLine("EXCEPTION: {0}", e);
                }
            }

            return(ConvertToFloatingPoint(fixedAnswer, boundingBox));
        }
Beispiel #5
0
 public Polygon(IEnumerable <IClosedPolyline> contours)
 {
     _contours    = contours.ToReadOnlyList();
     _boundingBox = Box2.Hull(_contours.SelectMany(c => c.Vertices));
 }
Beispiel #6
0
 public Polygon(IClosedPolyline contour)
 {
     _contours    = contour.AsSingleton();
     _boundingBox = Box2.Hull(contour.Vertices);
 }