-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathLineRect.pde
More file actions
107 lines (79 loc) · 2.67 KB
/
LineRect.pde
File metadata and controls
107 lines (79 loc) · 2.67 KB
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
/*
LINE/RECTANGLE
Jeff Thompson | 2015 | www.jeffreythompson.org
*/
float x1 = 0; // points for line (controlled by mouse)
float y1 = 0;
float x2 = 20; // static point
float y2 = 20;
float sx = 200; // square position
float sy = 100;
float sw = 200; // and size
float sh = 200;
void setup() {
size(600, 400);
strokeWeight(5); // make the line easier to see
}
void draw() {
background(255);
// set end of line to mouse coordinates
x1 = mouseX;
y1 = mouseY;
// check if line has hit the square
// if so, change the fill color
boolean hit = lineRect(x1,y1,x2,y2, sx,sy,sw,sh);
if (hit) fill(255,150,0);
else fill(0,150,255);
noStroke();
rect(sx, sy, sw, sh);
// draw the line
stroke(0, 150);
line(x1, y1, x2, y2);
}
// LINE/RECTANGLE
boolean lineRect(float x1, float y1, float x2, float y2, float rx, float ry, float rw, float rh) {
// is either end INSIDE the rectangle?
// if so, return true immediately
boolean inside1 = pointRect(x1,y1, rx,ry,rw,rh);
boolean inside2 = pointRect(x2,y2, rx,ry,rw,rh);
if (inside1 || inside2) return true;
// check if the line has hit any of the rectangle's sides
// uses the Line/Line function below
boolean left = lineLine(x1,y1,x2,y2, rx,ry,rx, ry+rh);
boolean right = lineLine(x1,y1,x2,y2, rx+rw,ry, rx+rw,ry+rh);
boolean top = lineLine(x1,y1,x2,y2, rx,ry, rx+rw,ry);
boolean bottom = lineLine(x1,y1,x2,y2, rx,ry+rh, rx+rw,ry+rh);
// if ANY of the above are true, the line has hit the rectangle
if (left || right || top || bottom) {
return true;
}
return false;
}
// POINT/RECTANGLE
boolean pointRect(float px, float py, float rx, float ry, float rw, float rh) {
// is the point inside the rectangle's bounds?
if (px >= rx && // right of the left edge AND
px <= rx + rw && // left of the right edge AND
py >= ry && // below the top AND
py <= ry + rh) { // above the bottom
return true;
}
return false;
}
// LINE/LINE
boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) {
// calculate the direction of the lines
float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
// if uA and uB are between 0-1, lines are colliding
if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {
// optionally, draw a circle where the lines meet
float intersectionX = x1 + (uA * (x2-x1));
float intersectionY = y1 + (uA * (y2-y1));
fill(255,0,0);
noStroke();
ellipse(intersectionX, intersectionY, 20, 20);
return true;
}
return false;
}