-
Notifications
You must be signed in to change notification settings - Fork 0
/
Caster.pde
138 lines (114 loc) · 3.82 KB
/
Caster.pde
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.util.*;
class Caster {
PVector position;
int radius = 10;
int rayCollor = 150;
color rayBackGroundCollor;
color rayThickness = 1;
class PointAngle {
PVector point;
float angle;
PointAngle(PVector point, float angle) {
this.point = point;
this.angle = angle;
}
float getAngle() {
return this.angle;
}
}
Caster(PVector position, color rayBackGroundCollor) {
this.position = position;
this.rayCollor = 150;
this.rayBackGroundCollor = rayBackGroundCollor;
}
Caster(
PVector position,
int rayCollor,
color rayBackGroundCollor,
color rayThickness
) {
this.position = position;
this.rayCollor = rayCollor;
this.rayBackGroundCollor = rayBackGroundCollor;
this.rayThickness = rayThickness;
}
Caster(PVector position) {
this.position = position;
this.rayBackGroundCollor = color(#454545);
}
void castRays(ArrayList<LineBoundary> lines) {
ArrayList<Ray> rays = this.makeRays(lines);
ArrayList<PointAngle> foundIntersections = findIntersections(rays, lines);
foundIntersections.sort(Comparator.comparing(PointAngle::getAngle));
strokeWeight(this.rayThickness);
stroke(this.rayCollor);
foundIntersections.forEach((pa) -> line(this.position.x, this.position.y, pa.point.x, pa.point.y));
}
void castBackground(ArrayList<LineBoundary> lines, color collor) {
ArrayList<Ray> rays = this.makeRays(lines);
ArrayList<PointAngle> foundIntersections = findIntersections(rays, lines);
foundIntersections.sort(Comparator.comparing(PointAngle::getAngle));
noStroke();
fill(collor);
beginShape();
foundIntersections.forEach((pa) -> vertex(pa.point.x, pa.point.y));
endShape();
}
void castBackground(ArrayList<LineBoundary> lines) {
ArrayList<Ray> rays = this.makeRays(lines);
ArrayList<PointAngle> foundIntersections = findIntersections(rays, lines);
foundIntersections.sort(Comparator.comparing(PointAngle::getAngle));
noStroke();
fill(this.rayBackGroundCollor);
beginShape();
foundIntersections.forEach((pa) -> vertex(pa.point.x, pa.point.y));
endShape();
}
void castPoint() {
stroke(255, 0, 0);
strokeWeight(10);
point(this.position.x, this.position.y);
}
private ArrayList<PointAngle> findIntersections(ArrayList<Ray> rays, ArrayList<LineBoundary> lines) {
ArrayList<PointAngle> foundIntersections = new ArrayList<PointAngle>();
for (int i = 0; i < rays.size(); i += 1) {
PVector closest = null;
double min = Double.POSITIVE_INFINITY;
for (int j = 0; j < lines.size(); j += 1) {
PVector intersection = rays.get(i).cast(lines.get(j));
if (intersection != null) {
float distance = this.position.dist(intersection);
if (distance < min) {
min = distance;
closest = intersection;
}
}
}
if (closest != null) {
foundIntersections.add(new PointAngle(closest, rays.get(i).angle));
}
}
return foundIntersections;
}
private ArrayList<Ray> makeRays(ArrayList<LineBoundary> lines) {
ArrayList<Ray> rays = new ArrayList<Ray>();
for (int i = 0; i < lines.size(); i += 1) {
LineBoundary line = lines.get(i);
ArrayList<Float> angles = new ArrayList<Float>();
Float displacement = 0.001;
float a = PVector.sub(line.pt1, this.position).normalize().heading();
angles.add(a);
angles.add(a + displacement);
angles.add(a - displacement);
float b = PVector.sub(line.pt2, this.position).normalize().heading();
angles.add(b);
angles.add(b + displacement);
angles.add(b - displacement);
angles.forEach((angle) -> rays.add(new Ray(this.position, angle)));
}
return rays;
}
void setPosition(float x, float y) {
this.position.set(x, y);
}
}