-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
443 lines (362 loc) · 11.5 KB
/
main.js
File metadata and controls
443 lines (362 loc) · 11.5 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
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
var w = window;
var d = document;
var omp, mp, msdown;
// cline
class Line {
/*
* Create line from O, to D
*/
constructor(o, d, mv = true, dbg = false) {
this.origin = o, this.dir = d;
this.slope = 0.0;
this.dbg = dbg;
this.is_movable = mv;
this.lockedb = this.lockede = this.lockedc = false;
this.mid = new Vec2(0, 0), this.off = new Vec2(0, 0);
this.far = this.last_c = this.last_e = this.last_b = new Vec2(0, 0);
this.need_update = true;
this.generate_points();
}
generate_points(q) {
/* Calculate slope */
this.slope = (this.dir.y - this.origin.y) /
(this.dir.x - this.origin.x);
if(this.slope == Infinity) this.slope = -1000;
if(this.slope == -Infinity) this.slope = 1000;
/* Calculate far points */
if(this.dir.x > this.origin.x) {
this.far.x = this.origin.x + 1600;
this.far.y = this.origin.y + this.slope * 1600;
}
else {
this.far.x = this.origin.x - 1600;
this.far.y = this.origin.y - this.slope * 1600;
}
/* Midpoint, between O and D */
this.mid = new Vec2( (this.origin.x + this.dir.x) / 2.0,
(this.origin.y + this.dir.y) / 2.0 );
}
// Handle input
draw_handles() {
if(!this.is_movable)
return;
if(!w.msdown)
this.lockedb = this.lockede = this.lockedc = false;
if(this.lockedc) {
this.origin.add(this.off);
this.dir.add(this.off);
this.off = new Vec2(0, 0);
this.generate_points();
}
let begin_p = new Path2D(),
end_p = new Path2D(),
center_p = new Path2D();
let origin = this.origin.copy();
let dir = this.dir.copy();
let ps = 2.5;
begin_p.rect(origin.x - ps, origin.y - ps, ps*2, ps*2);
end_p.rect(dir.x - ps, dir.y - ps, ps*2, ps*2);
center_p.rect((this.mid.x + this.off.x) - ps,
(this.mid.y + this.off.y) - ps, ps*2, ps*2);
let ms_on_b = ctx.isPointInPath(begin_p, Math.trunc(omp.x), Math.trunc(omp.y));
let ms_on_e = ctx.isPointInPath(end_p, Math.trunc(omp.x), Math.trunc(omp.y));
let ms_on_c = ctx.isPointInPath(center_p,Math.trunc(omp.x), Math.trunc(omp.y));
let center_on = this.as_dir().length() > 25;
if(this.lockedb && this.lockede)
this.lockedb = true, this.lockede = false;
if( (ms_on_b && w.msdown) || this.lockedb ) {
this.origin = new Vec2(mp.x, mp.y);
this.need_update = !this.origin.cmp(this.last_b);;
this.last_b = this.origin.copy();
this.lockedb = true;
this.generate_points(1);
}
if( (ms_on_e && w.msdown) || this.lockede ) {
this.dir = new Vec2(mp.x, mp.y);
this.lockede = true;
this.need_update = !this.dir.cmp(this.last_e);
this.last_e = this.dir.copy();
this.generate_points(1);
}
if( (ms_on_c && w.msdown && center_on) || this.lockedc ) {
this.off = new Vec2(mp.x - this.mid.x,
mp.y - this.mid.y);
this.need_update = !this.last_c.cmp(this.off);
this.lockedc = true;
this.last_c = this.off.copy();
}
ctx.fillStyle = 'blue', ctx.fill(begin_p);
ctx.fillStyle = 'red', ctx.fill(end_p);
ctx.fillStyle = 'green';
if(center_on)
ctx.fill(center_p);
}
as_dir() {
return this.dir.copy().sub(this.origin);
}
get_normal(p, t) {
let nl = this.as_dir().normalize();
if(t == 1) {
nl.mul(25);
return new Vec2(-nl.y+p.x, nl.x+p.y);
}
else
return new Vec2(-nl.y, nl.x);
}
draw_normal(p) {
let normal = this.get_normal(p, 1);
ctx.beginPath();
ctx.strokeStyle = 'red';
ctx.moveTo(p.x, p.y);
ctx.lineTo(normal.x, normal.y);
ctx.stroke();
ctx.closePath();
}
draw() {
ctx.save();
ctx.translate(cv.width * 0.5, cv.height * 0.5);
ctx.scale(1, -1);
ctx.beginPath();
this.draw_handles();
ctx.strokeStyle = 'yellow';
ctx.moveTo(this.origin.x + this.off.x, this.origin.y + this.off.y);
ctx.lineTo(this.dir.x + this.off.x, this.dir.y + this.off.y);
ctx.stroke();
ctx.closePath();
ctx.restore();
}
};
// cray
class Ray extends Line {
constructor(pos, dir, mv = true, dbg = false) {
super(pos, dir, mv, dbg);
this.trace = [];
}
cast_ray() {
let r = this;
let is = null;
let rid =0;
let i = 0;
let ref = null;
let cutoff = false;
this.trace = [];
while( (is = get_closest_intersection(r, rid)) != null ){
if(!i) {
this.trace.push(new Ray(
new Vec2(r.origin.x+r.off.x, r.origin.y+r.off.y),
new Vec2(is.p.x, is.p.y)
));
i++;
continue;
}
else {
// Not first
if(cutoff) {
this.trace.push(new Ray(
new Vec2(r.origin.x+r.off.x, r.origin.y+r.off.y),
new Vec2(is.p.x, is.p.y)
));
}
rid = is.m.id;
ref = reflect(r, is);
r = new Ray(new Vec2(is.p.x, is.p.y),
new Vec2(Math.round(ref.x), Math.round(ref.y) ), false, true);
cutoff = true;
}
}
if(ref != null) {
this.trace.push(new Ray(
new Vec2(r.origin.x, r.origin.y),
new Vec2(r.far.x, r.far.y)));
}
}
draw_trace() {
ctx.strokeStyle = 'yellow';
// Not reflected
if(!this.trace.length) {
ctx.moveTo(this.origin.x+this.off.x, this.origin.y+this.off.y);
ctx.lineTo(this.far.x, this.far.y);
ctx.stroke();
return;
}
// Reflected
// So, draw trace
for(let i=0;i<this.trace.length;++i) {
let r = this.trace[i];
ctx.beginPath();
ctx.moveTo(r.origin.x, r.origin.y);
ctx.lineTo(r.dir.x, r.dir.y);
ctx.stroke();
ctx.closePath();
}
}
/*
* Draw line from beginning to end,
* and from end to far points
*/
draw(i) {
ctx.save();
ctx.translate(cv.width * 0.5, cv.height * 0.5);
ctx.scale(1, -1);
ctx.beginPath();
super.draw_handles();
ctx.beginPath();
ctx.strokeStyle = 'red';
// If need update (ray moved)
// or if any mirror moved (then recalculate all rays)
// it is quite unoptimal, but we can use it here
if(this.need_update || any_mirror_moved()) {
this.cast_ray();
this.need_update = false;
// Is last ray?
if(i == ray_total_count()) {
mirror_updated();
}
}
this.draw_trace();
ctx.closePath();
ctx.restore();
}
};
// cbeam
class Beam extends Line {
constructor(a, b, density) {
super(a, b);
this.brays = [];
this.density = density ;
this.init_rays(density);
}
init_rays(n) {
let x1 = this.origin.x, y1 = this.origin.y;
let x2 = this.dir.x, y2 = this.dir.y;
let dx = (x2 - x1) / n, dy = (y2 - y1) / n;
for(let i=1;i<n;++i) {
let v = new Vec2(x1+i*dx, y1+i*dy);
let e = this.get_normal(v, 1);
this.brays.push(new Ray(v, e, false));
}
this.brays.push(new Ray(this.origin, this.get_normal(this.origin, 1), false) );
this.brays.push(new Ray(this.dir, this.get_normal(this.dir, 1), false) );
}
draw() {
ctx.save();
ctx.translate(cv.width * 0.5, cv.height * 0.5);
ctx.scale(1, -1);
this.draw_handles();
ctx.beginPath();
ctx.strokeStyle = 'violet';
ctx.moveTo(this.origin.x, this.origin.y);
ctx.lineTo(this.dir.x, this.dir.y);
ctx.stroke();
ctx.closePath();
if(this.need_update) {
this.brays = [];
this.init_rays(this.density);
this.need_update = false;
}
ctx.restore();
}
};
// cmirr
class Mirror extends Line {
constructor(pos, dir, id) {
super(pos, dir);
if(id)
this.id = id;
else {
this.id = get_highest_id();
}
}
draw() {
ctx.save();
ctx.translate(cv.width * 0.5, cv.height * 0.5);
ctx.scale(1, -1);
ctx.beginPath();
ctx.strokeStyle = 'silver';
ctx.moveTo(this.origin.x + this.off.x, this.origin.y + this.off.y);
ctx.lineTo(this.dir.x + this.off.x, this.dir.y + this.off.y);
ctx.stroke();
super.draw_handles();
ctx.closePath();
ctx.restore();
}
};
// clens
class Lens extends Line {
constructor(a, b) {
super(a, b);
}
draw() {
ctx.save();
ctx.translate(cv.width * 0.5, cv.height * 0.5);
ctx.scale(1, -1);
ctx.beginPath();
ctx.strokeStyle = 'red';
ctx.moveTo(this.origin.x + this.off.x, this.origin.y + this.off.y);
ctx.lineTo(this.dir.x + this.off.x, this.dir.y + this.off.y);
ctx.stroke();
super.draw_handles();
ctx.closePath();
ctx.restore();
}
};
function any_mirror_moved() {
for(let i=0;i<w.mirrors.length;++i) {
if(w.mirrors[i].need_update) {
//console.log("MMVO");
return true;
}
}
return false;
}
function mirror_updated() {
for(let i=0;i<w.mirrors.length;++i) {
w.mirrors[i].need_update = false;
}
}
function get_highest_id() {
let highest = (w.mirrors.length)?w.mirrors[0].id:0;
for(let i=0;i<w.mirrors.length;++i) {
if(w.mirrors[i].id > highest)
highest = w.mirrors[i].id;
}
return highest + 1;
}
function get_closest_intersection(r, rid = 0) {
let ists = [], istx = [];
for(let i=0;i<w.mirrors.length;++i) {
let m = w.mirrors[i];
if(m.id == rid)
continue;
let is = inter(r, m);
if(is) ists.push(is), istx.push(is.param);
}
let closest = Math.min.apply(Math, istx);
for(let i=0;i<ists.length;++i) {
if(ists[i].param == closest)
return ists[i];
}
return null;
}
/*
* Return count of all rays (single rays + beam rays)
*/
function ray_total_count() {
let s = w.rays.length;;
for(let i=0;i<w.beams.length;++i)
s += w.beams[i].brays.length;
return s;
}
/*
* Just generate reflection point
*/
function reflect(r, it) {
let d = r.as_dir();
let n = it.m.get_normal(it.p, 0);
let ddn = d.dot(n);
let dx = d.x - 2*ddn*n.x;
let dy = d.y - 2*ddn*n.y;
let a1 = (dy - (it.p.y+r.off.y)) /
(dx - (it.p.x+r.off.x));
return new Vec2(1024*dx+it.p.x, 1024*dy+it.p.y);
}