判断直线与矩形相交

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
public class Rectangle {
public static final int OUT_LEFT = 1;

public static final int OUT_TOP = 2;

public static final int OUT_RIGHT = 4;

public static final int OUT_BOTTOM = 8;
private float width;
private float height;
private float left;
private float top;

public Rectangle(float x1, float y1, float w, float h) {
this.left = x1;
this.top = y1;
width = w;
height = h;
}

public boolean intersectsLine(float x1, float y1, float x2, float y2) {
int out1, out2;
if ((out2 = outcode(x2, y2)) == 0) {
return true;
}
while ((out1 = outcode(x1, y1)) != 0) {
if ((out1 & out2) != 0) {
return false;
}
if ((out1 & (OUT_LEFT | OUT_RIGHT)) != 0) {
float x = left;
if ((out1 & OUT_RIGHT) != 0) {
x += width;
}
y1 = y1 + (x - x1) * (y2 - y1) / (x2 - x1);
x1 = x;
} else {
float y = top;
if ((out1 & OUT_BOTTOM) != 0) {
y += height;
}
x1 = x1 + (y - y1) * (x2 - x1) / (y2 - y1);
y1 = y;
}
}
return true;
}

public int outcode(double x, double y) {
int out = 0;
if (this.width <= 0) {
out |= OUT_LEFT | OUT_RIGHT;
} else if (x < this.left) {
out |= OUT_LEFT;
} else if (x > this.left + this.width) {
out |= OUT_RIGHT;
}
if (this.height <= 0) {
out |= OUT_TOP | OUT_BOTTOM;
} else if (y < this.top) {
out |= OUT_TOP;
} else if (y > this.top + this.height) {
out |= OUT_BOTTOM;
}
return out;
}
}