public static Path2D CreateRect(Rect aRect) { Path2D result = new Path2D(); result.Closed = true; result.Add(new Vector2(aRect.xMin, aRect.yMin), PointType.Sharp); result.Add(new Vector2(aRect.xMax, aRect.yMin), PointType.Sharp); result.Add(new Vector2(aRect.xMax, aRect.yMax), PointType.Sharp); result.Add(new Vector2(aRect.xMin, aRect.yMax), PointType.Sharp); return(result); }
public static Path2D CreateStrip(List <Vector2> aPoints, float aWidth) { Path2D result = new Path2D(); result.Closed = true; for (int i = 0; i < aPoints.Count; i++) { result.Add(aPoints[i] + PathUtil.GetPointNormal(i, aPoints, false) * aWidth); } for (int i = aPoints.Count - 1; i >= 0; i--) { result.Add(aPoints[i] - PathUtil.GetPointNormal(i, aPoints, false) * aWidth); } return(result); }
public Path2D BoolAdd(Path2D aOther) { if (!(Closed && aOther.Closed) || SelfIntersecting() || aOther.SelfIntersecting()) { if (!Closed || !aOther.Closed) { Debug.LogWarning("Paths must be closed for boolean operations!"); } if (SelfIntersecting()) { Debug.LogWarning("This path is self-intersecting!"); } if (aOther.SelfIntersecting()) { Debug.LogWarning("Other path is self-intersecting!"); } Path2D r = new Path2D(); r.Closed = true; return(r); } if (!IsClockwise()) { ReverseSelf(); } if (!aOther.IsClockwise()) { aOther.ReverseSelf(); } // find a point that is not within the other path int id = -1; for (int i = 0; i < _points.Count; i++) { if (!aOther.Contains(_points[i])) { id = i; break; } } // if it's entirely enclosed, then the other path is the result if (id == -1) { return(new Path2D(aOther)); } int currId = id; List <Vector2> currPath = _points; List <Vector2> otherPath = aOther._points; Vector2 start = currPath[currId]; Vector2 curr = start; int ignoreSegment = -1; Path2D result = new Path2D(); result.Closed = true; result.Add(start); int escape = (currPath.Count + otherPath.Count) * 2; while (!(curr == start && result.Count > 1)) { int nextId = (currId + 1) % currPath.Count; Vector2 next = currPath[nextId]; float dist = float.MaxValue; Vector2 nextPt = next; bool swapPaths = false; for (int i = 0; i < otherPath.Count; i++) { if (i == ignoreSegment) { continue; } Vector2 o1 = otherPath[i]; Vector2 o2 = otherPath[(i + 1) % otherPath.Count]; if (PathUtil.LineSegmentIntersection(curr, next, o1, o2)) { Vector2 pt = PathUtil.LineIntersectionPoint(curr, next, o1, o2); float mag = (curr - pt).sqrMagnitude; if (mag < dist) { dist = mag; nextPt = pt; swapPaths = true; nextId = i; } } } ignoreSegment = -1; result.Add(nextPt); if (swapPaths) { List <Vector2> t = currPath; currPath = otherPath; otherPath = t; ignoreSegment = currId; } currId = nextId; curr = nextPt; escape--; if (escape <= 0) { break; } } return(result); }