#pragma warning disable 0436 using UnityEngine; using System.Collections; using System.Collections.Generic; using ClipperLib; using Polygon = System.Collections.Generic.List; using Polygons = System.Collections.Generic.List>; using Delaunay; public static class ClipperHelper { private static float multiplier = 1000; public static List> clip(List boundary, Triangle piece) { //create Boundary Polygon Polygons boundaryPoly = createPolygons(boundary); //create Polygon from the triangular piece Polygons subjPoly = createPolygons(piece); //clip triangular polygon against the boundary polygon Polygons result = new Polygons(); //忽略warning #pragma warning disable CS0436 // The variable 'result' is assigned but its value is never used Clipper c = new Clipper(); c.AddPaths(subjPoly, PolyType.ptClip, true); c.AddPaths(boundaryPoly, PolyType.ptSubject, true); c.Execute(ClipType.ctIntersection, result, PolyFillType.pftEvenOdd, PolyFillType.pftEvenOdd); List> clippedPolygons = new List>(); foreach (Polygon poly in result) { List clippedPoly = new List(); foreach (IntPoint p in poly) { clippedPoly.Add(new Vector2(p.X, p.Y) / multiplier); } clippedPolygons.Add(clippedPoly); } return clippedPolygons; } public static List> clip(List boundary, List region) { Polygons boundaryPoly = createPolygons(boundary); Polygons regionPoly = createPolygons(region); //clip triangular polygon against the boundary polygon Polygons result = new Polygons(); Clipper c = new Clipper(); c.AddPaths(regionPoly, PolyType.ptClip, true); c.AddPaths(boundaryPoly, PolyType.ptSubject, true); c.Execute(ClipType.ctIntersection, result, PolyFillType.pftEvenOdd, PolyFillType.pftEvenOdd); List> clippedPolygons = new List>(); foreach (Polygon poly in result) { List clippedPoly = new List(); foreach (IntPoint p in poly) { clippedPoly.Add(new Vector2(p.X, p.Y) / multiplier); } clippedPolygons.Add(clippedPoly); } return clippedPolygons; } private static Polygons createPolygons(List source) { Polygons poly = new Polygons(1); poly.Add(new Polygon(source.Count)); foreach (Vector2 p in source) { poly[0].Add(new IntPoint(p.x * multiplier, p.y * multiplier)); } return poly; } private static Polygons createPolygons(Triangle tri) { Polygons poly = new Polygons(1); poly.Add(new Polygon(3)); for (int i = 0; i < 3; i++) { poly[0].Add(new IntPoint(tri.sites[i].x * multiplier, tri.sites[i].y * multiplier)); } return poly; } }