-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPolygonsIntersection.java
263 lines (235 loc) · 7.57 KB
/
PolygonsIntersection.java
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package Geometry;
import java.awt.Polygon;
import java.awt.geom.Area;
import java.awt.geom.PathIterator;
import java.util.*;
public class PolygonsIntersection {
public static double overlap(Point[] polygon1, Point[] polygon2) {
Point[][] polygons = {polygon1, polygon2};
Set<Double> xs = new TreeSet<>();
for (Point point : polygon1)
xs.add(point.x);
for (Point point : polygon2)
xs.add(point.x);
for (int i1 = 0, j1 = polygon1.length - 1; i1 < polygon1.length; j1 = i1++) {
for (int i2 = 0, j2 = polygon2.length - 1; i2 < polygon2.length; j2 = i2++) {
Point intersection = getSegmentsIntersection(polygon1[i1], polygon1[j1], polygon2[i2], polygon2[j2]);
if (intersection != null) {
xs.add(intersection.x);
}
}
}
Double[] xsa = xs.toArray(new Double[xs.size()]);
double res = 0.0;
for (int k = 0; k + 1 < xsa.length; k++) {
double x = (xsa[k] + xsa[k + 1]) * 0.5;
Point sweep0 = new Point(x, 0);
Point sweep1 = new Point(x, 1);
List<Event> events = new ArrayList<>();
for (int p = 0; p < 2; p++) {
Point[] polygon = polygons[p];
double area = 0;
for (int i = 0, j = polygon.length - 1; i < polygon.length; j = i++)
area += (polygon[j].x - polygon[i].x) * (polygon[j].y + polygon[i].y);
for (int j = 0, i = polygon.length - 1; j < polygon.length; i = j++) {
Point intersection = getLinesIntersection(polygon[j], polygon[i], sweep0, sweep1);
if (intersection != null) {
double y = intersection.y;
double x0 = polygon[i].x;
double x1 = polygon[j].x;
if (x0 < x && x1 > x) {
events.add(new Event(y, (int) Math.signum(area) * (1 << p)));
} else if (x0 > x && x1 < x) {
events.add(new Event(y, -(int) Math.signum(area) * (1 << p)));
}
}
}
}
Collections.sort(events);
double a = 0.0;
int mask = 0;
for (int j = 0; j < events.size(); j++) {
if (mask == 3)
a += events.get(j).y - events.get(j - 1).y;
mask += events.get(j).maskDelta;
}
res += a * (xsa[k + 1] - xsa[k]);
}
return res;
}
static class Point {
double x;
double y;
Point(double x, double y) {
this.x = x;
this.y = y;
}
}
static class Event implements Comparable<Event> {
public double y;
public int maskDelta;
public Event(double y, int maskDelta) {
this.y = y;
this.maskDelta = maskDelta;
}
@Override
public int compareTo(Event o) {
if (y != o.y)
return Double.compare(y, o.y);
return Integer.compare(maskDelta, o.maskDelta);
}
}
static final double eps = 1e-9;
static Point getLinesIntersection(Point p1, Point p2, Point p3, Point p4) {
double a1 = p2.y - p1.y;
double b1 = p1.x - p2.x;
double c1 = -(p1.x * p2.y - p2.x * p1.y);
double a2 = p4.y - p3.y;
double b2 = p3.x - p4.x;
double c2 = -(p3.x * p4.y - p4.x * p3.y);
double det = a1 * b2 - a2 * b1;
if (Math.abs(det) < eps)
return null;
double x = -(c1 * b2 - c2 * b1) / det;
double y = -(a1 * c2 - a2 * c1) / det;
return new Point(x, y);
}
static Point getSegmentsIntersection(Point p1, Point p2, Point p3, Point p4) {
if (!isCrossIntersect(p1, p2, p3, p4))
return null;
return getLinesIntersection(p1, p2, p3, p4);
}
static boolean isCrossIntersect(Point p1, Point p2, Point p3, Point p4) {
double z1 = (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x);
double z2 = (p2.x - p1.x) * (p4.y - p1.y) - (p2.y - p1.y) * (p4.x - p1.x);
double z3 = (p4.x - p3.x) * (p1.y - p3.y) - (p4.y - p3.y) * (p1.x - p3.x);
double z4 = (p4.x - p3.x) * (p2.y - p3.y) - (p4.y - p3.y) * (p2.x - p3.x);
return (z1 < -eps || z2 < -eps) && (z1 > eps || z2 > eps) && (z3 < -eps || z4 < -eps) && (z3 > eps || z4 > eps);
}
// random test
public static void main(String[] args) {
for (int step = 0; step < 1000; step++) {
int n = rnd.nextInt(20) + 3;
int range = 10;
Point[] polygon1 = convert(getRandomPolygon(n, range, range));
Point[] polygon2 = convert(getRandomPolygon(n, range, range));
double res1 = overlap(polygon1, polygon2);
double res2 = overlap2(polygon1, polygon2);
if (Math.abs(res1 - res2) > 1e-9)
throw new RuntimeException();
}
}
static Point[] convert(int[][] xy) {
Point[] polygon = new Point[xy[0].length];
for (int i = 0; i < xy[0].length; i++) {
polygon[i] = new Point(xy[0][i], xy[1][i]);
}
return polygon;
}
static double overlap2(Point[] points1, Point[] points2) {
Area a = new Area(getPolygon(points1));
a.intersect(new Area(getPolygon(points2)));
double pt[] = new double[6];
List<Double> x = new ArrayList<>();
List<Double> y = new ArrayList<>();
double area = 0;
for (PathIterator p = a.getPathIterator(null); !p.isDone(); p.next()) {
if (p.currentSegment(pt) == PathIterator.SEG_CLOSE) {
double cur = 0;
for (int i = 0, j = x.size() - 1; i < x.size(); j = i++)
cur += x.get(i) * y.get(j) - x.get(j) * y.get(i);
x.clear();
y.clear();
area += Math.abs(cur / 2);
} else {
x.add(pt[0]);
y.add(pt[1]);
}
}
return area;
}
static Polygon getPolygon(Point[] points) {
Polygon polygon = new Polygon();
for (Point point : points)
polygon.addPoint((int) point.x, (int) point.y);
return polygon;
}
static Random rnd = new Random(1);
static int[][] getRandomPolygon(int n, int maxWidth, int maxHeight) {
int[] x = new int[n];
int[] y = new int[n];
int[] p = new int[n];
while (true) {
for (int i = 0; i < n; i++) {
x[i] = rnd.nextInt(maxWidth);
y[i] = rnd.nextInt(maxHeight);
p[i] = i;
}
for (boolean improved = true; improved; ) {
improved = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int[] p1 = p.clone();
reverse(p1, i, j);
if (len(x, y, p) > len(x, y, p1)) {
p = p1;
improved = true;
}
}
}
}
int[] tx = x.clone();
int[] ty = y.clone();
for (int i = 0; i < n; i++) {
x[i] = tx[p[i]];
y[i] = ty[p[i]];
}
boolean ok = true;
for (int i = 0; i < n; i++) {
long x1 = x[(i - 1 + n) % n] - x[i];
long y1 = y[(i - 1 + n) % n] - y[i];
long x2 = x[(i + 1) % n] - x[i];
long y2 = y[(i + 1) % n] - y[i];
ok &= x1 * y2 - x2 * y1 != 0 || x1 * x2 + y1 * y2 <= 0;
}
for (int i2 = 0, i1 = p.length - 1; i2 < p.length; i1 = i2++)
for (int j2 = 0, j1 = p.length - 1; j2 < p.length; j1 = j2++)
ok &= i1 == j1 || i1 == j2 || i2 == j1
|| !isCrossOrTouchIntersect(x[i1], y[i1], x[i2], y[i2], x[j1], y[j1], x[j2], y[j2]);
if (ok)
return new int[][]{x, y};
}
}
// http://en.wikipedia.org/wiki/2-opt
static void reverse(int[] p, int i, int j) {
int n = p.length;
// reverse order from i to j
while (i != j) {
int t = p[j];
p[j] = p[i];
p[i] = t;
i = (i + 1) % n;
if (i == j) break;
j = (j - 1 + n) % n;
}
}
static double len(int[] x, int[] y, int[] p) {
double res = 0;
for (int i = 0, j = p.length - 1; i < p.length; j = i++) {
double dx = x[p[i]] - x[p[j]];
double dy = y[p[i]] - y[p[j]];
res += Math.sqrt(dx * dx + dy * dy);
}
return res;
}
static boolean isCrossOrTouchIntersect(long x1, long y1, long x2, long y2, long x3, long y3, long x4, long y4) {
if (Math.max(x1, x2) < Math.min(x3, x4) || Math.max(x3, x4) < Math.min(x1, x2)
|| Math.max(y1, y2) < Math.min(y3, y4) || Math.max(y3, y4) < Math.min(y1, y2))
return false;
long z1 = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1);
long z2 = (x2 - x1) * (y4 - y1) - (y2 - y1) * (x4 - x1);
long z3 = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
long z4 = (x4 - x3) * (y2 - y3) - (y4 - y3) * (x2 - x3);
return (z1 <= 0 || z2 <= 0) && (z1 >= 0 || z2 >= 0) && (z3 <= 0 || z4 <= 0) && (z3 >= 0 || z4 >= 0);
}
}