Skip to content

Commit 842e2e6

Browse files
authored
1.4.0 (#30)
1 parent 2c46be5 commit 842e2e6

File tree

11 files changed

+181
-196
lines changed

11 files changed

+181
-196
lines changed

src/main/java/clipper2/Clipper.java

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ public static RectD GetBounds(PathD path) {
808808
result.bottom = pt.y;
809809
}
810810
}
811-
return result.left == Double.MAX_VALUE ? new RectD() : result;
811+
return InternalClipper.IsAlmostZero(result.left - Double.MAX_VALUE) ? new RectD() : result;
812812
}
813813

814814
public static RectD GetBounds(PathsD paths) {
@@ -829,7 +829,7 @@ public static RectD GetBounds(PathsD paths) {
829829
}
830830
}
831831
}
832-
return result.left == Double.MAX_VALUE ? new RectD() : result;
832+
return InternalClipper.IsAlmostZero(result.left - Double.MAX_VALUE) ? new RectD() : result;
833833
}
834834

835835
public static Path64 MakePath(int[] arr) {
@@ -863,6 +863,36 @@ public static double Sqr(double value) {
863863
return value * value;
864864
}
865865

866+
public static double Sqr(long value) {
867+
return (double) value * (double) value;
868+
}
869+
870+
public static double DistanceSqr(Point64 pt1, Point64 pt2) {
871+
return Sqr(pt1.x - pt2.x) + Sqr(pt1.y - pt2.y);
872+
}
873+
874+
public static Point64 MidPoint(Point64 pt1, Point64 pt2) {
875+
return new Point64((pt1.x + pt2.x) / 2, (pt1.y + pt2.y) / 2);
876+
}
877+
878+
public static PointD MidPoint(PointD pt1, PointD pt2) {
879+
return new PointD((pt1.x + pt2.x) / 2, (pt1.y + pt2.y) / 2);
880+
}
881+
882+
public static void InflateRect(Rect64 rec, int dx, int dy) {
883+
rec.left -= dx;
884+
rec.right += dx;
885+
rec.top -= dy;
886+
rec.bottom += dy;
887+
}
888+
889+
public static void InflateRect(RectD rec, double dx, double dy) {
890+
rec.left -= dx;
891+
rec.right += dx;
892+
rec.top -= dy;
893+
rec.bottom += dy;
894+
}
895+
866896
public static boolean PointsNearEqual(PointD pt1, PointD pt2, double distanceSqrd) {
867897
return Sqr(pt1.x - pt2.x) + Sqr(pt1.y - pt2.y) < distanceSqrd;
868898
}
@@ -1366,10 +1396,10 @@ public static Path64 TrimCollinear(Path64 path, boolean isOpen) {
13661396
int len = path.size();
13671397
int i = 0;
13681398
if (!isOpen) {
1369-
while (i < len - 1 && InternalClipper.CrossProduct(path.get(len - 1), path.get(i), path.get(i + 1)) == 0) {
1399+
while (i < len - 1 && InternalClipper.IsCollinear(path.get(len - 1), path.get(i), path.get(i + 1))) {
13701400
i++;
13711401
}
1372-
while (i < len - 1 && InternalClipper.CrossProduct(path.get(len - 2), path.get(len - 1), path.get(i)) == 0) {
1402+
while (i < len - 1 && InternalClipper.IsCollinear(path.get(len - 2), path.get(len - 1), path.get(i))) {
13731403
len--;
13741404
}
13751405
}
@@ -1385,7 +1415,7 @@ public static Path64 TrimCollinear(Path64 path, boolean isOpen) {
13851415
Point64 last = path.get(i);
13861416
result.add(last);
13871417
for (i++; i < len - 1; i++) {
1388-
if (InternalClipper.CrossProduct(last, path.get(i), path.get(i + 1)) == 0) {
1418+
if (InternalClipper.IsCollinear(last, path.get(i), path.get(i + 1))) {
13891419
continue;
13901420
}
13911421
last = path.get(i);
@@ -1394,10 +1424,10 @@ public static Path64 TrimCollinear(Path64 path, boolean isOpen) {
13941424

13951425
if (isOpen) {
13961426
result.add(path.get(len - 1));
1397-
} else if (InternalClipper.CrossProduct(last, path.get(len - 1), result.get(0)) != 0) {
1427+
} else if (!InternalClipper.IsCollinear(last, path.get(len - 1), result.get(0))) {
13981428
result.add(path.get(len - 1));
13991429
} else {
1400-
while (result.size() > 2 && InternalClipper.CrossProduct(result.get(result.size() - 1), result.get(result.size() - 2), result.get(0)) == 0) {
1430+
while (result.size() > 2 && InternalClipper.IsCollinear(result.get(result.size() - 1), result.get(result.size() - 2), result.get(0))) {
14011431
result.remove(result.size() - 1);
14021432
}
14031433
if (result.size() < 3) {

src/main/java/clipper2/core/InternalClipper.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static long CheckCastInt64(double val) {
5252
return (long) Math.rint(val);
5353
}
5454

55-
public static boolean GetIntersectPoint(Point64 ln1a, Point64 ln1b, Point64 ln2a, Point64 ln2b, /* out */ Point64 ip) {
55+
public static boolean GetSegmentIntersectPt(Point64 ln1a, Point64 ln1b, Point64 ln2a, Point64 ln2b, /* out */ Point64 ip) {
5656
double dy1 = (ln1b.y - ln1a.y);
5757
double dx1 = (ln1b.x - ln1a.x);
5858
double dy2 = (ln2b.y - ln2a.y);
@@ -86,6 +86,11 @@ public static boolean GetIntersectPoint(Point64 ln1a, Point64 ln1b, Point64 ln2a
8686
return true;
8787
}
8888

89+
@Deprecated
90+
public static boolean GetIntersectPoint(Point64 ln1a, Point64 ln1b, Point64 ln2a, Point64 ln2b, /* out */ Point64 ip) {
91+
return GetSegmentIntersectPt(ln1a, ln1b, ln2a, ln2b, ip);
92+
}
93+
8994
public static boolean SegsIntersect(Point64 seg1a, Point64 seg1b, Point64 seg2a, Point64 seg2b) {
9095
return SegsIntersect(seg1a, seg1b, seg2a, seg2b, false);
9196
}
@@ -295,4 +300,4 @@ private static int triSign(long x) {
295300
return x > 0 ? 1 : (x < 0 ? -1 : 0);
296301
}
297302

298-
}
303+
}

src/main/java/clipper2/core/Path64.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ public Path64(Point64... path) {
3636
public String toString() {
3737
StringBuilder s = new StringBuilder();
3838
for (Point64 p : this) {
39-
s.append(p.toString()).append(" ");
39+
s.append(p.toString()).append(", ");
40+
}
41+
if (!s.isEmpty()) {
42+
s.setLength(s.length() - 2);
4043
}
4144
return s.toString();
4245
}

src/main/java/clipper2/core/PathD.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ public PathD(List<PointD> path) {
3131
public String toString() {
3232
StringBuilder s = new StringBuilder();
3333
for (PointD p : this) {
34-
s.append(p.toString()).append(" ");
34+
s.append(p.toString()).append(", ");
35+
}
36+
if (!s.isEmpty()) {
37+
s.setLength(s.length() - 2);
3538
}
3639
return s.toString();
3740
}

src/main/java/clipper2/core/Paths64.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public Paths64(Path64... paths) {
3232
public String toString() {
3333
StringBuilder s = new StringBuilder();
3434
for (Path64 p : this) {
35-
s.append(p.toString()).append("\n");
35+
s.append(p).append("\n");
3636
}
3737
return s.toString();
3838
}

0 commit comments

Comments
 (0)