Lines 1-5
Link Here
|
1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2005 IBM Corporation and others. |
2 |
* Copyright (c) 2005, 2008 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
Lines 41-66
Link Here
|
41 |
* Given the segments: u-------v. s-------t. If s->t is inside the triangle u-v-s, |
41 |
* Given the segments: u-------v. s-------t. If s->t is inside the triangle u-v-s, |
42 |
* then check whether the line u->u splits the line s->t. |
42 |
* then check whether the line u->u splits the line s->t. |
43 |
*/ |
43 |
*/ |
44 |
int usX = ux - sx; |
44 |
/* Values are casted to long to avoid integer overflows */ |
45 |
int usY = uy - sy; |
45 |
long usX = (long) ux - sx; |
46 |
int vsX = vx - sx; |
46 |
long usY = (long) uy - sy; |
47 |
int vsY = vy - sy; |
47 |
long vsX = (long) vx - sx; |
48 |
int stX = sx - tx; |
48 |
long vsY = (long) vy - sy; |
49 |
int stY = sy - ty; |
49 |
long stX = (long) sx - tx; |
50 |
long product = cross(vsX, vsY, stX, stY) * cross(stX, stY, usX, usY); |
50 |
long stY = (long) sy - ty; |
51 |
if (product >= 0) { |
51 |
if (productSign(cross(vsX, vsY, stX, stY), cross(stX, stY, usX, usY)) >= 0) { |
52 |
int vuX = vx - ux; |
52 |
long vuX = (long) vx - ux; |
53 |
int vuY = vy - uy; |
53 |
long vuY = (long) vy - uy; |
54 |
int utX = ux - tx; |
54 |
long utX = (long) ux - tx; |
55 |
int utY = uy - ty; |
55 |
long utY = (long) uy - ty; |
56 |
product = cross(-usX, -usY, vuX, vuY) * cross(vuX, vuY, utX, utY); |
56 |
return productSign(cross(-usX, -usY, vuX, vuY), cross(vuX, vuY, utX, utY)) <= 0; |
57 |
boolean intersects = product <= 0; |
|
|
58 |
return intersects; |
59 |
} |
57 |
} |
60 |
return false; |
58 |
return false; |
61 |
} |
59 |
} |
62 |
|
60 |
|
63 |
private static long cross(int x1, int y1, int x2, int y2) { |
61 |
private static int productSign(long x, long y) { |
|
|
62 |
if (x == 0 || y == 0) { |
63 |
return 0; |
64 |
} else if (x < 0 ^ y < 0) { |
65 |
return -1; |
66 |
} |
67 |
return 1; |
68 |
} |
69 |
|
70 |
private static long cross(long x1, long y1, long x2, long y2) { |
64 |
return x1 * y2 - x2 * y1; |
71 |
return x1 * y2 - x2 * y1; |
65 |
} |
72 |
} |
66 |
|
73 |
|