Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions src/main/java/clipper2/Clipper.java
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ public static RectD GetBounds(PathD path) {
result.bottom = pt.y;
}
}
return result.left == Double.MAX_VALUE ? new RectD() : result;
return InternalClipper.IsAlmostZero(result.left - Double.MAX_VALUE) ? new RectD() : result;
}

public static RectD GetBounds(PathsD paths) {
Expand All @@ -829,7 +829,7 @@ public static RectD GetBounds(PathsD paths) {
}
}
}
return result.left == Double.MAX_VALUE ? new RectD() : result;
return InternalClipper.IsAlmostZero(result.left - Double.MAX_VALUE) ? new RectD() : result;
}

public static Path64 MakePath(int[] arr) {
Expand Down Expand Up @@ -863,6 +863,36 @@ public static double Sqr(double value) {
return value * value;
}

public static double Sqr(long value) {
return (double) value * (double) value;
}

public static double DistanceSqr(Point64 pt1, Point64 pt2) {
return Sqr(pt1.x - pt2.x) + Sqr(pt1.y - pt2.y);
}

public static Point64 MidPoint(Point64 pt1, Point64 pt2) {
return new Point64((pt1.x + pt2.x) / 2, (pt1.y + pt2.y) / 2);
}

public static PointD MidPoint(PointD pt1, PointD pt2) {
return new PointD((pt1.x + pt2.x) / 2, (pt1.y + pt2.y) / 2);
}

public static void InflateRect(Rect64 rec, int dx, int dy) {
rec.left -= dx;
rec.right += dx;
rec.top -= dy;
rec.bottom += dy;
}

public static void InflateRect(RectD rec, double dx, double dy) {
rec.left -= dx;
rec.right += dx;
rec.top -= dy;
rec.bottom += dy;
}

public static boolean PointsNearEqual(PointD pt1, PointD pt2, double distanceSqrd) {
return Sqr(pt1.x - pt2.x) + Sqr(pt1.y - pt2.y) < distanceSqrd;
}
Expand Down Expand Up @@ -1366,10 +1396,10 @@ public static Path64 TrimCollinear(Path64 path, boolean isOpen) {
int len = path.size();
int i = 0;
if (!isOpen) {
while (i < len - 1 && InternalClipper.CrossProduct(path.get(len - 1), path.get(i), path.get(i + 1)) == 0) {
while (i < len - 1 && InternalClipper.IsCollinear(path.get(len - 1), path.get(i), path.get(i + 1))) {
i++;
}
while (i < len - 1 && InternalClipper.CrossProduct(path.get(len - 2), path.get(len - 1), path.get(i)) == 0) {
while (i < len - 1 && InternalClipper.IsCollinear(path.get(len - 2), path.get(len - 1), path.get(i))) {
len--;
}
}
Expand All @@ -1385,7 +1415,7 @@ public static Path64 TrimCollinear(Path64 path, boolean isOpen) {
Point64 last = path.get(i);
result.add(last);
for (i++; i < len - 1; i++) {
if (InternalClipper.CrossProduct(last, path.get(i), path.get(i + 1)) == 0) {
if (InternalClipper.IsCollinear(last, path.get(i), path.get(i + 1))) {
continue;
}
last = path.get(i);
Expand All @@ -1394,10 +1424,10 @@ public static Path64 TrimCollinear(Path64 path, boolean isOpen) {

if (isOpen) {
result.add(path.get(len - 1));
} else if (InternalClipper.CrossProduct(last, path.get(len - 1), result.get(0)) != 0) {
} else if (!InternalClipper.IsCollinear(last, path.get(len - 1), result.get(0))) {
result.add(path.get(len - 1));
} else {
while (result.size() > 2 && InternalClipper.CrossProduct(result.get(result.size() - 1), result.get(result.size() - 2), result.get(0)) == 0) {
while (result.size() > 2 && InternalClipper.IsCollinear(result.get(result.size() - 1), result.get(result.size() - 2), result.get(0))) {
result.remove(result.size() - 1);
}
if (result.size() < 3) {
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/clipper2/core/InternalClipper.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static long CheckCastInt64(double val) {
return (long) Math.rint(val);
}

public static boolean GetIntersectPoint(Point64 ln1a, Point64 ln1b, Point64 ln2a, Point64 ln2b, /* out */ Point64 ip) {
public static boolean GetSegmentIntersectPt(Point64 ln1a, Point64 ln1b, Point64 ln2a, Point64 ln2b, /* out */ Point64 ip) {
double dy1 = (ln1b.y - ln1a.y);
double dx1 = (ln1b.x - ln1a.x);
double dy2 = (ln2b.y - ln2a.y);
Expand Down Expand Up @@ -86,6 +86,11 @@ public static boolean GetIntersectPoint(Point64 ln1a, Point64 ln1b, Point64 ln2a
return true;
}

@Deprecated
public static boolean GetIntersectPoint(Point64 ln1a, Point64 ln1b, Point64 ln2a, Point64 ln2b, /* out */ Point64 ip) {
return GetSegmentIntersectPt(ln1a, ln1b, ln2a, ln2b, ip);
}

public static boolean SegsIntersect(Point64 seg1a, Point64 seg1b, Point64 seg2a, Point64 seg2b) {
return SegsIntersect(seg1a, seg1b, seg2a, seg2b, false);
}
Expand Down Expand Up @@ -295,4 +300,4 @@ private static int triSign(long x) {
return x > 0 ? 1 : (x < 0 ? -1 : 0);
}

}
}
5 changes: 4 additions & 1 deletion src/main/java/clipper2/core/Path64.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public Path64(Point64... path) {
public String toString() {
StringBuilder s = new StringBuilder();
for (Point64 p : this) {
s.append(p.toString()).append(" ");
s.append(p.toString()).append(", ");
}
if (!s.isEmpty()) {
s.setLength(s.length() - 2);
}
return s.toString();
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/clipper2/core/PathD.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public PathD(List<PointD> path) {
public String toString() {
StringBuilder s = new StringBuilder();
for (PointD p : this) {
s.append(p.toString()).append(" ");
s.append(p.toString()).append(", ");
}
if (!s.isEmpty()) {
s.setLength(s.length() - 2);
}
return s.toString();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/clipper2/core/Paths64.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Paths64(Path64... paths) {
public String toString() {
StringBuilder s = new StringBuilder();
for (Path64 p : this) {
s.append(p.toString()).append("\n");
s.append(p).append("\n");
}
return s.toString();
}
Expand Down
Loading