static public ClipperPolygons ToClipperPolygons(this IPolygon polygon) { Clipper c = new Clipper(); ClipperPolygons polygons = new ClipperPolygons(), result = new ClipperPolygons(); for (int r = 0, r_to = polygon.RingCount; r < r_to; r++) { var ring = polygon[r]; ring.Close(); var ringPoints = new List <IntPoint>(); for (int p = 0, p_to = ring.PointCount; p < p_to; p++) { ringPoints.Add(ring[p].ToClipperPointer()); } polygons.Add(ringPoints); } c.AddPaths(polygons, PolyType.ptSubject, true); if (c.Execute(ClipType.ctUnion, result) == true) { return(result); } return(null); }
static public ClipperPolygons Buffer(this ClipperPolygons polygons, double distance) { ClipperPolygons result = new ClipperPolygons(); ClipperOffset co = new ClipperOffset(); co.AddPaths(polygons, JoinType.jtRound, EndType.etClosedPolygon); co.Execute(ref result, distance * Acc); return result; }
private ClipperPolygons CombineLineSegments(ClipperPolygons lines) { PolylineReduction reduction = new PolylineReduction(); foreach (var points in lines) { reduction.AddLine(points); } return(reduction.Reduce()); }
static public IPolygon Merge(this List<IPolygon> polygons) { Clipper c = new Clipper(); foreach (var polygon in polygons) { c.AddPaths(polygon.ToClipperPolygons(), PolyType.ptSubject, true); } ClipperPolygons result = new ClipperPolygons(); if (c.Execute(ClipType.ctUnion, result, PolyFillType.pftPositive) == true) { return result.ToPolygon(); } return null; }
static public Polygon ToPolygon(this ClipperPolygons clipperPolygons) { Polygon result = new Polygon(); foreach (var clipperRing in clipperPolygons) { var ring = new Ring(); foreach (var clipperPoint in clipperRing) { ring.AddPoint(clipperPoint.ToPoint()); } result.AddRing(ring); } return result; }
static public IPolygon Clip(this IPolygon clippee, Envelope clipper) { var clippeePolygons = clippee.ToClipperPolygons(); var clipperPolygons = clipper.ToPolygon().ToClipperPolygons(); var c = new Clipper(); c.AddPaths(clippeePolygons, PolyType.ptSubject, true); c.AddPaths(clipperPolygons, PolyType.ptClip, true); ClipperPolygons result = new ClipperPolygons(); if (c.Execute(ClipType.ctIntersection, result) == true) { return result.ToPolygon(); } return clippee; }
private void AddPolygonCollider2DElements(ClipperPolygons polygons, List<XElement> xmlList) { if (polygons.Count == 0) return; // Add just one polygon collider that has all paths in it. List<XElement> pathElements = new List<XElement>(); foreach (var path in polygons) { string data = String.Join(" ", path.Select(pt => String.Format("{0},{1}", pt.X * Program.Scale, pt.Y * Program.Scale))); XElement pathElement = new XElement("Path", data); pathElements.Add(pathElement); } XElement polyColliderElement = new XElement("PolygonCollider2D", pathElements); xmlList.Add(polyColliderElement); }
private void AddEdgeCollider2DElements(ClipperPolygons lines, List<XElement> xmlList) { if (lines.Count == 0) return; // Add one edge collider for every polyline // Clipper does not combine line segments for us var combined = CombineLineSegments(lines); foreach (var points in combined) { string data = String.Join(" ", points.Select(pt => String.Format("{0},{1}", pt.X * Program.Scale, pt.Y * Program.Scale))); XElement edgeCollider = new XElement("EdgeCollider2D", new XElement("Points", data)); xmlList.Add(edgeCollider); } }
private void AddEdgeCollider2DElements(ClipperPolygons lines, List <XElement> xmlList) { if (lines.Count == 0) { return; } // Add one edge collider for every polyline // Clipper does not combine line segments for us var combined = CombineLineSegments(lines); foreach (var points in combined) { string data = String.Join(" ", points.Select(pt => String.Format("{0},{1}", pt.X * Program.Scale, pt.Y * Program.Scale))); XElement edgeCollider = new XElement("EdgeCollider2D", new XElement("Points", data)); xmlList.Add(edgeCollider); } }
private void AddPolygonCollider2DElements(ClipperPolygons polygons, List <XElement> xmlList) { if (polygons.Count == 0) { return; } // Add just one polygon collider that has all paths in it. List <XElement> pathElements = new List <XElement>(); foreach (var path in polygons) { string data = String.Join(" ", path.Select(pt => String.Format("{0},{1}", pt.X * Program.Scale, pt.Y * Program.Scale))); XElement pathElement = new XElement("Path", data); pathElements.Add(pathElement); } XElement polyColliderElement = new XElement("PolygonCollider2D", pathElements); xmlList.Add(polyColliderElement); }
public static List <List <Point> > DecomposeNonSimplePolygon(List <Point> points) { List <List <Point> > result = new List <List <Point> >(); ClipperPolygon polygon = new ClipperPolygon(); foreach (Point point in points) { IntPoint intPoint = new IntPoint(point.X, point.Y); polygon.Add(intPoint); } ClipperPolygons polygons = Clipper.SimplifyPolygon(polygon, PolyFillType.pftEvenOdd); foreach (List <IntPoint> item in polygons) { List <Point> pointList = new List <Point>(); foreach (IntPoint subitem in item) { pointList.Add(new Point((int)subitem.X, (int)subitem.Y)); } result.Add(pointList); } return(result); }
private ClipperPolygons CombineLineSegments(ClipperPolygons lines) { PolylineReduction reduction = new PolylineReduction(); foreach (var points in lines) { reduction.AddLine(points); } return reduction.Reduce(); }