forked from ricktu288/ray-optics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
5846 lines (5111 loc) · 198 KB
/
index.js
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var graphs = {
/**
* 基本圖型
**/
point: function(x, y) {return {type: 1, x: x, y: y, exist: true}},
line: function(p1, p2) {return {type: 2, p1: p1, p2: p2, exist: true}},
ray: function(p1, p2) {return {type: 3, p1: p1, p2: p2, exist: true}},
line_segment: function(p1, p2) {return {type: 4, p1: p1, p2: p2, exist: true}},
segment: function(p1, p2) {return {type: 4, p1: p1, p2: p2, exist: true}},
circle: function(c, r) {
if (typeof r == 'object' && r.type == 1) {
return {type: 5, c: c, r: this.line_segment(c, r), exist: true}
} else {
return {type: 5, c: c, r: r, exist: true}
}
},
/**
* inner product
* @method dot
* @param {graph.point} p1
* @param {graph.point} p2
* @return {Number}
**/
dot: function(p1, p2) {
return p1.x * p2.x + p1.y * p2.y;
},
/**
* outer product
* @method cross
* @param {graph.point} p1
* @param {graph.point} p2
* @return {Number}
**/
cross: function(p1, p2) {
return p1.x * p2.y - p1.y * p2.x;
},
/**
* 求交點
* @method intersection
* @param {graph} obj1
* @param {graph} obj2
* @return {graph.point}
**/
intersection: function(obj1, obj2) {
// line & line
if (obj1.type == 2 && obj2.type == 2) {
return this.intersection_2line(obj1, obj2);
}
// line & circle
else if (obj1.type == 2 && obj2.type == 5) {
return this.intersection_line_circle(obj1, obj2);
}
// circle & line
else if (obj1.type == 5 && obj2.type == 2) {
return this.intersection_line_circle(obj2, obj1);
}
},
/**
* 兩直線交點
* @method intersection_2line
* @param {graph.line} l1
* @param {graph.line} l2
* @return {graph.point}
**/
intersection_2line: function(l1, l2) {
var A = l1.p2.x * l1.p1.y - l1.p1.x * l1.p2.y;
var B = l2.p2.x * l2.p1.y - l2.p1.x * l2.p2.y;
var xa = l1.p2.x - l1.p1.x;
var xb = l2.p2.x - l2.p1.x;
var ya = l1.p2.y - l1.p1.y;
var yb = l2.p2.y - l2.p1.y;
return graphs.point((A * xb - B * xa) / (xa * yb - xb * ya), (A * yb - B * ya) / (xa * yb - xb * ya));
},
/**
* 直線與圓的交點
* @method intersection_2line
* @param {graph.line} l1
* @param {graph.circle} c2
* @return {graph.point}
**/
intersection_line_circle: function(l1, c1) {
var xa = l1.p2.x - l1.p1.x;
var ya = l1.p2.y - l1.p1.y;
var cx = c1.c.x;
var cy = c1.c.y;
var r_sq = (typeof c1.r == 'object') ? ((c1.r.p1.x - c1.r.p2.x) * (c1.r.p1.x - c1.r.p2.x) + (c1.r.p1.y - c1.r.p2.y) * (c1.r.p1.y - c1.r.p2.y)) : (c1.r * c1.r);
var l = Math.sqrt(xa * xa + ya * ya);
var ux = xa / l;
var uy = ya / l;
var cu = ((cx - l1.p1.x) * ux + (cy - l1.p1.y) * uy);
var px = l1.p1.x + cu * ux;
var py = l1.p1.y + cu * uy;
var d = Math.sqrt(r_sq - (px - cx) * (px - cx) - (py - cy) * (py - cy));
var ret = [];
ret[1] = graphs.point(px + ux * d, py + uy * d);
ret[2] = graphs.point(px - ux * d, py - uy * d);
return ret;
},
intersection_is_on_ray: function(p1, r1) {
return (p1.x - r1.p1.x) * (r1.p2.x - r1.p1.x) + (p1.y - r1.p1.y) * (r1.p2.y - r1.p1.y) >= 0;
},
intersection_is_on_segment: function(p1, s1) {
return (p1.x - s1.p1.x) * (s1.p2.x - s1.p1.x) + (p1.y - s1.p1.y) * (s1.p2.y - s1.p1.y) >= 0 && (p1.x - s1.p2.x) * (s1.p1.x - s1.p2.x) + (p1.y - s1.p2.y) * (s1.p1.y - s1.p2.y) >= 0;
},
/**
* 線段長度
* @method length_segment
* @param {graph.segment} seg
* @return {Number}
**/
length_segment: function(seg) {
return Math.sqrt(this.length_segment_squared(seg));
},
/**
* 線段長度平方
* @method length_segment_squared
* @param {graph.segment} seg
* @return {Number}
**/
length_segment_squared: function(seg) {
return this.length_squared(seg.p1, seg.p2);
},
/**
* 兩點距離
* @method length
* @param {graph.point} p1
* @param {graph.point} p2
* @return {Number}
**/
length: function(p1, p2) {
return Math.sqrt(this.length_squared(p1, p2));
},
/**
* 兩點距離平方
* @method length_squared
* @param {graph.point} p1
* @param {graph.point} p2
* @return {Number}
**/
length_squared: function(p1, p2) {
var dx = p1.x - p2.x;
var dy = p1.y - p2.y;
return dx * dx + dy * dy;
},
/*
* 基本作圖函數
*/
/**
* 線段中點
* @method midpoint
* @param {graph.line} l1
* @return {graph.point}
**/
midpoint: function(l1) {
var nx = (l1.p1.x + l1.p2.x) * 0.5;
var ny = (l1.p1.y + l1.p2.y) * 0.5;
return graphs.point(nx, ny);
},
midpoint_points: function(p1, p2) {
var nx = (p1.x + p2.x) * 0.5;
var ny = (p1.y + p2.y) * 0.5;
return graphs.point(nx, ny);
},
/**
* 線段中垂線
* @method perpendicular_bisector
* @param {graph.line} l1
* @return {graph.line}
**/
perpendicular_bisector: function(l1) {
return graphs.line(
graphs.point(
(-l1.p1.y + l1.p2.y + l1.p1.x + l1.p2.x) * 0.5,
(l1.p1.x - l1.p2.x + l1.p1.y + l1.p2.y) * 0.5
),
graphs.point(
(l1.p1.y - l1.p2.y + l1.p1.x + l1.p2.x) * 0.5,
(-l1.p1.x + l1.p2.x + l1.p1.y + l1.p2.y) * 0.5
)
);
},
/**
* 畫通過一點且與一直線平行的線
* @method parallel
* @param {graph.line} l1
* @param {graph.point} p1
* @return {graph.line}
**/
parallel: function(l1, p1) {
var dx = l1.p2.x - l1.p1.x;
var dy = l1.p2.y - l1.p1.y;
return graphs.line(p1, graphs.point(p1.x + dx, p1.y + dy));
}
};
function getMouseStyle(obj, style) {
if (obj == mouseObj && mouseObj)
return 'rgb(0,255,255)'
return style;
}
var canvasPainter = {
draw: function(graph, color) {
//var ctx = canvas.getContext('2d');
// point
if (graph.type == 1) {
ctx.fillStyle = color ? color : 'red';
ctx.fillRect(graph.x - 2, graph.y - 2, 5, 5); //繪製填滿的矩形
/*
ctx.beginPath();
ctx.arc(graph.x,graph.y,2,0,Math.PI*2,false);
ctx.fill();
*/
}
// line
else if (graph.type == 2) {
ctx.strokeStyle = color ? color : 'black';
ctx.beginPath();
var ang1 = Math.atan2((graph.p2.x - graph.p1.x), (graph.p2.y - graph.p1.y)); //從斜率取得角度
var cvsLimit = (Math.abs(graph.p1.x + origin.x) + Math.abs(graph.p1.y + origin.y) + canvas.height + canvas.width) / Math.min(1, scale); //取一個會超出繪圖區的距離(當做直線端點)
ctx.moveTo(graph.p1.x - Math.sin(ang1) * cvsLimit, graph.p1.y - Math.cos(ang1) * cvsLimit);
ctx.lineTo(graph.p1.x + Math.sin(ang1) * cvsLimit, graph.p1.y + Math.cos(ang1) * cvsLimit);
ctx.stroke();
}
// ray
else if (graph.type == 3) {
ctx.strokeStyle = color ? color : 'black';
var ang1, cvsLimit;
if (Math.abs(graph.p2.x - graph.p1.x) > 1e-5 || Math.abs(graph.p2.y - graph.p1.y) > 1e-5)
{
ctx.beginPath();
ang1 = Math.atan2((graph.p2.x - graph.p1.x), (graph.p2.y - graph.p1.y)); //從斜率取得角度
cvsLimit = (Math.abs(graph.p1.x + origin.x) + Math.abs(graph.p1.y + origin.y) + canvas.height + canvas.width) / Math.min(1, scale); //取一個會超出繪圖區的距離(當做直線端點)
ctx.moveTo(graph.p1.x, graph.p1.y);
ctx.lineTo(graph.p1.x + Math.sin(ang1) * cvsLimit, graph.p1.y + Math.cos(ang1) * cvsLimit);
ctx.stroke();
}
}
// (line_)segment
else if (graph.type == 4) {
ctx.strokeStyle = color ? color : 'black';
ctx.beginPath();
ctx.moveTo(graph.p1.x, graph.p1.y);
ctx.lineTo(graph.p2.x, graph.p2.y);
ctx.stroke();
}
// circle
else if (graph.type == 5) {
ctx.strokeStyle = color ? color : 'black';
ctx.beginPath();
if (typeof graph.r == 'object') {
var dx = graph.r.p1.x - graph.r.p2.x;
var dy = graph.r.p1.y - graph.r.p2.y;
ctx.arc(graph.c.x, graph.c.y, Math.sqrt(dx * dx + dy * dy), 0, Math.PI * 2, false);
} else {
ctx.arc(graph.c.x, graph.c.y, graph.r, 0, Math.PI * 2, false);
}
ctx.stroke();
}
},
cls: function() {
//var ctx = canvas.getContext('2d');
if (ctx.constructor != C2S) {
//only do this when not being exported to SVG to avoid bug
ctx.setTransform(1,0,0,1,0,0);
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.setTransform(scale,0,0,scale,origin.x, origin.y);
if (backgroundImage) {
ctx.drawImage(backgroundImage,0,0);
}
} else if (!backgroundImage) {
ctx.translate(origin.x / scale, origin.y / scale);
}
}
};
var objTypes = {};
//線段物件之原型
objTypes['lineobj'] = {
//==============================建立物件過程滑鼠按下=======================================
c_mousedown: function(obj, mouse)
{
obj.p2 = mouse;
if (!mouseOnPoint_construct(mouse, obj.p1))
{
draw();
}
},
//==============================建立物件過程滑鼠移動=======================================
c_mousemove: function(obj, mouse, ctrl, shift)
{
//var basePoint=ctrl?graphs.midpoint(obj):obj.p1;
//if(!obj.p2
if (shift)
{
//var sq3=Math.sqrt(3);
//obj.p2=snapToDirection(mouse,constructionPoint,[{x:1,y:0},{x:0,y:1},{x:1,y:1},{x:1,y:-1},{x:1,y:sq3},{x:1,y:-sq3},{x:sq3,y:1},{x:sq3,y:-1}]);
obj.p2 = snapToDirection(mouse, constructionPoint, [{x: 1, y: 0},{x: 0, y: 1},{x: 1, y: 1},{x: 1, y: -1}]);
}
else
{
obj.p2 = mouse;
}
obj.p1 = ctrl ? graphs.point(2 * constructionPoint.x - obj.p2.x, 2 * constructionPoint.y - obj.p2.y) : constructionPoint;
if (!mouseOnPoint_construct(mouse, obj.p1))
{
draw();
}
},
//==============================建立物件過程滑鼠放開=======================================
c_mouseup: function(obj, mouse)
{
if (!mouseOnPoint_construct(mouse, obj.p1))
{
isConstructing = false;
}
},
//=================================平移物件====================================
move: function(obj, diffX, diffY) {
//移動線段的第一點
obj.p1.x = obj.p1.x + diffX;
obj.p1.y = obj.p1.y + diffY;
//移動線段的第二點
obj.p2.x = obj.p2.x + diffX;
obj.p2.y = obj.p2.y + diffY;
},
//==========================繪圖區被按下時(判斷物件被按下的部分)===========================
clicked: function(obj, mouse_nogrid, mouse, draggingPart) {
if (mouseOnPoint(mouse_nogrid, obj.p1) && graphs.length_squared(mouse_nogrid, obj.p1) <= graphs.length_squared(mouse_nogrid, obj.p2))
{
draggingPart.part = 1;
draggingPart.targetPoint = graphs.point(obj.p1.x, obj.p1.y);
return true;
}
if (mouseOnPoint(mouse_nogrid, obj.p2))
{
draggingPart.part = 2;
draggingPart.targetPoint = graphs.point(obj.p2.x, obj.p2.y);
return true;
}
if (mouseOnSegment(mouse_nogrid, obj))
{
draggingPart.part = 0;
draggingPart.mouse0 = mouse; //開始拖曳時的滑鼠位置
draggingPart.mouse1 = mouse; //拖曳時上一點的滑鼠位置
draggingPart.snapData = {};
return true;
}
return false;
},
//=================================拖曳物件時====================================
dragging: function(obj, mouse, draggingPart, ctrl, shift) {
var basePoint;
if (draggingPart.part == 1)
{
//正在拖曳第一個端點
//Dragging the first endpoint
basePoint = ctrl ? graphs.midpoint(draggingPart.originalObj) : draggingPart.originalObj.p2;
obj.p1 = shift ? snapToDirection(mouse, basePoint, [{x: 1, y: 0},{x: 0, y: 1},{x: 1, y: 1},{x: 1, y: -1},{x: (draggingPart.originalObj.p2.x - draggingPart.originalObj.p1.x), y: (draggingPart.originalObj.p2.y - draggingPart.originalObj.p1.y)}]) : mouse;
obj.p2 = ctrl ? graphs.point(2 * basePoint.x - obj.p1.x, 2 * basePoint.y - obj.p1.y) : basePoint;
//obj.p1=mouse;
}
if (draggingPart.part == 2)
{
//正在拖曳第二個端點
//Dragging the second endpoint
basePoint = ctrl ? graphs.midpoint(draggingPart.originalObj) : draggingPart.originalObj.p1;
obj.p2 = shift ? snapToDirection(mouse, basePoint, [{x: 1, y: 0},{x: 0, y: 1},{x: 1, y: 1},{x: 1, y: -1},{x: (draggingPart.originalObj.p2.x - draggingPart.originalObj.p1.x), y: (draggingPart.originalObj.p2.y - draggingPart.originalObj.p1.y)}]) : mouse;
obj.p1 = ctrl ? graphs.point(2 * basePoint.x - obj.p2.x, 2 * basePoint.y - obj.p2.y) : basePoint;
//obj.p2=mouse;
}
if (draggingPart.part == 0)
{
//正在拖曳整條線
//Dragging the entire line
if (shift)
{
var mouse_snapped = snapToDirection(mouse, draggingPart.mouse0, [{x: 1, y: 0},{x: 0, y: 1},{x: (draggingPart.originalObj.p2.x - draggingPart.originalObj.p1.x), y: (draggingPart.originalObj.p2.y - draggingPart.originalObj.p1.y)},{x: (draggingPart.originalObj.p2.y - draggingPart.originalObj.p1.y), y: -(draggingPart.originalObj.p2.x - draggingPart.originalObj.p1.x)}], draggingPart.snapData);
}
else
{
var mouse_snapped = mouse;
draggingPart.snapData = {}; //放開shift時解除原先之拖曳方向鎖定
}
var mouseDiffX = draggingPart.mouse1.x - mouse_snapped.x; //目前滑鼠位置與上一次的滑鼠位置的X軸差
var mouseDiffY = draggingPart.mouse1.y - mouse_snapped.y; //目前滑鼠位置與上一次的滑鼠位置的Y軸差
//移動線段的第一點
obj.p1.x = obj.p1.x - mouseDiffX;
obj.p1.y = obj.p1.y - mouseDiffY;
//移動線段的第二點
obj.p2.x = obj.p2.x - mouseDiffX;
obj.p2.y = obj.p2.y - mouseDiffY;
//更新滑鼠位置
draggingPart.mouse1 = mouse_snapped;
}
},
//====================判斷一道光是否會射到此物件(若是,則回傳交點)====================
rayIntersection: function(obj, ray) {
var rp_temp = graphs.intersection_2line(graphs.line(ray.p1, ray.p2), graphs.line(obj.p1, obj.p2)); //求光(的延長線)與物件(的延長線)的交點
if (graphs.intersection_is_on_segment(rp_temp, obj) && graphs.intersection_is_on_ray(rp_temp, ray))
{
//↑若rp_temp在ray上且rp_temp在obj上(即ray真的有射到obj,不是ray的延長線射到或射到obj的延長線上)
return rp_temp; //回傳光線的頭與鏡子的交點
}
}
};
//"halfplane"(半平面折射鏡)物件
objTypes['halfplane'] = {
p_name: 'Refractive index', //屬性名稱
p_min: 1,
p_max: 3,
p_step: 0.01,
supportSurfaceMerging: true, //支援界面融合
//======================================建立物件=========================================
create: function(mouse) {
return {type: 'halfplane', p1: mouse, p2: mouse, p: 1.5};
},
//使用lineobj原型
c_mousedown: objTypes['lineobj'].c_mousedown,
c_mousemove: objTypes['lineobj'].c_mousemove,
c_mouseup: objTypes['lineobj'].c_mouseup,
move: objTypes['lineobj'].move,
//==========================繪圖區被按下時(判斷物件被按下的部分)===========================
clicked: function(obj, mouse_nogrid, mouse, draggingPart) {
if (mouseOnPoint(mouse_nogrid, obj.p1) && graphs.length_squared(mouse_nogrid, obj.p1) <= graphs.length_squared(mouse_nogrid, obj.p2))
{
draggingPart.part = 1;
draggingPart.targetPoint = graphs.point(obj.p1.x, obj.p1.y);
return true;
}
if (mouseOnPoint(mouse_nogrid, obj.p2))
{
draggingPart.part = 2;
draggingPart.targetPoint = graphs.point(obj.p2.x, obj.p2.y);
return true;
}
if (mouseOnLine(mouse_nogrid, obj))
{
draggingPart.part = 0;
draggingPart.mouse0 = mouse; //開始拖曳時的滑鼠位置
draggingPart.mouse1 = mouse; //拖曳時上一點的滑鼠位置
draggingPart.snapData = {};
return true;
}
return false;
},
//=================================拖曳物件時====================================
dragging: function(obj, mouse, draggingPart, ctrl, shift) {
var basePoint;
if (draggingPart.part == 1)
{
//正在拖曳第一個端點
basePoint = ctrl ? graphs.midpoint(draggingPart.originalObj) : draggingPart.originalObj.p2;
obj.p1 = shift ? snapToDirection(mouse, basePoint, [{x: 1, y: 0},{x: 0, y: 1},{x: 1, y: 1},{x: 1, y: -1},{x: (draggingPart.originalObj.p2.x - draggingPart.originalObj.p1.x), y: (draggingPart.originalObj.p2.y - draggingPart.originalObj.p1.y)}]) : mouse;
obj.p2 = ctrl ? graphs.point(2 * basePoint.x - obj.p2.x, 2 * basePoint.y - obj.p2.y) : basePoint;
//obj.p1=mouse;
}
if (draggingPart.part == 2)
{
//正在拖曳第二個端點
basePoint = ctrl ? graphs.midpoint(draggingPart.originalObj) : draggingPart.originalObj.p1;
obj.p2 = shift ? snapToDirection(mouse, basePoint, [{x: 1, y: 0},{x: 0, y: 1},{x: 1, y: 1},{x: 1, y: -1},{x: (draggingPart.originalObj.p2.x - draggingPart.originalObj.p1.x), y: (draggingPart.originalObj.p2.y - draggingPart.originalObj.p1.y)}]) : mouse;
obj.p1 = ctrl ? graphs.point(2 * basePoint.x - obj.p2.x, 2 * basePoint.y - obj.p2.y) : basePoint;
//obj.p2=mouse;
}
if (draggingPart.part == 0)
{
//正在拖曳整條線
if (shift)
{
var mouse_snapped = snapToDirection(mouse, draggingPart.mouse0, [{x: (draggingPart.originalObj.p2.x - draggingPart.originalObj.p1.x), y: (draggingPart.originalObj.p2.y - draggingPart.originalObj.p1.y)},{x: (draggingPart.originalObj.p2.y - draggingPart.originalObj.p1.y), y: -(draggingPart.originalObj.p2.x - draggingPart.originalObj.p1.x)}], draggingPart.snapData);
}
else
{
var mouse_snapped = mouse;
draggingPart.snapData = {}; //放開shift時解除原先之拖曳方向鎖定
}
var mouseDiffX = draggingPart.mouse1.x - mouse_snapped.x; //目前滑鼠位置與上一次的滑鼠位置的X軸差
var mouseDiffY = draggingPart.mouse1.y - mouse_snapped.y; //目前滑鼠位置與上一次的滑鼠位置的Y軸差
//移動線段的第一點
obj.p1.x = obj.p1.x - mouseDiffX;
obj.p1.y = obj.p1.y - mouseDiffY;
//移動線段的第二點
obj.p2.x = obj.p2.x - mouseDiffX;
obj.p2.y = obj.p2.y - mouseDiffY;
//更新滑鼠位置
draggingPart.mouse1 = mouse_snapped;
}
},
//====================判斷一道光是否會射到此物件(若是,則回傳交點)====================
rayIntersection: function(obj, ray) {
if (obj.p <= 0)return;
var rp_temp = graphs.intersection_2line(graphs.line(ray.p1, ray.p2), graphs.line(obj.p1, obj.p2)); //求光(的延長線)與物件的交點
if (graphs.intersection_is_on_ray(rp_temp, ray))
{
//↑若rp_temp在ray上(即ray真的有射到obj,不是ray的延長線射到)
return rp_temp; //回傳光線的頭與鏡子的交點
}
},
//=================================將物件畫到Canvas上====================================
draw: function(obj, canvas, aboveLight) {
if (!aboveLight)
{
var len = Math.sqrt((obj.p2.x - obj.p1.x) * (obj.p2.x - obj.p1.x) + (obj.p2.y - obj.p1.y) * (obj.p2.y - obj.p1.y));
var par_x = (obj.p2.x - obj.p1.x) / len;
var par_y = (obj.p2.y - obj.p1.y) / len;
var per_x = par_y;
var per_y = -par_x;
var sufficientlyLargeDistance = (Math.abs(obj.p1.x + origin.x) + Math.abs(obj.p1.y + origin.y) + canvas.height + canvas.width) / Math.min(1, scale);
ctx.beginPath();
ctx.moveTo(obj.p1.x - par_x * sufficientlyLargeDistance, obj.p1.y - par_y * sufficientlyLargeDistance);
ctx.lineTo(obj.p1.x + par_x * sufficientlyLargeDistance, obj.p1.y + par_y * sufficientlyLargeDistance);
ctx.lineTo(obj.p1.x + (par_x - per_x) * sufficientlyLargeDistance, obj.p1.y + (par_y - per_y) * sufficientlyLargeDistance);
ctx.lineTo(obj.p1.x - (par_x + per_x) * sufficientlyLargeDistance, obj.p1.y - (par_y + per_y) * sufficientlyLargeDistance);
objTypes['refractor'].fillGlass(obj.p, obj);
}
ctx.fillStyle = 'indigo';
ctx.fillRect(obj.p1.x - 2, obj.p1.y - 2, 3, 3);
ctx.fillRect(obj.p2.x - 2, obj.p2.y - 2, 3, 3);
},
//=============================當物件被光射到時================================
shot: function(obj, ray, rayIndex, rp, surfaceMerging_objs) {
//ray.exist=false;
var rdots = (ray.p2.x - ray.p1.x) * (obj.p2.x - obj.p1.x) + (ray.p2.y - ray.p1.y) * (obj.p2.y - obj.p1.y); //ray與此線段之內積
var ssq = (obj.p2.x - obj.p1.x) * (obj.p2.x - obj.p1.x) + (obj.p2.y - obj.p1.y) * (obj.p2.y - obj.p1.y); //此線段長度平方
var normal = {x: rdots * (obj.p2.x - obj.p1.x) - ssq * (ray.p2.x - ray.p1.x), y: rdots * (obj.p2.y - obj.p1.y) - ssq * (ray.p2.y - ray.p1.y)};
//normal.x=rdots*(obj.p2.x-obj.p1.x)-ssq*(ray.p2.x-ray.p1.x);
//normal.y=rdots*(obj.p2.y-obj.p1.y)-ssq*(ray.p2.y-ray.p1.y);
var shotType = this.getShotType(obj, ray);
if (shotType == 1)
{
//從內部射向外部
var n1 = obj.p; //來源介質的折射率(目的介質假設為1)
//canvasPainter.draw(graphs.segment(ray.p1,s_point),canvas,"red");
}
else if (shotType == -1)
{
//從外部射向內部
var n1 = 1 / obj.p;
}
else
{
//可能導致Bug的狀況(如射到邊界點)
//為防止光線射向錯誤方向導致誤解,將光線吸收
ray.exist = false;
return;
}
//界面融合
//if(surfaceMerging_obj)
for (var i = 0; i < surfaceMerging_objs.length; i++)
{
shotType = objTypes[surfaceMerging_objs[i].type].getShotType(surfaceMerging_objs[i], ray);
if (shotType == 1)
{
//從內部射向外部
n1 *= surfaceMerging_objs[i].p;
}
else if (shotType == -1)
{
//從外部射向內部
n1 /= surfaceMerging_objs[i].p;
}
else if (shotType == 0)
{
//等同於沒射到(例如兩界面重合)
//n1=n1;
}
else
{
//可能導致Bug的狀況(如射到邊界點)
//為防止光線射向錯誤方向導致誤解,將光線吸收
ray.exist = false;
return;
}
}
objTypes['refractor'].refract(ray, rayIndex, rp, normal, n1);
},
getShotType: function(obj, ray) {
var rcrosss = (ray.p2.x - ray.p1.x) * (obj.p2.y - obj.p1.y) - (ray.p2.y - ray.p1.y) * (obj.p2.x - obj.p1.x);
if (rcrosss > 0)
{
return 1; //由內向外
}
if (rcrosss < 0)
{
return -1; //由外向內
}
return 2;
}
};
//"circlelens"物件
objTypes['circlelens'] = {
p_name: 'Refractive index', //屬性名稱
p_min: 1,
p_max: 3,
p_step: 0.01,
supportSurfaceMerging: true, //支援界面融合
//======================================建立物件=========================================
create: function(mouse) {
return {type: 'circlelens', p1: mouse, p2: mouse, p: 1.5};
},
//使用lineobj原型
c_mousedown: objTypes['lineobj'].c_mousedown,
c_mousemove: function(obj, mouse, ctrl, shift) {objTypes['lineobj'].c_mousemove(obj, mouse, false, shift)},
c_mouseup: objTypes['lineobj'].c_mouseup,
move: objTypes['lineobj'].move,
//==========================繪圖區被按下時(判斷物件被按下的部分)===========================
// When the drawing area is pressed (to determine the part of the object being pressed)
clicked: function(obj, mouse_nogrid, mouse, draggingPart) {
// clicking on p1 (center)?
if (mouseOnPoint(mouse_nogrid, obj.p1) && graphs.length_squared(mouse_nogrid, obj.p1) <= graphs.length_squared(mouse_nogrid, obj.p2))
{
draggingPart.part = 1;
draggingPart.targetPoint = graphs.point(obj.p1.x, obj.p1.y);
return true;
}
// clicking on p2 (edge)?
if (mouseOnPoint(mouse_nogrid, obj.p2))
{
draggingPart.part = 2;
draggingPart.targetPoint = graphs.point(obj.p2.x, obj.p2.y);
return true;
}
// clicking on outer edge of circle? then drag entire circle
//if (Math.abs(graphs.length(obj.p1, mouse_nogrid) - graphs.length_segment(obj)) < clickExtent_line)
// clicking inside circle? then drag entire circle
if (Math.abs(graphs.length(obj.p1, mouse_nogrid) < graphs.length_segment(obj)))
{
draggingPart.part = 0;
draggingPart.mouse0 = mouse; //開始拖曳時的滑鼠位置
draggingPart.mouse1 = mouse; //拖曳時上一點的滑鼠位置
draggingPart.snapData = {};
return true;
}
return false;
},
//=================================拖曳物件時====================================
dragging: function(obj, mouse, draggingPart, ctrl, shift) {objTypes['lineobj'].dragging(obj, mouse, draggingPart, false, shift)},
//====================判斷一道光是否會射到此物件(若是,則回傳交點)====================
rayIntersection: function(obj, ray) {
if (obj.p <= 0)return;
var rp_temp = graphs.intersection_line_circle(graphs.line(ray.p1, ray.p2), graphs.circle(obj.p1, obj.p2)); //求光(的延長線)與鏡子的交點
var rp_exist = [];
var rp_lensq = [];
for (var i = 1; i <= 2; i++)
{
rp_exist[i] = graphs.intersection_is_on_ray(rp_temp[i], ray) && graphs.length_squared(rp_temp[i], ray.p1) > minShotLength_squared;
rp_lensq[i] = graphs.length_squared(ray.p1, rp_temp[i]); //光線射到第i交點的距離
}
if (rp_exist[1] && ((!rp_exist[2]) || rp_lensq[1] < rp_lensq[2])) {return rp_temp[1];}
if (rp_exist[2] && ((!rp_exist[1]) || rp_lensq[2] < rp_lensq[1])) {return rp_temp[2];}
},
//=================================將物件畫到Canvas上====================================
draw: function(obj, canvas, aboveLight) {
if (!aboveLight)
{
ctx.beginPath();
ctx.arc(obj.p1.x, obj.p1.y, graphs.length_segment(obj), 0, Math.PI * 2, false);
objTypes['refractor'].fillGlass(obj.p, obj);
}
ctx.lineWidth = 1;
//ctx.fillStyle="indigo";
ctx.fillStyle = 'red';
ctx.fillRect(obj.p1.x - 2, obj.p1.y - 2, 3, 3);
//ctx.fillStyle="rgb(255,0,255)";
ctx.fillStyle = 'indigo';
//ctx.fillStyle="Purple";
ctx.fillRect(obj.p2.x - 2, obj.p2.y - 2, 3, 3);
},
//=============================當物件被光射到時================================
shot: function(obj, ray, rayIndex, rp, surfaceMerging_objs) {
var midpoint = graphs.midpoint(graphs.line_segment(ray.p1, rp));
var d = graphs.length_squared(obj.p1, obj.p2) - graphs.length_squared(obj.p1, midpoint);
if (d > 0)
{
//從內部射向外部
var n1 = obj.p; //來源介質的折射率(目的介質假設為1)
//var normal={x:rp.x-obj.p1.x,y:rp.y-obj.p1.y};
var normal = {x: obj.p1.x - rp.x, y: obj.p1.y - rp.y};
}
else if (d < 0)
{
//從外部射向內部
var n1 = 1 / obj.p;
var normal = {x: rp.x - obj.p1.x, y: rp.y - obj.p1.y};
//var normal={x:obj.p1.x-rp.x,y:obj.p1.y-rp.y};
}
else
{
//可能導致Bug的狀況(如射到邊界點)
//為防止光線射向錯誤方向導致誤解,將光線吸收
ray.exist = false;
return;
}
//console.log(n1);
var shotType;
//界面融合
//if(surfaceMerging_obj)
for (var i = 0; i < surfaceMerging_objs.length; i++)
{
shotType = objTypes[surfaceMerging_objs[i].type].getShotType(surfaceMerging_objs[i], ray);
if (shotType == 1)
{
//從內部射向外部
n1 *= surfaceMerging_objs[i].p;
}
else if (shotType == -1)
{
//從外部射向內部
n1 /= surfaceMerging_objs[i].p;
}
else if (shotType == 0)
{
//等同於沒射到(例如兩界面重合)
//n1=n1;
}
else
{
//可能導致Bug的狀況(如射到邊界點)
//為防止光線射向錯誤方向導致誤解,將光線吸收
ray.exist = false;
return;
}
}
objTypes['refractor'].refract(ray, rayIndex, rp, normal, n1);
},
getShotType: function(obj, ray) {
var midpoint = graphs.midpoint(graphs.line_segment(ray.p1, this.rayIntersection(obj, ray)));
var d = graphs.length_squared(obj.p1, obj.p2) - graphs.length_squared(obj.p1, midpoint);
if (d > 0)
{
return 1; //由內向外
}
if (d < 0)
{
return -1; //由外向內
}
return 2;
}
};
//"refractor"物件
objTypes['refractor'] = {
p_name: 'Refractive index', //屬性名稱
p_min: 1,
p_max: 3,
p_step: 0.01,
supportSurfaceMerging: true, //支援界面融合
//======================================建立物件=========================================
create: function(mouse) {
return {type: 'refractor', path: [{x: mouse.x, y: mouse.y, arc: false}], notDone: true, p: 1.5};
},
//==============================建立物件過程滑鼠按下=======================================
c_mousedown: function(obj, mouse)
{
if (obj.path.length > 1)
{
if (obj.path.length > 3 && mouseOnPoint(mouse, obj.path[0]))
{
//滑鼠按了第一點
obj.path.length--;
obj.notDone = false;
draw();
return;
}
obj.path[obj.path.length - 1] = {x: mouse.x, y: mouse.y}; //移動最後一點
obj.path[obj.path.length - 1].arc = true;
}
},
//==============================建立物件過程滑鼠移動=======================================
c_mousemove: function(obj, mouse, ctrl, shift)
{
if (!obj.notDone) {return;}
if (typeof obj.path[obj.path.length - 1].arc != 'undefined')
{
if (obj.path[obj.path.length - 1].arc && Math.sqrt(Math.pow(obj.path[obj.path.length - 1].x - mouse.x, 2) + Math.pow(obj.path[obj.path.length - 1].y - mouse.y, 2)) >= 5)
{
obj.path[obj.path.length] = mouse;
draw();
}
}
else
{
obj.path[obj.path.length - 1] = {x: mouse.x, y: mouse.y}; //移動最後一點
draw();
}
},
//==============================建立物件過程滑鼠放開=======================================
c_mouseup: function(obj, mouse)
{
if (!obj.notDone) {
isConstructing = false;
draw();
return;
}
if (obj.path.length > 3 && mouseOnPoint(mouse, obj.path[0]))
{
//滑鼠在第一點處放開
obj.path.length--;
obj.notDone = false;
isConstructing = false;
draw();
return;
}
if (obj.path[obj.path.length - 2] && !obj.path[obj.path.length - 2].arc && mouseOnPoint_construct(mouse, obj.path[obj.path.length - 2]))
{
delete obj.path[obj.path.length - 1].arc;
}
else
{
obj.path[obj.path.length - 1] = {x: mouse.x, y: mouse.y}; //移動最後一點
obj.path[obj.path.length - 1].arc = false;
obj.path[obj.path.length] = {x: mouse.x, y: mouse.y}; //建立新的一點
}
draw();
},
//=================================將物件畫到Canvas上====================================
draw: function(obj, canvas, aboveLight) {
//var ctx = canvas.getContext('2d');
var p1;
var p2;
var p3;
var center;
var r;
var a1;
var a2;
var a3;
var acw;
if (obj.notDone)
{
//使用者尚未畫完物件
ctx.beginPath();
ctx.moveTo(obj.path[0].x, obj.path[0].y);
for (var i = 0; i < obj.path.length - 1; i++)
{
//ii=i%(obj.path.length);
if (obj.path[(i + 1)].arc && !obj.path[i].arc && i < obj.path.length - 2)
{
p1 = graphs.point(obj.path[i].x, obj.path[i].y);
p2 = graphs.point(obj.path[(i + 2)].x, obj.path[(i + 2)].y);
p3 = graphs.point(obj.path[(i + 1)].x, obj.path[(i + 1)].y);
center = graphs.intersection_2line(graphs.perpendicular_bisector(graphs.line(p1, p3)), graphs.perpendicular_bisector(graphs.line(p2, p3)));
if (isFinite(center.x) && isFinite(center.y))
{
r = graphs.length(center, p3);
a1 = Math.atan2(p1.y - center.y, p1.x - center.x);
a2 = Math.atan2(p2.y - center.y, p2.x - center.x);
a3 = Math.atan2(p3.y - center.y, p3.x - center.x);
acw = (a2 < a3 && a3 < a1) || (a1 < a2 && a2 < a3) || (a3 < a1 && a1 < a2); //p1->p3->p2之旋轉方向,逆時針為true
ctx.arc(center.x, center.y, r, a1, a2, acw);
}
else
{
//圓弧三點共線,當作線段處理
//arcInvalid=true;
ctx.lineTo(obj.path[(i + 2)].x, obj.path[(i + 2)].y);
}
}
else if (!obj.path[(i + 1)].arc && !obj.path[i].arc)
{
ctx.lineTo(obj.path[(i + 1)].x, obj.path[(i + 1)].y);
}
}
//if(!arcInvalid)
ctx.globalAlpha = 1;
ctx.strokeStyle = 'rgb(128,128,128)';
ctx.lineWidth = 1;
ctx.stroke();
}
else if (!aboveLight)
{
//物件已經畫完
ctx.beginPath();
ctx.moveTo(obj.path[0].x, obj.path[0].y);
for (var i = 0; i < obj.path.length; i++)
{
//ii=i%(obj.path.length);
if (obj.path[(i + 1) % obj.path.length].arc && !obj.path[i % obj.path.length].arc)
{
p1 = graphs.point(obj.path[i % obj.path.length].x, obj.path[i % obj.path.length].y);
p2 = graphs.point(obj.path[(i + 2) % obj.path.length].x, obj.path[(i + 2) % obj.path.length].y);
p3 = graphs.point(obj.path[(i + 1) % obj.path.length].x, obj.path[(i + 1) % obj.path.length].y);
center = graphs.intersection_2line(graphs.perpendicular_bisector(graphs.line(p1, p3)), graphs.perpendicular_bisector(graphs.line(p2, p3)));
//console.log([center.x,center.y]);
if (isFinite(center.x) && isFinite(center.y))
{
r = graphs.length(center, p3);
a1 = Math.atan2(p1.y - center.y, p1.x - center.x);