-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathray.pde
More file actions
59 lines (47 loc) · 1.33 KB
/
ray.pde
File metadata and controls
59 lines (47 loc) · 1.33 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
class Ray {
float angle;
PVector dir;
PVector itr = new PVector(0, 0);
Ray(float angle) {
this.angle = angle*PI/180;
this.dir = PVector.fromAngle(this.angle).mult(dist(0, 0, width, height));
}
void show() {
push();
stroke(255, 255, 255, 2);
line( mouseX, mouseY, mouseX +this.dir.x, mouseY+this.dir.y);
pop();
}
float inter(Wall other) {
//this.show();
float x1 = other.start.x;
float x2 = other.end.x;
float x3 = mouseX;
float x4 = mouseX +this.dir.x;
float y1 = other.start.y;
float y2 = other.end.y;
float y3 = mouseY;
float y4 = mouseY+this.dir.y;
float div = (x1-x2)*(y3-y4)-(y1-y2)*(x3-x4);
float tu_div =(x1-x2)*(y3-y4)-(y1-y2)*(x3-x4);
if (tu_div !=0) {
float t = ((x1-x3)*(y3-y4)-(y1-y3)*(x3-x4))/tu_div;
float u = -((x1-x2)*(y1-y3)-(y1-y2)*(x1-x3))/tu_div;
if (1>t && t>0 && u>0) {
this.itr.x = ((x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4))/div;
this.itr.y = ((x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4))/div;
//this.display(this.itr.x, this.itr.y);
}
}
return dist(mouseX, mouseY, this.itr.x, this.itr.y);
}
void display(float x, float y) {
push();
stroke(255);
fill(255);
strokeWeight(3);
line(mouseX, mouseY, x, y);
//ellipse(x, y, 5, 5);
pop();
}
}