-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathPolyPathBase.java
More file actions
109 lines (93 loc) · 2.46 KB
/
PolyPathBase.java
File metadata and controls
109 lines (93 loc) · 2.46 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package clipper2.engine;
import java.util.ArrayList;
import java.util.List;
import clipper2.Nullable;
import clipper2.core.Path64;
public abstract class PolyPathBase implements Iterable<PolyPathBase> {
@Nullable
PolyPathBase parent;
List<PolyPathBase> children = new ArrayList<>();
PolyPathBase(@Nullable PolyPathBase parent) {
this.parent = parent;
}
PolyPathBase() {
this(null);
}
@Override
public final NodeIterator iterator() {
return new NodeIterator(children);
}
private int getLevel() {
int result = 0;
@Nullable
PolyPathBase pp = parent;
while (pp != null) {
++result;
pp = pp.parent;
}
return result;
}
/**
* Indicates whether the Polygon property represents a hole or the outer bounds
* of a polygon.
*/
public final boolean getIsHole() {
int lvl = getLevel();
return lvl != 0 && (lvl & 1) == 0;
}
/**
* Indicates the number of contained children.
*/
public final int getCount() {
return children.size();
}
public abstract PolyPathBase AddChild(Path64 p);
/**
* This method clears the Polygon and deletes any contained children.
*/
public final void Clear() {
children.clear();
}
private String toStringInternal(int idx, int level) {
int count = children.size();
StringBuilder result = new StringBuilder();
StringBuilder padding = new StringBuilder();
String plural = "s";
if (children.size() == 1) {
plural = "";
}
// Create padding by concatenating spaces
for (int i = 0; i < level * 2; i++) {
padding.append(" ");
}
if ((level & 1) == 0) {
result.append(String.format("%s+- hole (%d) contains %d nested polygon%s.\n", padding, idx, children.size(), plural));
} else {
result.append(String.format("%s+- polygon (%d) contains %d hole%s.\n", padding, idx, children.size(), plural));
}
for (int i = 0; i < count; i++) {
if (children.get(i).getCount() > 0) {
result.append(children.get(i).toStringInternal(i, level + 1));
}
}
return result.toString();
}
@Override
public String toString() {
int count = children.size();
if (getLevel() > 0) {
return ""; // only accept tree root
}
String plural = "s";
if (children.size() == 1) {
plural = "";
}
StringBuilder result = new StringBuilder(String.format("Polytree with %d polygon%s.\n", children.size(), plural));
for (int i = 0; i < count; i++) {
if (children.get(i).getCount() > 0) {
result.append(children.get(i).toStringInternal(i, 1));
}
}
return result.append('\n').toString();
}
}