-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathMinkowski.java
More file actions
84 lines (70 loc) · 2.51 KB
/
Minkowski.java
File metadata and controls
84 lines (70 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package clipper2;
import clipper2.core.FillRule;
import clipper2.core.Path64;
import clipper2.core.PathD;
import clipper2.core.Paths64;
import clipper2.core.PathsD;
import clipper2.core.Point64;
public final class Minkowski {
private Minkowski() {
}
public static Paths64 Sum(Path64 pattern, Path64 path, boolean isClosed) {
return Clipper.Union(MinkowskiInternal(pattern, path, true, isClosed), FillRule.NonZero);
}
public static PathsD Sum(PathD pattern, PathD path, boolean isClosed) {
return Sum(pattern, path, isClosed, 2);
}
public static PathsD Sum(PathD pattern, PathD path, boolean isClosed, int decimalPlaces) {
double scale = Math.pow(10, decimalPlaces);
Paths64 tmp = Clipper.Union(
MinkowskiInternal(Clipper.ScalePath64(pattern, scale), Clipper.ScalePath64(path, scale), true, isClosed), FillRule.NonZero);
return Clipper.ScalePathsD(tmp, 1 / scale);
}
public static Paths64 Diff(Path64 pattern, Path64 path, boolean isClosed) {
return Clipper.Union(MinkowskiInternal(pattern, path, false, isClosed), FillRule.NonZero);
}
public static PathsD Diff(PathD pattern, PathD path, boolean isClosed) {
return Diff(pattern, path, isClosed, 2);
}
public static PathsD Diff(PathD pattern, PathD path, boolean isClosed, int decimalPlaces) {
double scale = Math.pow(10, decimalPlaces);
Paths64 tmp = Clipper.Union(
MinkowskiInternal(Clipper.ScalePath64(pattern, scale), Clipper.ScalePath64(path, scale), false, isClosed),
FillRule.NonZero);
return Clipper.ScalePathsD(tmp, 1 / scale);
}
private static Paths64 MinkowskiInternal(Path64 pattern, Path64 path, boolean isSum, boolean isClosed) {
int delta = isClosed ? 0 : 1;
int patLen = pattern.size(), pathLen = path.size();
Paths64 tmp = new Paths64(pathLen);
for (Point64 pathPt : path) {
Path64 path2 = new Path64(patLen);
if (isSum) {
for (Point64 basePt : pattern) {
path2.add(Point64.opAdd(pathPt, basePt));
}
} else {
for (Point64 basePt : pattern) {
path2.add(Point64.opSubtract(pathPt, basePt));
}
}
tmp.add(path2);
}
Paths64 result = new Paths64((pathLen - delta) * patLen);
int g = isClosed ? pathLen - 1 : 0;
int h = patLen - 1;
for (int i = delta; i < pathLen; i++) {
for (int j = 0; j < patLen; j++) {
Path64 quad = new Path64(tmp.get(g).get(h), tmp.get(i).get(h), tmp.get(i).get(j), tmp.get(g).get(j));
if (!Clipper.IsPositive(quad)) {
result.add(Clipper.ReversePath(quad));
} else {
result.add(quad);
}
h = j;
}
g = i;
}
return result;
}
}