-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
1472 lines (1235 loc) · 48.7 KB
/
Copy pathgame.js
File metadata and controls
1472 lines (1235 loc) · 48.7 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
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
// ================================================
// EUCLID'S ELEMENTS — BOOK I
// ================================================
// ---------- GEOMETRY STATE ----------
var geo = {
points: [],
lines: [],
circles: [],
nextLabelIndex: 0,
history: [],
};
var canvas, ctx;
var currentTool = "point";
var toolState = { firstPoint: null };
var mouseX = 0;
var mouseY = 0;
var hoveredPoint = null;
var snapTarget = null;
var SNAP_RADIUS = 15;
var POINT_RADIUS = 5;
var LABELS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// ---------- MATH HELPERS ----------
function dist(x1, y1, x2, y2) {
var dx = x2 - x1;
var dy = y2 - y1;
return Math.sqrt(dx * dx + dy * dy);
}
function getPointById(id) {
for (var i = 0; i < geo.points.length; i++) {
if (geo.points[i].id === id) return geo.points[i];
}
return null;
}
function getNextLabel() {
var label = LABELS[geo.nextLabelIndex % LABELS.length];
geo.nextLabelIndex++;
return label;
}
// ---------- INTERSECTION MATH ----------
function circleCircleIntersections(c1x, c1y, r1, c2x, c2y, r2) {
var d = dist(c1x, c1y, c2x, c2y);
if (d > r1 + r2 + 0.01) return [];
if (d < Math.abs(r1 - r2) - 0.01) return [];
if (d < 0.01) return [];
var a = (r1 * r1 - r2 * r2 + d * d) / (2 * d);
var hSq = r1 * r1 - a * a;
if (hSq < 0) hSq = 0;
var h = Math.sqrt(hSq);
var mx = c1x + a * (c2x - c1x) / d;
var my = c1y + a * (c2y - c1y) / d;
var results = [];
results.push({
x: mx + h * (c2y - c1y) / d,
y: my - h * (c2x - c1x) / d
});
if (h > 0.01) {
results.push({
x: mx - h * (c2y - c1y) / d,
y: my + h * (c2x - c1x) / d
});
}
return results;
}
function lineCircleIntersections(p1x, p1y, p2x, p2y, cx, cy, r) {
var dx = p2x - p1x;
var dy = p2y - p1y;
var fx = p1x - cx;
var fy = p1y - cy;
var a = dx * dx + dy * dy;
var b = 2 * (fx * dx + fy * dy);
var c = fx * fx + fy * fy - r * r;
var disc = b * b - 4 * a * c;
if (disc < 0) return [];
var results = [];
var sqrtDisc = Math.sqrt(disc);
var t1 = (-b - sqrtDisc) / (2 * a);
var t2 = (-b + sqrtDisc) / (2 * a);
[t1, t2].forEach(function(t) {
var ix = p1x + t * dx;
var iy = p1y + t * dy;
if (ix >= -20 && ix <= 800 && iy >= -20 && iy <= 520) {
results.push({ x: ix, y: iy });
}
});
return results;
}
function lineLineIntersection(p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y) {
var denom = (p1x - p2x) * (p3y - p4y) - (p1y - p2y) * (p3x - p4x);
if (Math.abs(denom) < 0.01) return [];
var t = ((p1x - p3x) * (p3y - p4y) - (p1y - p3y) * (p3x - p4x)) / denom;
var ix = p1x + t * (p2x - p1x);
var iy = p1y + t * (p2y - p1y);
if (ix >= 0 && ix <= 800 && iy >= 0 && iy <= 520) {
return [{ x: ix, y: iy }];
}
return [];
}
// ---------- FIND ALL INTERSECTIONS ----------
function getAllIntersections() {
var pts = [];
// Circle-circle
for (var i = 0; i < geo.circles.length; i++) {
for (var j = i + 1; j < geo.circles.length; j++) {
var c1 = geo.circles[i];
var c2 = geo.circles[j];
var cen1 = getPointById(c1.centerId);
var edge1 = getPointById(c1.edgeId);
var cen2 = getPointById(c2.centerId);
var edge2 = getPointById(c2.edgeId);
if (cen1 && edge1 && cen2 && edge2) {
var r1 = dist(cen1.x, cen1.y, edge1.x, edge1.y);
var r2 = dist(cen2.x, cen2.y, edge2.x, edge2.y);
var ints = circleCircleIntersections(cen1.x, cen1.y, r1, cen2.x, cen2.y, r2);
pts = pts.concat(ints);
}
}
}
// Line-circle
for (var i = 0; i < geo.lines.length; i++) {
for (var j = 0; j < geo.circles.length; j++) {
var l = geo.lines[i];
var c = geo.circles[j];
var p1 = getPointById(l.p1id);
var p2 = getPointById(l.p2id);
var cen = getPointById(c.centerId);
var edge = getPointById(c.edgeId);
if (p1 && p2 && cen && edge) {
var r = dist(cen.x, cen.y, edge.x, edge.y);
var ints = lineCircleIntersections(p1.x, p1.y, p2.x, p2.y, cen.x, cen.y, r);
pts = pts.concat(ints);
}
}
}
// Line-line
for (var i = 0; i < geo.lines.length; i++) {
for (var j = i + 1; j < geo.lines.length; j++) {
var l1 = geo.lines[i];
var l2 = geo.lines[j];
var p1 = getPointById(l1.p1id);
var p2 = getPointById(l1.p2id);
var p3 = getPointById(l2.p1id);
var p4 = getPointById(l2.p2id);
if (p1 && p2 && p3 && p4) {
var ints = lineLineIntersection(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y);
pts = pts.concat(ints);
}
}
}
// Remove intersections too close to existing points
var filtered = [];
for (var i = 0; i < pts.length; i++) {
var tooClose = false;
for (var j = 0; j < geo.points.length; j++) {
if (dist(pts[i].x, pts[i].y, geo.points[j].x, geo.points[j].y) < SNAP_RADIUS) {
tooClose = true;
break;
}
}
if (!tooClose) {
var duplicate = false;
for (var j = 0; j < filtered.length; j++) {
if (dist(pts[i].x, pts[i].y, filtered[j].x, filtered[j].y) < SNAP_RADIUS) {
duplicate = true;
break;
}
}
if (!duplicate) filtered.push(pts[i]);
}
}
return filtered;
}
// ---------- SNAP SYSTEM ----------
function findNearestPoint(x, y) {
var best = null;
var bestDist = SNAP_RADIUS;
for (var i = 0; i < geo.points.length; i++) {
var d = dist(x, y, geo.points[i].x, geo.points[i].y);
if (d < bestDist) {
bestDist = d;
best = geo.points[i];
}
}
return best;
}
function findNearestIntersection(x, y) {
var intersections = getAllIntersections();
var best = null;
var bestDist = SNAP_RADIUS;
for (var i = 0; i < intersections.length; i++) {
var d = dist(x, y, intersections[i].x, intersections[i].y);
if (d < bestDist) {
bestDist = d;
best = intersections[i];
}
}
return best;
}
// ---------- ADD GEOMETRY ----------
function addPoint(x, y, label, isGiven) {
var id = "pt_" + Date.now() + "_" + Math.floor(Math.random() * 10000);
if (!label) label = getNextLabel();
var pt = { id: id, x: x, y: y, label: label, isGiven: !!isGiven };
geo.points.push(pt);
if (!isGiven) geo.history.push({ type: "point", id: id });
return pt;
}
function addLine(p1id, p2id, isGiven) {
var id = "ln_" + Date.now() + "_" + Math.floor(Math.random() * 10000);
var ln = { id: id, p1id: p1id, p2id: p2id, isGiven: !!isGiven };
geo.lines.push(ln);
if (!isGiven) geo.history.push({ type: "line", id: id });
return ln;
}
function addCircle(centerId, edgeId, isGiven) {
var id = "cr_" + Date.now() + "_" + Math.floor(Math.random() * 10000);
var cr = { id: id, centerId: centerId, edgeId: edgeId, isGiven: !!isGiven };
geo.circles.push(cr);
if (!isGiven) geo.history.push({ type: "circle", id: id });
return cr;
}
// ---------- UNDO ----------
function undo() {
if (geo.history.length === 0) return;
var last = geo.history.pop();
if (last.type === "point") {
var pid = last.id;
geo.circles = geo.circles.filter(function(c) {
return c.centerId !== pid && c.edgeId !== pid;
});
geo.lines = geo.lines.filter(function(l) {
return l.p1id !== pid && l.p2id !== pid;
});
geo.points = geo.points.filter(function(p) { return p.id !== pid; });
geo.nextLabelIndex = Math.max(0, geo.nextLabelIndex - 1);
} else if (last.type === "line") {
geo.lines = geo.lines.filter(function(l) { return l.id !== last.id; });
} else if (last.type === "circle") {
geo.circles = geo.circles.filter(function(c) { return c.id !== last.id; });
}
toolState.firstPoint = null;
render();
}
function clearConstruction() {
geo.points = geo.points.filter(function(p) { return p.isGiven; });
geo.lines = geo.lines.filter(function(l) { return l.isGiven; });
geo.circles = geo.circles.filter(function(c) { return c.isGiven; });
geo.history = [];
geo.nextLabelIndex = geo.points.length;
toolState.firstPoint = null;
render();
}
// ---------- CANVAS RENDERING ----------
function render() {
if (!canvas || !ctx) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Subtle grid
ctx.strokeStyle = "#e8e4dc";
ctx.lineWidth = 0.5;
for (var x = 0; x < canvas.width; x += 40) {
ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x, canvas.height); ctx.stroke();
}
for (var y = 0; y < canvas.height; y += 40) {
ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(canvas.width, y); ctx.stroke();
}
// Circles
for (var i = 0; i < geo.circles.length; i++) {
var c = geo.circles[i];
var center = getPointById(c.centerId);
var edge = getPointById(c.edgeId);
if (center && edge) {
var r = dist(center.x, center.y, edge.x, edge.y);
ctx.beginPath();
ctx.arc(center.x, center.y, r, 0, Math.PI * 2);
ctx.strokeStyle = c.isGiven ? "#2c2c2c" : "#4a90d9";
ctx.lineWidth = c.isGiven ? 2 : 1.5;
ctx.stroke();
}
}
// Lines
for (var i = 0; i < geo.lines.length; i++) {
var l = geo.lines[i];
var p1 = getPointById(l.p1id);
var p2 = getPointById(l.p2id);
if (p1 && p2) {
ctx.beginPath();
ctx.moveTo(p1.x, p1.y);
ctx.lineTo(p2.x, p2.y);
ctx.strokeStyle = l.isGiven ? "#2c2c2c" : "#4a90d9";
ctx.lineWidth = l.isGiven ? 2 : 1.5;
ctx.stroke();
}
}
// Intersection indicators (small red circles)
var intersections = getAllIntersections();
for (var i = 0; i < intersections.length; i++) {
var pt = intersections[i];
ctx.beginPath();
ctx.arc(pt.x, pt.y, 4, 0, Math.PI * 2);
ctx.strokeStyle = "#d9534f";
ctx.lineWidth = 1.5;
ctx.stroke();
}
// Points
for (var i = 0; i < geo.points.length; i++) {
var p = geo.points[i];
ctx.beginPath();
ctx.arc(p.x, p.y, POINT_RADIUS, 0, Math.PI * 2);
ctx.fillStyle = p.isGiven ? "#2c2c2c" : "#4a90d9";
ctx.fill();
ctx.font = "bold 16px 'Cormorant Garamond', serif";
ctx.fillStyle = "#2c2c2c";
ctx.fillText(p.label, p.x + 10, p.y - 10);
}
// Tool preview (ghost line or circle)
if (toolState.firstPoint) {
var fp = toolState.firstPoint;
ctx.setLineDash([5, 5]);
ctx.strokeStyle = "rgba(74, 144, 217, 0.5)";
ctx.lineWidth = 1.5;
if (currentTool === "line") {
ctx.beginPath();
ctx.moveTo(fp.x, fp.y);
ctx.lineTo(mouseX, mouseY);
ctx.stroke();
} else if (currentTool === "circle") {
var r = dist(fp.x, fp.y, mouseX, mouseY);
ctx.beginPath();
ctx.arc(fp.x, fp.y, r, 0, Math.PI * 2);
ctx.stroke();
}
ctx.setLineDash([]);
}
// Hover highlight
if (hoveredPoint) {
ctx.beginPath();
ctx.arc(hoveredPoint.x, hoveredPoint.y, SNAP_RADIUS, 0, Math.PI * 2);
ctx.strokeStyle = "rgba(74, 144, 217, 0.3)";
ctx.lineWidth = 2;
ctx.stroke();
}
if (snapTarget) {
ctx.beginPath();
ctx.arc(snapTarget.x, snapTarget.y, SNAP_RADIUS, 0, Math.PI * 2);
ctx.strokeStyle = "rgba(217, 83, 79, 0.4)";
ctx.lineWidth = 2;
ctx.stroke();
}
}
// ---------- MOUSE HANDLING ----------
function getMousePos(e) {
var rect = canvas.getBoundingClientRect();
var scaleX = canvas.width / rect.width;
var scaleY = canvas.height / rect.height;
return {
x: (e.clientX - rect.left) * scaleX,
y: (e.clientY - rect.top) * scaleY
};
}
function handleMouseMove(e) {
var pos = getMousePos(e);
mouseX = pos.x;
mouseY = pos.y;
hoveredPoint = findNearestPoint(pos.x, pos.y);
snapTarget = null;
if (!hoveredPoint) {
snapTarget = findNearestIntersection(pos.x, pos.y);
}
// Update hint text based on tool state
var hintEl = document.getElementById("hint-text");
if (currentTool === "point") {
if (snapTarget) {
hintEl.textContent = "Click to place a point at this intersection.";
} else if (hoveredPoint) {
hintEl.textContent = "Point " + hoveredPoint.label + " — already placed.";
} else {
hintEl.textContent = "Click to place a free point.";
}
} else if (currentTool === "line") {
if (toolState.firstPoint) {
if (hoveredPoint) {
hintEl.textContent = "Click " + hoveredPoint.label + " to complete line from " + toolState.firstPoint.label + ".";
} else if (snapTarget) {
hintEl.textContent = "Click intersection to create point and complete line from " + toolState.firstPoint.label + ".";
} else {
hintEl.textContent = "Click a point to complete line from " + toolState.firstPoint.label + ". (Or click empty space for free point.)";
}
} else {
if (hoveredPoint) {
hintEl.textContent = "Click " + hoveredPoint.label + " to start a line.";
} else {
hintEl.textContent = "Click a point to start a line.";
}
}
} else if (currentTool === "circle") {
if (toolState.firstPoint) {
var r = Math.round(dist(toolState.firstPoint.x, toolState.firstPoint.y, mouseX, mouseY));
if (hoveredPoint) {
hintEl.textContent = "Click " + hoveredPoint.label + " to set radius. Circle centered at " + toolState.firstPoint.label + " (r≈" + r + ").";
} else if (snapTarget) {
hintEl.textContent = "Click intersection to set radius. Circle centered at " + toolState.firstPoint.label + " (r≈" + r + ").";
} else {
hintEl.textContent = "Click to set radius for circle centered at " + toolState.firstPoint.label + " (r≈" + r + ").";
}
} else {
if (hoveredPoint) {
hintEl.textContent = "Click " + hoveredPoint.label + " as center of circle.";
} else {
hintEl.textContent = "Click a point to set as center of circle.";
}
}
}
render();
}
function handleClick(e) {
var pos = getMousePos(e);
var clickedPoint = findNearestPoint(pos.x, pos.y);
var clickedSnap = null;
if (!clickedPoint) {
clickedSnap = findNearestIntersection(pos.x, pos.y);
}
if (currentTool === "point") {
handlePointTool(pos, clickedPoint, clickedSnap);
} else if (currentTool === "line") {
handleLineTool(pos, clickedPoint, clickedSnap);
} else if (currentTool === "circle") {
handleCircleTool(pos, clickedPoint, clickedSnap);
}
render();
}
// ---------- POINT TOOL ----------
function handlePointTool(pos, clickedPoint, clickedSnap) {
if (clickedPoint) {
// Already exists, do nothing
return;
}
if (clickedSnap) {
addPoint(clickedSnap.x, clickedSnap.y);
} else {
addPoint(pos.x, pos.y);
}
}
// ---------- LINE TOOL ----------
function handleLineTool(pos, clickedPoint, clickedSnap) {
var target = null;
if (clickedPoint) {
target = clickedPoint;
} else if (clickedSnap) {
target = addPoint(clickedSnap.x, clickedSnap.y);
} else {
// For lines, we require clicking on a point or intersection
// but let's allow free points too for flexibility
target = addPoint(pos.x, pos.y);
}
if (!toolState.firstPoint) {
// First click — set start point
toolState.firstPoint = target;
} else {
// Second click — complete the line
if (target.id !== toolState.firstPoint.id) {
addLine(toolState.firstPoint.id, target.id);
}
toolState.firstPoint = null;
}
}
// ---------- CIRCLE TOOL ----------
function handleCircleTool(pos, clickedPoint, clickedSnap) {
var target = null;
if (clickedPoint) {
target = clickedPoint;
} else if (clickedSnap) {
target = addPoint(clickedSnap.x, clickedSnap.y);
} else {
target = addPoint(pos.x, pos.y);
}
if (!toolState.firstPoint) {
// First click — set center
toolState.firstPoint = target;
} else {
// Second click — set edge point (radius)
if (target.id !== toolState.firstPoint.id) {
addCircle(toolState.firstPoint.id, target.id);
}
toolState.firstPoint = null;
}
}
// ---------- TOOL SELECTION ----------
function setTool(tool) {
currentTool = tool;
toolState.firstPoint = null;
// Update toolbar button styles
var btns = document.querySelectorAll(".tool-btn");
btns.forEach(function(btn) {
btn.classList.remove("active");
if (btn.getAttribute("data-tool") === tool) {
btn.classList.add("active");
}
});
// Update hint
var hintEl = document.getElementById("hint-text");
if (tool === "point") {
hintEl.textContent = "Click on the canvas to place a point, or click an intersection.";
} else if (tool === "line") {
hintEl.textContent = "Click a point to start a line, then click another point to finish it. [Postulate 1]";
} else if (tool === "circle") {
hintEl.textContent = "Click a point for the center, then click another point to set the radius. [Postulate 3]";
}
render();
}
// ---------- KEYBOARD SHORTCUTS ----------
function handleKeypress(e) {
if (e.key === "p" || e.key === "P") setTool("point");
if (e.key === "l" || e.key === "L") setTool("line");
if (e.key === "c" || e.key === "C") setTool("circle");
if (e.key === "z" && (e.ctrlKey || e.metaKey)) {
e.preventDefault();
undo();
}
}
// ---------- SETUP CANVAS EVENTS ----------
function initCanvas() {
canvas = document.getElementById("geo-canvas");
ctx = canvas.getContext("2d");
canvas.addEventListener("mousemove", handleMouseMove);
canvas.addEventListener("click", handleClick);
document.addEventListener("keydown", handleKeypress);
// Touch support for mobile
canvas.addEventListener("touchstart", function(e) {
e.preventDefault();
var touch = e.touches[0];
var mouseEvent = new MouseEvent("click", {
clientX: touch.clientX,
clientY: touch.clientY
});
canvas.dispatchEvent(mouseEvent);
});
canvas.addEventListener("touchmove", function(e) {
e.preventDefault();
var touch = e.touches[0];
var mouseEvent = new MouseEvent("mousemove", {
clientX: touch.clientX,
clientY: touch.clientY
});
canvas.dispatchEvent(mouseEvent);
});
}
// ---------- TOOLBAR BUTTON EVENTS ----------
function initToolbar() {
var toolBtns = document.querySelectorAll(".tool-btn[data-tool]");
toolBtns.forEach(function(btn) {
btn.addEventListener("click", function() {
setTool(btn.getAttribute("data-tool"));
});
});
document.getElementById("undo-btn").addEventListener("click", function() {
undo();
});
document.getElementById("clear-canvas-btn").addEventListener("click", function() {
if (confirm("Clear all your construction? (Given elements will remain.)")) {
clearConstruction();
}
});
}
// ---------- FACT DATABASES ----------
var definitions = [
{ id: "def1", label: "Definition 1", text: "A point is that which has no part." },
{ id: "def2", label: "Definition 2", text: "A line is breadthless length." },
{ id: "def4", label: "Definition 4", text: "A straight line lies evenly with the points on itself." },
{ id: "def10", label: "Definition 10", text: "When a straight line standing on another makes adjacent angles equal, each is a right angle." },
{ id: "def15", label: "Definition 15", text: "A circle is a plane figure contained by one line such that all straight lines from the center to the circumference are equal." },
{ id: "def20", label: "Definition 20", text: "Of trilateral figures, an equilateral triangle is that which has its three sides equal." },
{ id: "def21", label: "Definition 21", text: "An isosceles triangle is that which has two of its sides equal." },
];
var postulates = [
{ id: "post1", label: "Postulate 1", text: "To draw a straight line from any point to any point." },
{ id: "post2", label: "Postulate 2", text: "To produce a finite straight line continuously in a straight line." },
{ id: "post3", label: "Postulate 3", text: "To describe a circle with any center and radius." },
{ id: "post4", label: "Postulate 4", text: "All right angles are equal to one another." },
{ id: "post5", label: "Postulate 5", text: "If a straight line falling on two straight lines makes the interior angles on the same side less than two right angles, the lines if produced meet on that side." },
];
var commonNotions = [
{ id: "cn1", label: "Common Notion 1", text: "Things which are equal to the same thing are also equal to one another." },
{ id: "cn2", label: "Common Notion 2", text: "If equals are added to equals, the wholes are equal." },
{ id: "cn3", label: "Common Notion 3", text: "If equals are subtracted from equals, the remainders are equal." },
{ id: "cn4", label: "Common Notion 4", text: "Things which coincide with one another are equal to one another." },
{ id: "cn5", label: "Common Notion 5", text: "The whole is greater than the part." },
];
// ---------- PROPOSITIONS ----------
var propositions = [
{
id: 1,
title: "Proposition 1",
statement: "On a given finite straight line, to construct an equilateral triangle.",
type: "construction",
unlockText: "🔧 Tool Unlocked: Equilateral Triangle Construction — You can now construct equilateral triangles on any line segment.",
// What the player is given on the canvas
givenSetup: function() {
var A = addPoint(250, 300, "A", true);
var B = addPoint(510, 300, "B", true);
addLine(A.id, B.id, true);
},
// Validate the construction
validateConstruction: function() {
// Need: two circles and a point at their intersection
// forming an equilateral triangle with A and B
if (geo.circles.length < 2) {
return { success: false, message: "You need two circles — one centered at each endpoint." };
}
var A = geo.points[0]; // first given point
var B = geo.points[1]; // second given point
var ab = dist(A.x, A.y, B.x, B.y);
// Check for circle centered at A with radius AB
var hasCircleA = false;
var hasCircleB = false;
for (var i = 0; i < geo.circles.length; i++) {
var c = geo.circles[i];
var center = getPointById(c.centerId);
var edge = getPointById(c.edgeId);
if (!center || !edge) continue;
var r = dist(center.x, center.y, edge.x, edge.y);
if (center.id === A.id && Math.abs(r - ab) < 5) hasCircleA = true;
if (center.id === B.id && Math.abs(r - ab) < 5) hasCircleB = true;
}
if (!hasCircleA || !hasCircleB) {
return { success: false, message: "You need a circle centered at A with radius AB, and a circle centered at B with radius BA." };
}
// Check for equilateral triangle vertex
var foundVertex = false;
var tolerance = 8;
for (var i = 0; i < geo.points.length; i++) {
var p = geo.points[i];
if (p.id === A.id || p.id === B.id) continue;
var dA = dist(p.x, p.y, A.x, A.y);
var dB = dist(p.x, p.y, B.x, B.y);
if (Math.abs(dA - ab) < tolerance && Math.abs(dB - ab) < tolerance) {
// Check that lines exist from this point to A and B
var hasLineToA = false;
var hasLineToB = false;
for (var j = 0; j < geo.lines.length; j++) {
var l = geo.lines[j];
if ((l.p1id === p.id && l.p2id === A.id) || (l.p2id === p.id && l.p1id === A.id)) {
hasLineToA = true;
}
if ((l.p1id === p.id && l.p2id === B.id) || (l.p2id === p.id && l.p1id === B.id)) {
hasLineToB = true;
}
}
if (hasLineToA && hasLineToB) {
foundVertex = true;
break;
}
}
}
if (!foundVertex) {
return { success: false, message: "Place a point at the intersection of the circles, and draw lines from it to both A and B to complete the triangle." };
}
return { success: true, message: "Excellent construction! The equilateral triangle is complete." };
},
requiredFacts: ["post1", "post3", "def15", "def20", "cn1"],
proofSteps: [
{ id: "p1s1", text: "Let AB be the given finite straight line." },
{ id: "p1s2", text: "With center A and radius AB, describe circle BCD. [Postulate 3]" },
{ id: "p1s3", text: "With center B and radius BA, describe circle ACE. [Postulate 3]" },
{ id: "p1s4", text: "Let C be the point at which the circles intersect." },
{ id: "p1s5", text: "Draw the straight line CA from C to A. [Postulate 1]" },
{ id: "p1s6", text: "Draw the straight line CB from C to B. [Postulate 1]" },
{ id: "p1s7", text: "Since A is the center of circle BCD, AC equals AB. [Definition 15]" },
{ id: "p1s8", text: "Since B is the center of circle ACE, BC equals BA. [Definition 15]" },
{ id: "p1s9", text: "Both AC and BC equal AB, so AC also equals BC. [Common Notion 1]" },
{ id: "p1s10", text: "Therefore AB, BC, and AC are equal — triangle ABC is equilateral. [Definition 20] ∎" },
],
distractorSteps: [
{ id: "p1d1", text: "Extend line AB beyond B to a new point D. [Postulate 2]" },
{ id: "p1d2", text: "Since all right angles are equal, angle ACB is right. [Postulate 4]" },
{ id: "p1d3", text: "The whole AB is greater than the part CB. [Common Notion 5]" },
],
},
{
id: 2,
title: "Proposition 2",
statement: "To place at a given point (as an extremity) a straight line equal to a given straight line.",
type: "construction",
unlockText: "🔧 Tool Unlocked: Line Transfer — You can now place a line equal to any given line at any given point.",
givenSetup: function() {
var A = addPoint(200, 200, "A", true);
var B = addPoint(450, 350, "B", true);
var C = addPoint(550, 350, "C", true);
addLine(B.id, C.id, true);
},
validateConstruction: function() {
var A = geo.points[0];
var B = geo.points[1];
var C = geo.points[2];
var bc = dist(B.x, B.y, C.x, C.y);
var tolerance = 8;
// Check: is there a line starting at A whose length equals BC?
var found = false;
for (var i = 0; i < geo.lines.length; i++) {
var l = geo.lines[i];
var p1 = getPointById(l.p1id);
var p2 = getPointById(l.p2id);
if (!p1 || !p2) continue;
var hasA = (p1.id === A.id || p2.id === A.id);
if (!hasA) continue;
var len = dist(p1.x, p1.y, p2.x, p2.y);
if (Math.abs(len - bc) < tolerance) {
found = true;
break;
}
}
if (!found) {
return { success: false, message: "You need a line starting at point A whose length equals BC. Use Proposition 1 to build an equilateral triangle on AB, then use circles to transfer the length." };
}
// Check that they used at least one circle (the construction requires circles)
if (geo.circles.length < 1) {
return { success: false, message: "This construction requires circles to transfer lengths." };
}
return { success: true, message: "The line has been placed at A equal to BC." };
},
requiredFacts: ["post1", "post2", "post3", "def15", "cn1", "cn3", "prop1"],
proofSteps: [
{ id: "p2s1", text: "Let A be the given point, and BC the given straight line." },
{ id: "p2s2", text: "Draw the straight line AB from point A to point B. [Postulate 1]" },
{ id: "p2s3", text: "On AB, construct the equilateral triangle DAB. [Proposition 1]" },
{ id: "p2s4", text: "Produce DA to point E and DB to point F. [Postulate 2]" },
{ id: "p2s5", text: "With center B and radius BC, describe circle CGH. [Postulate 3]" },
{ id: "p2s6", text: "With center D and radius DG, describe circle GKL. [Postulate 3]" },
{ id: "p2s7", text: "Since B is the center of circle CGH, BC equals BG. [Definition 15]" },
{ id: "p2s8", text: "Since D is the center of circle GKL, DL equals DG. [Definition 15]" },
{ id: "p2s9", text: "DA equals DB, since DAB is equilateral." },
{ id: "p2s10", text: "Subtracting equals DA and DB from equals DL and DG, AL equals BG. [Common Notion 3]" },
{ id: "p2s11", text: "Since BC also equals BG, then AL equals BC. [Common Notion 1]" },
{ id: "p2s12", text: "Therefore AL is placed at point A and equals the given line BC. ∎" },
],
distractorSteps: [
{ id: "p2d1", text: "All right angles are equal, so angle DAB is right. [Postulate 4]" },
{ id: "p2d2", text: "The whole DA is greater than the part BA. [Common Notion 5]" },
{ id: "p2d3", text: "If equals are added to equals, the wholes are equal. [Common Notion 2]" },
],
},
{
id: 3,
title: "Proposition 3",
statement: "Given two unequal straight lines, to cut off from the greater a straight line equal to the less.",
type: "construction",
unlockText: "🔧 Tool Unlocked: Line Cutting — You can now cut a line segment to match any shorter given length.",
givenSetup: function() {
var A = addPoint(150, 280, "A", true);
var B = addPoint(550, 280, "B", true);
addLine(A.id, B.id, true);
var C = addPoint(150, 400, "C", true);
var D = addPoint(300, 400, "D", true);
addLine(C.id, D.id, true);
},
validateConstruction: function() {
var A = geo.points[0];
var B = geo.points[1];
var C = geo.points[2];
var D = geo.points[3];
var cd = dist(C.x, C.y, D.x, D.y);
var tolerance = 8;
// Check: is there a point E on line AB such that AE = CD?
var found = false;
for (var i = 0; i < geo.points.length; i++) {
var p = geo.points[i];
if (p.isGiven) continue;
var dToA = dist(p.x, p.y, A.x, A.y);
// Check point is roughly on line AB
var dToB = dist(p.x, p.y, B.x, B.y);
var ab = dist(A.x, A.y, B.x, B.y);
var onLine = Math.abs(dToA + dToB - ab) < tolerance;
if (onLine && Math.abs(dToA - cd) < tolerance) {
found = true;
break;
}
}
if (!found) {
return { success: false, message: "Place a line equal to CD at point A (Prop 2), then use a circle centered at A to find where it cuts AB." };
}
if (geo.circles.length < 1) {
return { success: false, message: "This construction requires a circle to cut the line." };
}
return { success: true, message: "Line AB has been cut at a point equal to CD from A." };
},
requiredFacts: ["post3", "def15", "cn1", "prop2"],
proofSteps: [
{ id: "p3s1", text: "Let AB be the greater and CD the less of two given unequal straight lines." },
{ id: "p3s2", text: "At point A, place a straight line AE equal to CD. [Proposition 2]" },
{ id: "p3s3", text: "With center A and radius AE, describe circle EFG. [Postulate 3]" },
{ id: "p3s4", text: "Let F be the point where the circle intersects AB." },
{ id: "p3s5", text: "Since A is the center of circle EFG, AF equals AE. [Definition 15]" },
{ id: "p3s6", text: "Since AE equals CD, then AF also equals CD. [Common Notion 1]" },
{ id: "p3s7", text: "Therefore AF has been cut off from AB, equal to the lesser line CD. ∎" },
],
distractorSteps: [
{ id: "p3d1", text: "Draw a straight line from B to D. [Postulate 1]" },
{ id: "p3d2", text: "Extend AB beyond B to a new point G. [Postulate 2]" },
{ id: "p3d3", text: "The whole AB is greater than the part AF. [Common Notion 5]" },
],
},
];
// ---------- CONSTRUCTION VALIDATION ----------
function validateCurrentConstruction() {
var prop = state.currentProp;
if (!prop || !prop.validateConstruction) return null;
return prop.validateConstruction();
}
// ---------- GAME STATE ----------
var state = {
completed: [],
currentProp: null,
selectedFacts: [],
proofOrder: [],
availableSteps: [],
selectionResult: null,
attempts: 0,
constructionValidated: false,
};
// ---------- SAVE / LOAD ----------
function saveProgress() {
localStorage.setItem("euclid-save", JSON.stringify(state.completed));
}
function loadProgress() {
var data = localStorage.getItem("euclid-save");
if (data) {
state.completed = JSON.parse(data);
}
}
// ---------- SCREEN HELPERS ----------
function showScreen(id) {
var screens = document.querySelectorAll(".screen");
for (var i = 0; i < screens.length; i++) {
screens[i].classList.remove("active");
}
document.getElementById(id).classList.add("active");
}
function showPhase(id) {
var phases = document.querySelectorAll(".phase");
for (var i = 0; i < phases.length; i++) {
phases[i].classList.remove("active");
}
document.getElementById(id).classList.add("active");
}
function shuffleArray(arr) {
var a = arr.slice();
for (var i = a.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = a[i];
a[i] = a[j];
a[j] = temp;
}
return a;
}
// ---------- GET ALL AVAILABLE FACTS ----------