1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2005, 2010 IBM Corporation and others.
|
3
|
* All rights reserved. This program and the accompanying materials
|
4
|
* are made available under the terms of the Eclipse Public License v1.0
|
5
|
* which accompanies this distribution, and is available at
|
6
|
* http://www.eclipse.org/legal/epl-v10.html
|
7
|
*
|
8
|
* Contributors:
|
9
|
* IBM Corporation - initial API and implementation
|
10
|
* Alexander Shatalin (Borland) - Contribution for Bug 238874
|
11
|
*******************************************************************************/
|
12
|
package org.eclipse.draw2d.geometry;
|
13
|
|
14
|
/**
|
15
|
* A Utilities class for geometry operations.
|
16
|
*
|
17
|
* @author Pratik Shah
|
18
|
* @author Alexander Nyssen
|
19
|
* @since 3.1
|
20
|
*/
|
21
|
public class Geometry {
|
22
|
|
23
|
/**
|
24
|
* Determines whether the two line segments p1->p2 and p3->p4, given by
|
25
|
* p1=(x1, y1), p2=(x2,y2), p3=(x3,y3), p4=(x4,y4) intersect. Two line
|
26
|
* segments are regarded to be intersecting in case they share at least one
|
27
|
* common point, i.e if one of the two line segments starts or ends on the
|
28
|
* other line segment or the line segments are collinear and overlapping,
|
29
|
* then they are as well considered to be intersecting.
|
30
|
*
|
31
|
* @param x1
|
32
|
* x coordinate of starting point of line segment 1
|
33
|
* @param y1
|
34
|
* y coordinate of starting point of line segment 1
|
35
|
* @param x2
|
36
|
* x coordinate of ending point of line segment 1
|
37
|
* @param y2
|
38
|
* y coordinate of ending point of line segment 1
|
39
|
* @param x3
|
40
|
* x coordinate of the starting point of line segment 2
|
41
|
* @param y3
|
42
|
* y coordinate of the starting point of line segment 2
|
43
|
* @param x4
|
44
|
* x coordinate of the ending point of line segment 2
|
45
|
* @param y4
|
46
|
* y coordinate of the ending point of line segment 2
|
47
|
*
|
48
|
* @return <code>true</code> if the two line segments formed by the given
|
49
|
* coordinates share at least one common point.
|
50
|
*
|
51
|
* @since 3.1
|
52
|
*/
|
53
|
public static boolean linesIntersect(int x1, int y1, int x2, int y2,
|
54
|
int x3, int y3, int x4, int y4) {
|
55
|
|
56
|
// calculate bounding box of segment p1->p2
|
57
|
int bb1_x = Math.min(x1, x2);
|
58
|
int bb1_y = Math.min(y1, y2);
|
59
|
int bb2_x = Math.max(x1, x2);
|
60
|
int bb2_y = Math.max(y1, y2);
|
61
|
|
62
|
// calculate bounding box of segment p3->p4
|
63
|
int bb3_x = Math.min(x3, x4);
|
64
|
int bb3_y = Math.min(y3, y4);
|
65
|
int bb4_x = Math.max(x3, x4);
|
66
|
int bb4_y = Math.max(y3, y4);
|
67
|
|
68
|
// check if bounding boxes intersect
|
69
|
if (!(bb2_x >= bb3_x && bb4_x >= bb1_x && bb2_y >= bb3_y && bb4_y >= bb1_y)) {
|
70
|
// if bounding boxes do not intersect, line segments cannot
|
71
|
// intersect either
|
72
|
return false;
|
73
|
}
|
74
|
|
75
|
// If p3->p4 is inside the triangle p1-p2-p3, then check whether the
|
76
|
// line p1->p2 crosses the line p3->p4.
|
77
|
long p1p3_x = (long) x1 - x3;
|
78
|
long p1p3_y = (long) y1 - y3;
|
79
|
long p2p3_x = (long) x2 - x3;
|
80
|
long p2p3_y = (long) y2 - y3;
|
81
|
long p3p4_x = (long) x3 - x4;
|
82
|
long p3p4_y = (long) y3 - y4;
|
83
|
if (productSign(crossProduct(p2p3_x, p2p3_y, p3p4_x, p3p4_y),
|
84
|
crossProduct(p3p4_x, p3p4_y, p1p3_x, p1p3_y)) >= 0) {
|
85
|
long p2p1_x = (long) x2 - x1;
|
86
|
long p2p1_y = (long) y2 - y1;
|
87
|
long p1p4_x = (long) x1 - x4;
|
88
|
long p1p4_y = (long) y1 - y4;
|
89
|
return productSign(crossProduct(-p1p3_x, -p1p3_y, p2p1_x, p2p1_y),
|
90
|
crossProduct(p2p1_x, p2p1_y, p1p4_x, p1p4_y)) <= 0;
|
91
|
}
|
92
|
return false;
|
93
|
}
|
94
|
|
95
|
private static int productSign(long x, long y) {
|
96
|
if (x == 0 || y == 0) {
|
97
|
return 0;
|
98
|
} else if (x < 0 ^ y < 0) {
|
99
|
return -1;
|
100
|
}
|
101
|
return 1;
|
102
|
}
|
103
|
|
104
|
private static long crossProduct(long x1, long y1, long x2, long y2) {
|
105
|
return x1 * y2 - x2 * y1;
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* @see PointList#polylineContainsPoint(int, int, int)
|
110
|
* @since 3.5
|
111
|
*/
|
112
|
public static boolean polylineContainsPoint(PointList points, int x, int y,
|
113
|
int tolerance) {
|
114
|
int coordinates[] = points.toIntArray();
|
115
|
/*
|
116
|
* For each segment of PolyLine calling isSegmentPoint
|
117
|
*/
|
118
|
for (int index = 0; index < coordinates.length - 3; index += 2) {
|
119
|
if (segmentContainsPoint(coordinates[index],
|
120
|
coordinates[index + 1], coordinates[index + 2],
|
121
|
coordinates[index + 3], x, y, tolerance)) {
|
122
|
return true;
|
123
|
}
|
124
|
}
|
125
|
return false;
|
126
|
}
|
127
|
|
128
|
/**
|
129
|
* @return true if the least distance between point (px,py) and segment
|
130
|
* (x1,y1) - (x2,y2) is less then specified tolerance
|
131
|
*/
|
132
|
private static boolean segmentContainsPoint(int x1, int y1, int x2, int y2,
|
133
|
int px, int py, int tolerance) {
|
134
|
/*
|
135
|
* Point should be located inside Rectangle(x1 -+ tolerance, y1 -+
|
136
|
* tolerance, x2 +- tolerance, y2 +- tolerance)
|
137
|
*/
|
138
|
Rectangle lineBounds = Rectangle.SINGLETON;
|
139
|
lineBounds.setSize(0, 0);
|
140
|
lineBounds.setLocation(x1, y1);
|
141
|
lineBounds.union(x2, y2);
|
142
|
lineBounds.expand(tolerance, tolerance);
|
143
|
if (!lineBounds.contains(px, py)) {
|
144
|
return false;
|
145
|
}
|
146
|
|
147
|
/*
|
148
|
* If this is horizontal, vertical line or dot then the distance between
|
149
|
* specified point and segment is not more then tolerance (due to the
|
150
|
* lineBounds check above)
|
151
|
*/
|
152
|
if (x1 == x2 || y1 == y2) {
|
153
|
return true;
|
154
|
}
|
155
|
|
156
|
/*
|
157
|
* Calculating square distance from specified point to this segment
|
158
|
* using formula for Dot product of two vectors.
|
159
|
*/
|
160
|
long v1x = x2 - x1;
|
161
|
long v1y = y2 - y1;
|
162
|
long v2x = px - x1;
|
163
|
long v2y = py - y1;
|
164
|
long numerator = v2x * v1y - v1x * v2y;
|
165
|
long denominator = v1x * v1x + v1y * v1y;
|
166
|
long squareDistance = numerator * numerator / denominator;
|
167
|
return squareDistance <= tolerance * tolerance;
|
168
|
}
|
169
|
|
170
|
/**
|
171
|
* One simple way of finding whether the point is inside or outside a simple
|
172
|
* polygon is to test how many times a ray starting from the point
|
173
|
* intersects the edges of the polygon. If the point in question is not on
|
174
|
* the boundary of the polygon, the number of intersections is an even
|
175
|
* number if the point is outside, and it is odd if inside.
|
176
|
*
|
177
|
* @see PointList#polygonContainsPoint(int, int)
|
178
|
* @since 3.5
|
179
|
*/
|
180
|
public static boolean polygonContainsPoint(PointList points, int x, int y) {
|
181
|
boolean isOdd = false;
|
182
|
int coordinates[] = points.toIntArray();
|
183
|
int n = coordinates.length;
|
184
|
if (n > 3) { // If there are at least 2 Points (4 ints)
|
185
|
int x1, y1;
|
186
|
int x0 = coordinates[n - 2];
|
187
|
int y0 = coordinates[n - 1];
|
188
|
|
189
|
for (int i = 0; i < n; x0 = x1, y0 = y1) {
|
190
|
x1 = coordinates[i++];
|
191
|
y1 = coordinates[i++];
|
192
|
if (!segmentContaintPoint(y0, y1, y)) {
|
193
|
// Current edge has no intersection with the point by Y
|
194
|
// coordinates
|
195
|
continue;
|
196
|
}
|
197
|
int crossProduct = crossProduct(x1, y1, x0, y0, x, y);
|
198
|
if (crossProduct == 0) {
|
199
|
// cross product == 0 only if this point is on the line
|
200
|
// containing selected edge
|
201
|
if (segmentContaintPoint(x0, x1, x)) {
|
202
|
// This point is on the edge
|
203
|
return true;
|
204
|
}
|
205
|
// This point is outside the edge - simply skipping possible
|
206
|
// intersection (no parity changes)
|
207
|
} else if ((y0 <= y && y < y1 && crossProduct > 0)
|
208
|
|| (y1 <= y && y < y0 && crossProduct < 0)) {
|
209
|
// has intersection
|
210
|
isOdd = !isOdd;
|
211
|
}
|
212
|
}
|
213
|
return isOdd;
|
214
|
}
|
215
|
return false;
|
216
|
}
|
217
|
|
218
|
/**
|
219
|
* @return true if segment with two ends x0, x1 contains point x
|
220
|
*/
|
221
|
private static boolean segmentContaintPoint(int x0, int x1, int x) {
|
222
|
return !((x < x0 && x < x1) || (x > x0 && x > x1));
|
223
|
}
|
224
|
|
225
|
/**
|
226
|
* Calculating cross product of two vectors: 1. [ax - cx, ay - cx] 2. [bx -
|
227
|
* cx, by - cy]
|
228
|
*/
|
229
|
private static int crossProduct(int ax, int ay, int bx, int by, int cx,
|
230
|
int cy) {
|
231
|
return (ax - cx) * (by - cy) - (ay - cy) * (bx - cx);
|
232
|
}
|
233
|
|
234
|
}
|