Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 162082 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/draw2d/geometry/Geometry.java (-52 / +69 lines)
Lines 18-77 Link Here
18
 */
18
 */
19
public class Geometry
19
public class Geometry
20
{
20
{
21
	/**
22
	 * Determines whether the two line segments formed by the given coordinates
23
	 * intersect. If one of the two line segments starts or ends on the other
24
	 * line, then they are considered to be intersecting.
25
	 * TODO: Decide whether co-linear overlapping line segments should be regarded
26
	 *       as intersecting as well.
27
	 * 
28
	 * @param ux
29
	 *            x coordinate of starting point of line 1
30
	 * @param uy
31
	 *            y coordinate of starting point of line 1
32
	 * @param vx
33
	 *            x coordinate of ending point of line 1
34
	 * @param vy
35
	 *            y coordinate of ending point of line 1
36
	 * @param sx
37
	 *            x coordinate of the starting point of line 2
38
	 * @param sy
39
	 *            y coordinate of the starting point of line 2
40
	 * @param tx
41
	 *            x coordinate of the ending point of line 2
42
	 * @param ty
43
	 *            y coordinate of the ending point of line 2
44
	 * @return <code>true</code> if the two line segments formed by the given
45
	 *         coordinates cross
46
	 * @since 3.1
47
	 */
48
	public static boolean linesIntersect(int ux, int uy, int vx, int vy,
49
			int sx, int sy, int tx, int ty) {
50
		/*
51
		 * The following implementation is based on the line intersection
52
		 * algorithm published by Paul Bourke
53
		 * (http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/).
54
		 */
55
		double den = (((double) ty - (double) sy) * ((double) vx - (double) ux))
56
				- (((double) tx - (double) sx) * ((double) vy - (double) uy));
57
58
		double numA = (((double) tx - (double) sx) * ((double) uy - (double) sy))
59
				- (((double) ty - (double) sy) * ((double) ux - (double) sx));
60
61
		double numB = (((double) vx - (double) ux) * ((double) uy - (double) sy))
62
				- (((double) vy - (double) uy) * ((double) ux - (double) sx));
63
64
		if (den == 0.0) {
65
			if (numA == 0.0 && numB == 0.0) {
66
				// co-linear
67
				// TODO: decide whether co-linear overlapping line segments should be 
68
				// regarded as 
69
				// check if there is an overlap (sufficient to check x coordinates only)
70
				if ((sx > vx && tx > vx) || (sx < ux && tx < ux)) {
71
					// co-linear & non-overlapping
72
					return false;
73
				}
74
				// co-linear & overlapping
75
				return true;
76
			}
77
			// parallel
78
			return false;
79
		}
21
80
22
/**
81
		double ua = numA / den;
23
 * Determines whether the two line segments formed by the given coordinates intersect.  If
82
		double ub = numB / den;
24
 * one of the two line segments starts or ends on the other line, then they are considered
25
 * to be intersecting.
26
 * 
27
 * @param ux x coordinate of starting point of line 1
28
 * @param uy y coordinate of starting point of line 1
29
 * @param vx x coordinate of ending point of line 1
30
 * @param vy y coordinate of endpoing point of line 1
31
 * @param sx x coordinate of the starting point of line 2
32
 * @param sy y coordinate of the starting point of line 2
33
 * @param tx x coordinate of the ending point of line 2
34
 * @param ty y coordinate of the ending point of line 2
35
 * @return <code>true</code> if the two line segments formed by the given coordinates 
36
 *         cross
37
 * @since 3.1
38
 */
39
public static boolean linesIntersect(int ux, int uy, int vx, int vy, 
40
		int sx, int sy, int tx, int ty) {
41
	/*
42
	 * Given the segments: u-------v. s-------t. If s->t is inside the triangle u-v-s, 
43
	 * then check whether the line u->u splits the line s->t.
44
	 */
45
	/* Values are casted to long to avoid integer overflows */
46
	long usX = (long) ux - sx;
47
	long usY = (long) uy - sy;
48
	long vsX = (long) vx - sx;
49
	long vsY = (long) vy - sy;
50
	long stX = (long) sx - tx;
51
	long stY = (long) sy - ty;
52
	if (productSign(cross(vsX, vsY, stX, stY), cross(stX, stY, usX, usY)) >= 0) {
53
		long vuX = (long) vx - ux;
54
		long vuY = (long) vy - uy;
55
		long utX = (long) ux - tx;
56
		long utY = (long) uy - ty;
57
		return productSign(cross(-usX, -usY, vuX, vuY), cross(vuX, vuY, utX, utY)) <= 0;
58
	}
59
	return false;
60
}
61
62
private static int productSign(long x, long y) {
63
	if (x == 0 || y == 0) {
64
		return 0;
65
	} else if (x < 0 ^ y < 0) {
66
		return -1;
67
	}
68
	return 1;
69
}
70
71
private static long cross(long x1, long y1, long x2, long y2) {
72
	return x1 * y2 - x2 * y1;
73
}
74
83
84
		if (ua >= 0.0 && ua <= 1.0 && ub >= 0.0 && ub <= 1.0) {
85
			// intersecting
86
			return true;
87
		}
88
		// non intersecting
89
		return false;
90
	}
91
	
75
	/**
92
	/**
76
	 * @see PointList#polylineContainsPoint(int, int, int)
93
	 * @see PointList#polylineContainsPoint(int, int, int)
77
	 * @since 3.5
94
	 * @since 3.5

Return to bug 162082