-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLineofSight.pde
152 lines (122 loc) · 4.18 KB
/
LineofSight.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// This Script visualizes a 2-dimensional point light's area of covereage within a landscape of opaque wall segments.
// The primary output is a complex polygon that simulates the "line of sight" from the point light.
// Shadows are represented by areas not covered by the complex polygon.
// Ira Winder, [email protected], January 2017.
Walls map;
Light src;
Bug[] bugs;
// Press 'd' to show debug visualization
boolean debug = false;
boolean useMouse = true;
// Width of margin, in pixels
int margin;
String systemOS = System.getProperty("os.name").substring(0,3);
void setup() {
size(450, 550);
margin = 0;
background(0);
// Constructs Opaque Walls in two dimensional space
map = new Walls();
buildTheWalls(map, margin);
// Initializes the light location in the center of the canvas
src = new Light();
src.setLocation(0.5*width, 0.5*height);
src.shineLight(map);
// Initializes "bugs" that are sensitive to light
int numBugs = 30;
bugs = new Bug[numBugs];
for (int i=0; i<numBugs; i++) {
bugs[i] = new Bug(random(margin, width - margin), random(margin, height - margin));
}
initUDP();
// noLoop();
}
void draw() {
background(0);
stroke(#FFFFFF);
fill(#FFFFFF);
text("2D Visibility Alorithm (sort of works!), Ira Winder, [email protected]", 10, 20);
text("Move mouse within red square. Press 'd' for debug visualization.", 10, height - 20);
if (useMouse) {
src.setLocation(mouseX, mouseY);
} else {
int center = int( 0.5*(width - 2*margin)/inputUMax );
for (int u=0; u<displayU/4; u++) {
for (int v=0; v<displayV/4; v++) {
if (tablePieceInput[u][v][0] > -1) {
src.setLocation(
margin + (4.0*(displayU/4 - u)/displayU)*(width - 2*margin) - center,
margin + (4.0*v/displayV)*(height - 2*margin) + center);
}
}
}
}
src.shineLight(map);
for (Bug bug : bugs) {
bug.update(src.lightPolygon.pointInPolygon(bug.loc.x, bug.loc.y), margin, width - margin, margin, height - margin, src.location);
}
// Draw Walls
map.display();
// Draw Point Light with Area of Sight
src.display();
// Draw Bugs
for (Bug bug : bugs) {
bug.display();
}
// Draw Margin
stroke(#FF0000);
strokeWeight(5);
noFill();
rect(margin, margin, width - 2*margin, height - 2*margin);
strokeWeight(1);
// noLoop();
// Exports table Graphic to Projector
projector = get(margin, margin, width - 2*margin, height-2*margin);
// In Lieu of Projection creates the square table on main canvas for testing when on mac
if (systemOS.equals("Mac") && testProjectorOnMac) {
background(0);
image(projector, 0, 0);
}
}
// A demonstration of wall configuration
void buildTheWalls(Walls w, int border) {
// Cursor needs to be inside of enclosed space like this for algorithm to work
w.addWall(new PVector(border, border), new PVector(width-border, border));
w.addWall(new PVector(width-border, border), new PVector(width-border, height-border));
w.addWall(new PVector(width-border, height-border), new PVector(border, height-border));
w.addWall(new PVector(border, height-border), new PVector(border, border));
// Diagonal Walls
w.addWall(new PVector(100, 300), new PVector(300, 100));
w.addWall(new PVector(200, 300), new PVector(400, 200));
// // Random Horizontal Walls
// for (int i=0; i<50; i++) {
// float xRand = random(border, width-border - 15);
// float yRand = random(border, height-border - 15);
// w.addWall( new PVector(xRand, yRand), new PVector(xRand + 15, yRand) );
// }
//
// // Random Vertical Walls
// for (int i=0; i<50; i++) {
// float xRand = random(border, width-border - 15);
// float yRand = random(border, height-border - 15);
// w.addWall( new PVector(xRand, yRand), new PVector(xRand, yRand + 15) );
// }
// Dotted Horizonal Walls
for (int i=1; i<6; i++) {
w.addWall( new PVector(border + 60*i,350), new PVector(border + 60*i + 30, 350) );
}
}
//void mouseMoved() {
// loop();
//}
void keyPressed() {
switch(key) {
case 'd': // Change horizontal 'slice' layer
debug = !debug;
loop();
break;
case '`': // "Enable Projection (`)" // 21
toggle2DProjection();
break;
}
}