forked from irawinder/LineofSight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshadowsnew.pde
489 lines (379 loc) · 13.4 KB
/
shadowsnew.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
Walls map;
PVector light;
ArrayList<ArrayList<PVector>> Buildings;
ArrayList<ArrayList<PVector>> LOS;
ArrayList<PVector> shadows;
int border = 30;
void setup() {
fill(255);
size(500, 500);
Buildings = new ArrayList<ArrayList<PVector>>();
ArrayList<PVector> b1 = new ArrayList<PVector> ();
b1.add(new PVector (150.0, 160.0));
b1.add(new PVector (70.0, 140.0));
b1.add(new PVector (120.0, 200.0));
Buildings.add(b1);
map = new Walls(Buildings);
map.addWall(new PVector(100, 300), new PVector(300, 350));
map.addWall(new PVector (border, border), new PVector(width - border, border));
map.addWall(new PVector (width - border, border), new PVector (width - border, height - border));
map.addWall(new PVector (width - border, height - border), new PVector(border, height - border));
map.addWall(new PVector (border, height - border), new PVector(border, border));
light = new PVector (0.75 * 500, 0.25 * 500);
LOS = map.sweep(light);
shadows = map.getShadows(LOS, map.endPoints, light);
background(0);
map.display();
fill(#FFFF00);
noStroke();
ellipse (light.x, light.y, 10, 10);
noLoop();
}
void draw() {
background(0);
light.x = mouseX;
light.y = mouseY;
LOS = map.sweep(light);
shadows = map.getShadows(LOS, map.endPoints, light);
beginShape();
fill(#FFFF00, 50); //display points in LOS and Shadows
for (int i = 0; i < LOS.size(); i ++) {
for (int j = 0; j < LOS.get(i).size(); j++) {
fill (#2F9F9F);
ellipse (LOS.get(i).get(j).x, LOS.get(i).get(j).y, 10, 10);
stroke (#2F3FA7);
line(light.x, light.y, LOS.get(i).get(j).x, LOS.get(i).get(j).y);
}
}
for (int j = 0; j < shadows.size(); j ++) {
fill(#CB329F);
ellipse(shadows.get(j).x, shadows.get(j).y, 10, 10);
}
endShape();
map.display();
ellipse(light.x, light.y, 10, 10);
//LOS
fill(#FFFF00);
noStroke();
ellipse (light.x, light.y, 10, 10);
noLoop();
}
void mouseMoved() {
loop();
}
class Walls {
ArrayList<Wall> walls;
ArrayList<Endpoint> endPoints, sortedEndPoints;
ArrayList<Integer> indices;
int counter = 0;
Walls(ArrayList<ArrayList<PVector>> buildings) {
walls = new ArrayList<Wall>();
endPoints = new ArrayList<Endpoint>();
sortedEndPoints = new ArrayList<Endpoint>();
converttowalls(buildings);
}
//converts buildings to walls
void converttowalls (ArrayList<ArrayList<PVector>> buildings) {
for (ArrayList<PVector> bld: buildings) {
for (int index = 0; index < bld.size(); index ++) {
if (index == (bld.size() - 1)) {
addWall(bld.get(index), bld.get(0));
} else {
addWall(bld.get(index),bld.get(index + 1));
}
}
}
}
void addWall(PVector begin, PVector end) {
walls.add(new Wall(begin, end, counter));
addEndPoint(begin, counter);
addEndPoint(end, counter);
counter ++;
}
void addEndPoint(PVector point, int index) {
boolean duplicate = false;
for (int i = 0; i < endPoints.size(); i++) {
if (point.x == endPoints.get(i).location.x && point.y == endPoints.get(i).location.y) {
duplicate = true;
endPoints.get(i).addIndex(index);
break;
}
}
if (!duplicate) {
endPoints.add(new Endpoint(point, index));
}
}
void display() {
for (int i = 0; i < walls.size(); i ++) {
walls.get(i).display();
}
for (int i = 0; i < endPoints.size(); i ++) {
endPoints.get(i).display();
}
}
//obtains endpoints on screen
ArrayList<ArrayList<PVector>> sweep (PVector source) {
ArrayList<ArrayList<PVector>> LineOfSight = new ArrayList<ArrayList<PVector>>();
//Calculate End Point angles from source
for (int i = 0; i < endPoints.size(); i ++) {
endPoints.get(i).sourceangle(source);
}
//calculate closest distance of each wall from source
for (int i = 0; i < walls.size(); i ++) {
walls.get(i).sourceClosestDistance(source);
}
//Sorts End Points by angle from source
ArrayList<Endpoint> sortedEndPoints = new ArrayList<Endpoint>();
sortedEndPoints.clear();
sortedEndPoints.add(endPoints.get(0));
boolean sorted;
for (int i = 1; i < endPoints.size(); i++) {
sorted = false;
for (int j = 0; j < sortedEndPoints.size(); j ++) {
if (sortedEndPoints.get(j).angle > endPoints.get(i).angle) {
sortedEndPoints.add(j, endPoints.get(i));
sorted = true;
break;
}
}
if (!sorted) {
sortedEndPoints.add(endPoints.get(i));
}
}
//Iterates through Endpoints, finding wall intersections
ArrayList<Integer> intersectedWalls = new ArrayList<Integer>();
for (int p = 0; p < sortedEndPoints.size(); p ++){
intersectedWalls.clear();
//Adds walls already known to be associated with selected endpoints
for (int i = 0; i < sortedEndPoints.get(p).indices.size(); i ++) {
intersectedWalls.add(sortedEndPoints.get(p).indices.get(i));
walls.get(sortedEndPoints.get(p).indices.get(i)).intersect = sortedEndPoints.get(p).location;
walls.get(sortedEndPoints.get(p).indices.get(i)).sourceDistance(source, sortedEndPoints.get(p).location);
}
//looks for wall intersections that aren't already at endpoints
for (int w = 0; w < walls.size(); w ++) {
boolean duplicate = false;
for (int i = 0; i < intersectedWalls.size(); i ++) {
if (intersectedWalls.get(i) == w) {
duplicate = true;
break;
}
}
if (duplicate == false) {
if (rayLineIntersect (walls.get(w), source, sortedEndPoints.get(p))) {
intersectedWalls.add(walls.get(w).index);
}
}
}
//deletes extra points
ArrayList<Integer> toRemove = new ArrayList <Integer>();
toRemove.clear();
for (int i = 0; i < intersectedWalls.size(); i ++) {
for (int w = 0; w < walls.size(); w ++) {
if (lineIntersect(walls.get(intersectedWalls.get(i)).intersect, walls.get(w), source)) {
toRemove.add(intersectedWalls.get(i));
}
}
}
for (int i = 0; i < toRemove.size(); i ++) {
intersectedWalls.remove(toRemove.get(i));
}
ArrayList<PVector> toAddtoLOS = new ArrayList<PVector>();
toAddtoLOS.clear();
for (int w = 0; w < intersectedWalls.size(); w ++) {
toAddtoLOS.add(walls.get(intersectedWalls.get(w)).intersect);
}
LineOfSight.add(toAddtoLOS);
}
return LineOfSight;
}
boolean rayLineIntersect (Wall wall, PVector source, Endpoint point) {
float x1 = wall.begin.x;
float y1 = wall.begin.y;
float x2 = wall.end.x;
float y2 = wall.end.y;
float x3 = source.x;
float y3 = source.y;
PVector dir = new PVector (point.location.x - x3, point.location.y - y3);
dir.setMag(width + height);
float x4 = source.x + dir.x;
float y4 = source.y + dir.y;
float a1 = y2 - y1;
float b1 = x1 - x2;
float c1 = a1*x1 + b1*y1;
float a2 = y4 - y3;
float b2 = x3 - x4;
float c2 = a2*x3 + b2*y3;
float det = a1 * b2 - a2 * b1;
boolean over = false;
if (det == 0) {
//do nothing
} else {
float tolerance = 0.01;
float x = (b2 * c1 - b1 * c2)/det;
float y = (a1 * c2 - a2 * c1)/det;
if (x >= min (x1, x2) - tolerance && x <= max(x1, x2) + tolerance &&
x >= min(x3, x4) -tolerance && x <= max(x3, x4) +tolerance &&
y >= min(y1, y2) -tolerance && y <= max(y1, y2) +tolerance &&
y >= min(y3, y4) -tolerance && y <= max(y3, y4) +tolerance ){
over =true;
wall.setIntersect(new PVector (x, y));
wall.distance = sqrt(sq(x - source.x) + sq(y - source.y));
}
}
return over;
}
//helper function for sweep, determines if a line of sight crosses over a wall or a building
boolean lineIntersect(PVector intersect, Wall wall, PVector source) {
boolean over = false;
float x1 = wall.begin.x;
float y1 = wall.begin.y;
float x2 = wall.end.x;
float y2 = wall.end.y;
float x3 = source.x;
float y3 = source.y;
float x4 = intersect.x;
float y4 = intersect.y;
float a1 = y2 - y1;
float b1 = x1 - x2;
float c1 = a1 * x1 + b1 * y1;
float a2 = y4 - y3;
float b2 = x3 - x4;
float c2 = a2*x3 + b2*y3;
float det = a1*b2 - a2*b1;
if (det == 0) {
//do nothing
} else {
float x = (b2 * c1 - b1 * c2)/det;
float y = (a1 * c2 - a2 * c1)/det;
float tolerance = 0.001;
if (abs (x - wall.begin.x) > tolerance && abs (x - wall.end.x) > tolerance && abs (y - wall.begin.y) > tolerance && abs (y - wall.end.y) > tolerance && abs (x - intersect.x) > tolerance && abs (y - intersect.y) > tolerance &&
x >= min(x1, x2) +tolerance && x <= max(x1, x2) -tolerance &&
x >= min(x3, x4) +tolerance && x <= max(x3, x4) -tolerance &&
y >= min(y1, y2) +tolerance && y <= max(y1, y2) -tolerance &&
y >= min(y3, y4) +tolerance && y <= max(y3, y4) -tolerance) {
over = true;
}
}
return over;
}
//determines where the shadow lies, given Line Of Sight
ArrayList<PVector> getShadows (ArrayList<ArrayList<PVector>> LineOfSight, ArrayList<Endpoint> endPoints, PVector source) {
ArrayList<PVector> sh = new ArrayList<PVector> ();
sh.clear();
for (int i = 0; i < endPoints.size(); i ++) {
boolean dup = false;
for (int j = 0; j < LineOfSight.size(); j++) {
for (int k = 0; k < LineOfSight.get(j).size(); k++) {
if (LineOfSight.get(j).get(k).x == endPoints.get(i).location.x && LineOfSight.get(j).get(k).y == endPoints.get(i).location.y) {
dup = true;
}
}
}
if (!dup) {
sh.add(endPoints.get(i).location);
}
}
int size = LineOfSight.size();
for (int i = 0; i < LineOfSight.size(); i++) {
float maxDist = 0;
PVector farthestPoint = new PVector ();
for (int j =0; j < LineOfSight.get(i).size(); j++) {
float dist = sqrt(sq(LineOfSight.get(i).get(j).x - source.x) + sq(LineOfSight.get(i).get(j).y - source.y));
if (dist > maxDist) {
maxDist = dist;
farthestPoint = LineOfSight.get(i).get(j);
}
}
float tolerance = 0.1;
if (abs(farthestPoint.x - border) < tolerance || abs(farthestPoint.x - ( width - border)) < tolerance || abs(farthestPoint.y - border) < tolerance || abs(farthestPoint.y - (height - border)) < tolerance) {
} else {
sh.add(farthestPoint);
if (i == 0) {
for (PVector p: LineOfSight.get(size - 1)) {
sh.add(p);
}
} else {
for (PVector p: LineOfSight.get(i - 1)) {
sh.add(p);
}
}
for (PVector v: LineOfSight.get((i + 1)%size)) {
sh.add(v);
}
}
}
return sh;
}
}
class Endpoint {
PVector location;
float angle, distance;
ArrayList<Integer> indices;
Endpoint(PVector location, int index) {
this.location = new PVector();
this.location = location;
indices = new ArrayList<Integer>();
indices.add (index);
angle = 0;
}
void addIndex (int index) {
boolean duplicate = false;
for (int i = 0; i < indices.size(); i ++) {
if (index == indices.get(i)) {
duplicate = true;
break;
}
}
if (!duplicate) {
indices.add(index);
}
}
void sourceangle(PVector source) {
angle = atan( (location.x - source.x)/(location.y - source.y) );
angle += 0.5*PI;
angle = PI - angle;
if ((location.y - source.y) > 0) {
angle += PI;
}
}
void display() {
fill(#FF0000);
ellipse (light.x, light.y, 10, 10);
}
}
class Wall {
PVector begin;
PVector end;
int index;
PVector intersect;
float avgDistance, closestDistance, distance;
Wall (PVector begin, PVector end, int index) {
this.begin = begin;
this.end = end;
this.index = index;
}
void setIntersect (PVector intersect) {
this.intersect = intersect;
}
void sourceClosestDistance(PVector source) {
float deltaX = end.x - begin.x;
float deltaY = end.y - begin.y;
float num = abs(deltaY*(source.x) - deltaX*(source.y) + (end.x * begin.y) - (end.y * begin.x));
float den = sqrt (sq(deltaY) + sq(deltaX));
closestDistance = num/den;
}
void sourceAvgDistance (PVector source) {
float avgX = 0.5 * (begin.x + end.x);
float avgY = 0.5 * (begin.y + end.y);
avgDistance = sqrt(sq(avgX - source.x) + sq(avgY - source.y));
}
void sourceDistance(PVector source, PVector point) {
distance = sqrt(sq(point.x - source.x) + sq(point.y - source.y));
}
void display() {
stroke (200);
fill (200);
line (begin.x, begin.y, end.x, end.y);
}
}