-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstruction_tests.py
More file actions
999 lines (754 loc) · 37.5 KB
/
construction_tests.py
File metadata and controls
999 lines (754 loc) · 37.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
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
from Constructions import GeometricConstructor
from relations import geometric, predicate
from Problem import GeometricProblem
from verify_eqangles import verify_eqangles
import os
def setup_output_directory():
out_dir = os.path.join(os.path.dirname(__file__), 'test_outputs')
os.makedirs(out_dir, exist_ok=True)
return out_dir
def assert_diagram_predicates_ok(G: GeometricConstructor, test_name: str, tol_deg: float = 0.01):
"""Run verify_eqangles on the current problem and fail if any predicate mismatches the diagram."""
out_dir = setup_output_directory()
outfile = os.path.join(out_dir, f"{test_name}_verification.txt")
verify_eqangles(G.problem, outfile_path=outfile, tol_deg=tol_deg)
with open(outfile, 'r') as f:
contents = f.read().strip()
if contents:
raise AssertionError(f"{test_name}: predicate verification failures:\n{contents}")
# helper to compute line coefficients from two points (ax + by + c = 0)
def line_from_points(p1, p2):
dx = p2.x - p1.x
dy = p2.y - p1.y
a = dy
b = -dx
c = dx * p1.y - dy * p1.x
return (a, b, c)
def print_relations(test_name: str, rels):
"""Print derived relations in a consistent, multi-line format."""
if rels is None:
print(f"{test_name}: No relations")
return
if not isinstance(rels, list):
try:
rels = list(rels)
except Exception:
rels = [rels]
print(f"{test_name}: {len(rels)} derived relation(s)")
for r in rels:
try:
print(f" - {str(r)}")
except Exception:
print(f" - {repr(r)}")
def zoom_to_points(G: GeometricConstructor, pts, pad_frac: float = 0.2):
"""Adjust the axes limits to tightly frame the given points with a fractional padding.
G: GeometricConstructor instance
pts: iterable of geometric.Point
pad_frac: fraction of the span to add as padding (0.2 = 20%)
"""
xs = [p.x for p in pts]
ys = [p.y for p in pts]
if not xs or not ys:
return
xmin, xmax = min(xs), max(xs)
ymin, ymax = min(ys), max(ys)
# If all points share same x or y, provide a small span
dx = max(1e-6, (xmax - xmin))
dy = max(1e-6, (ymax - ymin))
pad_x = dx * pad_frac
pad_y = dy * pad_frac
G.ax.set_xlim(xmin - pad_x, xmax + pad_x)
G.ax.set_ylim(ymin - pad_y, ymax + pad_y)
def test_construct_angle_bisector(G):
A = geometric.Point('A', 0, 0)
B = geometric.Point('B', 1, 1)
C = geometric.Point('C', 2, 0)
# Draw the base points
G.draw_points({'A': A, 'B': B, 'C': C})
# Construct angle bisector
X, rels = G.construct_angle_bisector(A, B, C, None)
# Basic checks
assert X is not None, 'construct_angle_bisector returned None'
# Draw triangle sides and bisector line using the module-level helper
G.draw_lines({'AB': line_from_points(A, B), 'BC': line_from_points(B, C), 'AC': line_from_points(A, C)})
# bisector line through B and X
G.draw_lines({'bisector_B': line_from_points(B, X)})
# Ensure the drawing context is updated
G.ax.figure.canvas.draw()
# Numeric check: X should lie on AC (collinear test via cross product)
ax = X.x - A.x
ay = X.y - A.y
cx = C.x - A.x
cy = C.y - A.y
cross = ax * cy - ay * cx
assert abs(cross) < 1e-8, f'Constructed bisector point not on AC: cross={cross}'
# Relations should include Eqangle and Col
assert isinstance(rels, list) and any(isinstance(r, predicate.Eqangle) for r in rels), 'Expected Eqangle in relations'
assert any(isinstance(r, predicate.Col) for r in rels), 'Expected Col in relations'
print_relations('Angle bisector', rels)
assert_diagram_predicates_ok(G, "construct_angle_bisector")
G.show_construction("Angle Bisector Construction")
def test_construct_angle_bisector_generic(G):
"""Variation: acute scalene triangle to exercise generic angle bisector placement."""
A = geometric.Point('A', -1.5, 0.5)
B = geometric.Point('B', 2.0, 2.5)
C = geometric.Point('C', 3.5, -0.5)
G.draw_points({'A': A, 'B': B, 'C': C})
X, rels = G.construct_angle_bisector(A, B, C, None)
assert X is not None, 'construct_angle_bisector (generic) returned None'
G.draw_lines({'AB': line_from_points(A, B), 'BC': line_from_points(B, C), 'AC': line_from_points(A, C)})
G.draw_lines({'bisector_B_generic': line_from_points(B, X)})
G.ax.figure.canvas.draw()
ax = X.x - A.x
ay = X.y - A.y
cx = C.x - A.x
cy = C.y - A.y
cross = ax * cy - ay * cx
assert abs(cross) < 1e-8, f'Generic bisector point not on AC: cross={cross}'
assert isinstance(rels, list) and any(isinstance(r, predicate.Eqangle) for r in rels), 'Expected Eqangle in generic bisector'
assert any(isinstance(r, predicate.Col) for r in rels), 'Expected Col in generic bisector'
print_relations('Angle bisector (generic)', rels)
assert_diagram_predicates_ok(G, "construct_angle_bisector_generic")
G.show_construction("Angle Bisector Construction (generic)")
def test_construct_midpoint(G):
A = geometric.Point('A', 0, 0)
B = geometric.Point('B', 2, 2)
# Draw the base points
G.draw_points({'A': A, 'B': B})
# Construct midpoint
_, rels = G.construct_midpoint(A, B, None)
# Draw the line segment AB for reference
def line_from_points(p1, p2):
dx = p2.x - p1.x
dy = p2.y - p1.y
a = dy
b = -dx
c = dx * p1.y - dy * p1.x
return (a, b, c)
G.draw_lines({'AB': line_from_points(A, B)})
# Ensure the drawing context is updated
G.ax.figure.canvas.draw()
print_relations('Midpoint', rels)
assert_diagram_predicates_ok(G, "construct_midpoint")
G.show_construction("Midpoint Construction")
def test_construct_circle(G):
A = geometric.Point('A', 0, 0)
B = geometric.Point('B', 4, 0)
C = geometric.Point('C', 2, 3)
# Draw the base points
G.draw_points({'A': A, 'B': B, 'C': C})
# Construct circumcircle using construct_circle
circumcenter, rels = G.construct_circle(A, B, C, cname=f"O_{A.name}{B.name}{C.name}")
# Helper function to compute line equation from two points
def line_from_points(p1, p2):
dx = p2.x - p1.x
dy = p2.y - p1.y
a = dy
b = -dx
c = dx * p1.y - dy * p1.x
return (a, b, c)
# Draw triangle edges for reference
G.draw_lines({
'AB': line_from_points(A, B),
'BC': line_from_points(B, C),
'CA': line_from_points(C, A)
})
# Also draw perpendicular bisectors used to find circumcenter
mid_ab, _ = G.construct_midpoint(A, B, None)
mid_ac, _ = G.construct_midpoint(A, C, None)
perp_ab = G.compute_perpendicular(A, B, mid_ab)
perp_ac = G.compute_perpendicular(A, C, mid_ac)
G.draw_lines({'perp_AB': perp_ab, 'perp_AC': perp_ac})
# Draw the circumcircle explicitly
radius = ((circumcenter.x - A.x)**2 + (circumcenter.y - A.y)**2)**0.5
G.draw_circles({f"Circumcircle_{A.name}{B.name}{C.name}": (circumcenter.x, circumcenter.y, radius)})
# Ensure the drawing context is updated
G.ax.figure.canvas.draw()
print_relations('Circumcircle', rels)
assert_diagram_predicates_ok(G, "construct_circle")
G.show_construction("Circumcircle Construction")
def test_intersect_lines(G):
# Lines y=x and y=1-x intersect at (0.5, 0.5)
p1 = geometric.Point('P1', 0, 0)
p2 = geometric.Point('P2', 1, 1)
p3 = geometric.Point('Q1', 0, 1)
p4 = geometric.Point('Q2', 1, 0)
# Draw base points for reference
G.draw_points({'P1': p1, 'P2': p2, 'Q1': p3, 'Q2': p4})
# Draw the two lines
G.draw_lines({'L1': line_from_points(p1, p2), 'L2': line_from_points(p3, p4)})
P, rels = G.construct_intersect_lines(p1, p2, p3, p4, None)
# Basic assertions to validate function behavior
assert P is not None, 'intersect_lines returned None for intersecting lines'
assert abs(P.x - 0.5) < 1e-8 and abs(P.y - 0.5) < 1e-8, f'Unexpected intersection coords: {(P.x, P.y)}'
assert isinstance(rels, list) and len(rels) == 2, 'Expected two derived relations'
# Check relations are collinearity predicates
assert all(isinstance(r, predicate.Col) for r in rels), 'Derived relations should be predicate.Col'
print('Intersect point:', P.name, P.x, P.y)
print_relations('Intersect lines', rels)
assert_diagram_predicates_ok(G, "intersect_lines")
# Optionally show the construction for manual inspection
G.show_construction('Intersect Lines Test')
def test_construct_incenter(G):
# Triangle for incenter test
A = geometric.Point('A', 0, 0)
B = geometric.Point('B', 4, 0)
C = geometric.Point('C', 1, 3)
# Draw base points
G.draw_points({'A': A, 'B': B, 'C': C})
# Construct incenter
I, rels = G.construct_incenter(A, B, C, name=None)
# Draw triangle edges
G.draw_lines({'AB': line_from_points(A, B), 'BC': line_from_points(B, C), 'CA': line_from_points(C, A)})
# Draw angle bisectors used (lines through vertex and bisector intersection points)
bis1, _ = G.construct_angle_bisector(B, A, C, None)
bis2, _ = G.construct_angle_bisector(A, B, C, None)
G.draw_lines({'bis_A': line_from_points(A, bis1), 'bis_B': line_from_points(B, bis2)})
assert I is not None, 'construct_incenter returned None for a valid triangle'
# Expect at least the three eqangle relations to be present
assert isinstance(rels, list) and any(isinstance(r, predicate.Eqangle) for r in rels), 'Expected eqangle relations in result'
print('Incenter:', I.name, I.x, I.y)
print_relations('Incenter', rels)
# Zoom to the triangle + incenter so the figure isn't overly zoomed out
try:
zoom_to_points(G, [A, B, C, I], pad_frac=0.25)
except Exception:
# If zoom fails for any reason, fall back to autoscale
G.ax.relim(); G.ax.autoscale_view()
assert_diagram_predicates_ok(G, "construct_incenter")
# Show for manual inspection
G.show_construction('Incenter Construction')
def test_construct_incenter_generic(G):
"""Variation: scalene triangle with non-axis-aligned vertices."""
A = geometric.Point('A', -0.5, 0.0)
B = geometric.Point('B', 4.5, 1.0)
C = geometric.Point('C', 1.5, 4.0)
G.draw_points({'A': A, 'B': B, 'C': C})
I, rels = G.construct_incenter(A, B, C, name=None)
G.draw_lines({'AB': line_from_points(A, B), 'BC': line_from_points(B, C), 'CA': line_from_points(C, A)})
bis1, _ = G.construct_angle_bisector(B, A, C, None)
bis2, _ = G.construct_angle_bisector(A, B, C, None)
G.draw_lines({'bis_A_generic': line_from_points(A, bis1), 'bis_B_generic': line_from_points(B, bis2)})
assert I is not None, 'construct_incenter (generic) returned None'
assert isinstance(rels, list) and any(isinstance(r, predicate.Eqangle) for r in rels), 'Expected Eqangle in generic incenter'
print('Incenter (generic):', I.name, I.x, I.y)
print_relations('Incenter (generic)', rels)
try:
zoom_to_points(G, [A, B, C, I], pad_frac=0.25)
except Exception:
G.ax.relim(); G.ax.autoscale_view()
assert_diagram_predicates_ok(G, "construct_incenter_generic")
G.show_construction('Incenter Construction (generic)')
def test_construct_incenter2(G):
# Triangle for incenter2 test
A = geometric.Point('A', 0, 0)
B = geometric.Point('B', 4, 0)
C = geometric.Point('C', 1, 3)
# Draw base points
G.draw_points({'A': A, 'B': B, 'C': C})
# Construct incenter2
I, X, Y, Z, rels = G.construct_incenter2(A, B, C, name=None)
# Basic checks
assert I is not None, 'construct_incenter2 returned None for incenter'
assert X is not None and Y is not None and Z is not None, 'construct_incenter2 did not return tangency points'
# Expect at least the three eqangle relations for the incenter
assert isinstance(rels, list) and any(isinstance(r, predicate.Eqangle) for r in rels), 'Expected Eqangle relations in result'
# Expect perpendicular relations for the three tangency feet
assert any(isinstance(r, predicate.Perp) for r in rels), 'Expected Perp relations for tangency points'
print('Incenter2:', I.name, I.x, I.y)
print_relations('Incenter2', rels)
# Numeric spot-check: X should lie on BC (collinear)
ax = X.x - B.x
ay = X.y - B.y
cx = C.x - B.x
cy = C.y - B.y
cross = ax * cy - ay * cx
assert abs(cross) < 1e-8, f'Constructed tangency X not on BC: cross={cross}'
try:
zoom_to_points(G, [A, B, C, I, X, Y, Z], pad_frac=0.25)
except Exception:
G.ax.relim(); G.ax.autoscale_view()
assert_diagram_predicates_ok(G, "construct_incenter2")
G.show_construction('Incenter2 Construction')
def test_construct_incenter2_generic(G):
"""Variation: scalene triangle; check tangency feet and eqangle/perp relations."""
A = geometric.Point('A', -1.0, 0.5)
B = geometric.Point('B', 5.0, -0.5)
C = geometric.Point('C', 1.5, 4.5)
G.draw_points({'A': A, 'B': B, 'C': C})
I, X, Y, Z, rels = G.construct_incenter2(A, B, C, name=None)
assert I is not None and X is not None and Y is not None and Z is not None, 'construct_incenter2 (generic) returned None'
assert isinstance(rels, list) and any(isinstance(r, predicate.Eqangle) for r in rels), 'Expected Eqangle in generic incenter2'
assert any(isinstance(r, predicate.Perp) for r in rels), 'Expected Perp relations in generic incenter2'
print('Incenter2 (generic):', I.name, I.x, I.y)
print_relations('Incenter2 (generic)', rels)
# Spot-check: X should lie on BC
ax = X.x - B.x
ay = X.y - B.y
cx = C.x - B.x
cy = C.y - B.y
cross = ax * cy - ay * cx
assert abs(cross) < 1e-8, f'Generic tangency X not on BC: cross={cross}'
try:
zoom_to_points(G, [A, B, C, I, X, Y, Z], pad_frac=0.25)
except Exception:
G.ax.relim(); G.ax.autoscale_view()
assert_diagram_predicates_ok(G, "construct_incenter2_generic")
G.show_construction('Incenter2 Construction (generic)')
def test_construct_foot(G):
# A above BC, foot should be vertically below A onto BC
A = geometric.Point('A', 1, 1)
B = geometric.Point('B', 0, 0)
C = geometric.Point('C', 2, 0)
# Draw base points and baseline
G.draw_points({'A': A, 'B': B, 'C': C})
G.draw_lines({'BC': line_from_points(B, C)})
X, rels = G.construct_foot(A, B, C, name=None)
assert X is not None, 'construct_foot returned None for valid input'
# Expected foot is (1,0)
assert abs(X.x - 1.0) < 1e-8 and abs(X.y - 0.0) < 1e-8, f'Unexpected foot coords: {(X.x, X.y)}'
print('Foot:', X.name, X.x, X.y)
print_relations('Foot', rels)
# Draw the perpendicular from A to BC for visualization
G.draw_lines({'AX': line_from_points(A, X)})
assert_diagram_predicates_ok(G, "construct_foot")
G.show_construction('Foot of Perpendicular Test')
def test_construct_tangents(G):
# Circle centered at O with radius OB
O = geometric.Point('O', 0, 0)
B = geometric.Point('B', 1, 0)
# External point A (outside the circle)
A = geometric.Point('A', 2, 0)
# Draw base points and circle
G.draw_points({'O': O, 'B': B, 'A': A})
G.draw_circles({f"Circle_{O.name}{B.name}": (O.x, O.y, 1.0)})
# Construct tangents from A to circle(O,OB)
X, Y, rels = G.construct_tangents(A, O, B, None, None)
assert X is not None and Y is not None, 'construct_tangents returned None for valid input'
# For an external point we expect two distinct tangency points
assert X is not Y, 'Expected two distinct tangent points for an external point'
assert sum(1 for r in rels if isinstance(r, predicate.Perp)) == 2, 'Expected two Perp relations'
print('Tangents (external):', X.name, X.x, X.y, Y.name, Y.x, Y.y)
print_relations('Tangents (external)', rels)
# Show the construction for manual inspection
assert_diagram_predicates_ok(G, "construct_tangents")
G.show_construction('Tangents Construction (external)')
def test_construct_tangents2(G):
# Circle centered at O with radius OB
O = geometric.Point('O', 0, 0)
B = geometric.Point('B', 1, 0)
# Now test the on-circle case: point A_on lies on the circle -> single tangency
A_on = geometric.Point('A_on', 0, 1)
G.draw_points({'O': O, 'B': B, 'A_on': A_on})
G.draw_circles({f"Circle_{O.name}{B.name}": (O.x, O.y, 1.0)})
X2, Y2, rels2 = G.construct_tangents(A_on, O, B, None, None)
# When A lies on the circle, we expect a single tangency point (X2 is Y2)
assert X2 is Y2, 'Expected a single tangent point when A lies on the circle'
# do not add a Perp predicate in this degenerate-on-circle case;
# only a Cong relation (equal radii) is expected.
assert isinstance(rels2, list) and len(rels2) == 1 and isinstance(rels2[0], predicate.Cong), \
'Expected a single Cong relation for on-circle tangency'
print('Tangents (on-circle):', X2.name, X2.x, X2.y)
print_relations('Tangents (on-circle)', rels2)
assert_diagram_predicates_ok(G, "construct_tangents2")
G.show_construction('Tangents Construction (on-circle)')
def test_construct_on_dia(G):
# Construct a point X on the circle with diameter AB so that AX ⟂ XB
A = geometric.Point('A', 0, 0)
B = geometric.Point('B', 4, 0)
# Draw base points and AB
G.draw_points({'A': A, 'B': B})
G.draw_lines({'AB': line_from_points(A, B)})
X, rels = G.construct_on_dia(A, B, name=None)
assert X is not None, 'construct_on_dia returned None for valid input'
# Expect a single Perp relation: perp A X X B
assert isinstance(rels, list) and len(rels) == 1 and isinstance(rels[0], predicate.Perp), 'Expected a single Perp relation'
# Numeric check: vector AX dot XB should be ~0 (perpendicular)
vax = (X.x - A.x, X.y - A.y)
vxb = (B.x - X.x, B.y - X.y)
dot = vax[0] * vxb[0] + vax[1] * vxb[1]
assert abs(dot) < 1e-8, f'AX not perpendicular to XB, dot={dot}'
print('On-diameter point:', X.name, X.x, X.y)
print_relations('On-diameter', rels)
# Draw the perpendicular segments for visualization
G.draw_lines({'AX': line_from_points(A, X), 'XB': line_from_points(X, B)})
assert_diagram_predicates_ok(G, "construct_on_dia")
G.show_construction('On-Diameter Construction')
def test_construct_mirror(G):
# Mirror A across midpoint B to get X so that B is midpoint of AX
A = geometric.Point('A', 1, 2)
B = geometric.Point('B', 3, 4)
G.draw_points({'A': A, 'B': B})
X, rels = G.construct_mirror(A, B, name=None)
assert X is not None, 'construct_mirror returned None for valid input'
# Expected coordinates: X = 2*B - A
expected_x = 2.0 * B.x - A.x
expected_y = 2.0 * B.y - A.y
assert abs(X.x - expected_x) < 1e-8 and abs(X.y - expected_y) < 1e-8, f'Unexpected mirror coords: {(X.x, X.y)}'
# Expect a single Midp relation: midp B A X
assert isinstance(rels, list) and len(rels) == 1 and isinstance(rels[0], predicate.Midp), 'Expected a single Midp relation'
print('Mirror:', X.name, X.x, X.y)
print_relations('Mirror', rels)
# Draw the AX line for visualization (construct_mirror already draws it)
assert_diagram_predicates_ok(G, "construct_mirror")
G.show_construction('Mirror Construction')
def test_construct_reflection(G):
# Reflect A across line BC
A = geometric.Point('A', 1.0, 2.0)
B = geometric.Point('B', 0.0, 0.0)
C = geometric.Point('C', 2.0, 0.0)
# Draw base points and baseline
G.draw_points({'A': A, 'B': B, 'C': C})
G.draw_lines({'BC': line_from_points(B, C)})
X, Y, rels = G.construct_reflection(A, B, C, name1=None, name2=None)
assert X is not None and Y is not None, 'construct_reflection returned None for valid input'
# Numeric check: Y should be projection of A onto BC (y-coordinate = 0 for this baseline)
assert abs(Y.y - 0.0) < 1e-8, f'Y not on BC: {Y.y}'
# X should be symmetric: Y is midpoint of A and X -> X = 2*Y - A
expected_x = 2.0 * Y.x - A.x
expected_y = 2.0 * Y.y - A.y
assert abs(X.x - expected_x) < 1e-8 and abs(X.y - expected_y) < 1e-8, f'Unexpected reflection coords: {(X.x, X.y)}'
# Relations: expect midp Y A X, col Y B C, perp A X B C
assert isinstance(rels, list) and len(rels) == 3, 'Expected three relations from construct_reflection'
assert any(isinstance(r, predicate.Midp) for r in rels), 'Expected a Midp relation'
assert any(isinstance(r, predicate.Col) for r in rels), 'Expected a Col relation'
assert any(isinstance(r, predicate.Perp) for r in rels), 'Expected a Perp relation'
print('Reflection:', X.name, X.x, X.y, 'foot', Y.name, Y.x, Y.y)
print_relations('Reflection', rels)
# Draw the construction for manual inspection
assert_diagram_predicates_ok(G, "construct_reflection")
G.show_construction('Reflection Construction')
def test_construct_centroid(G):
# Centroid of triangle ABC
A = geometric.Point('A', 0.0, 0.0)
B = geometric.Point('B', 6.0, 0.0)
C = geometric.Point('C', 0.0, 6.0)
# Draw base points and triangle
G.draw_points({'A': A, 'B': B, 'C': C})
G.draw_lines({'AB': line_from_points(A, B), 'BC': line_from_points(B, C), 'CA': line_from_points(C, A)})
Gpt, X, Y, Z, rels = G.construct_centroid(A, B, C, name=None)
assert Gpt is not None, 'construct_centroid returned None for centroid'
# Numeric checks: midpoints
exp_Xx = (A.x + B.x) / 2.0
exp_Xy = (A.y + B.y) / 2.0
exp_Yx = (B.x + C.x) / 2.0
exp_Yy = (B.y + C.y) / 2.0
exp_Zx = (A.x + C.x) / 2.0
exp_Zy = (A.y + C.y) / 2.0
assert abs(X.x - exp_Xx) < 1e-8 and abs(X.y - exp_Xy) < 1e-8, 'Midpoint X incorrect'
assert abs(Y.x - exp_Yx) < 1e-8 and abs(Y.y - exp_Yy) < 1e-8, 'Midpoint Y incorrect'
assert abs(Z.x - exp_Zx) < 1e-8 and abs(Z.y - exp_Zy) < 1e-8, 'Midpoint Z incorrect'
# Centroid expected: average of vertices
exp_Gx = (A.x + B.x + C.x) / 3.0
exp_Gy = (A.y + B.y + C.y) / 3.0
assert abs(Gpt.x - exp_Gx) < 1e-8 and abs(Gpt.y - exp_Gy) < 1e-8, 'Centroid coordinates incorrect'
print('Centroid:', Gpt.name, Gpt.x, Gpt.y)
print_relations('Centroid', rels)
# Zoom to triangle + centroid for nicer view
try:
zoom_to_points(G, [A, B, C, Gpt], pad_frac=0.25)
except Exception:
G.ax.relim(); G.ax.autoscale_view()
assert_diagram_predicates_ok(G, "construct_centroid")
G.show_construction('Centroid Construction')
def test_construct_orthocenter(G):
# Right triangle at A: orthocenter should be at A
A = geometric.Point('A', 0.0, 0.0)
B = geometric.Point('B', 6.0, 0.0)
C = geometric.Point('C', 0.0, 6.0)
# Draw base points and triangle
G.draw_points({'A': A, 'B': B, 'C': C})
G.draw_lines({'AB': line_from_points(A, B), 'BC': line_from_points(B, C), 'CA': line_from_points(C, A)})
H, X, Y, Z, rels = G.construct_orthocenter(A, B, C, name=None)
assert H is not None, 'construct_orthocenter returned None for valid input'
# For this right triangle the orthocenter should coincide with A
assert abs(H.x - A.x) < 1e-8 and abs(H.y - A.y) < 1e-8, f'Unexpected orthocenter coords: {(H.x, H.y)}'
# Expected feet: X is projection of A onto BC -> (3,3); Y proj of B onto AC -> (0,0); Z proj of C onto AB -> (0,0)
assert abs(X.x - 3.0) < 1e-8 and abs(X.y - 3.0) < 1e-8, f'Unexpected foot X coords: {(X.x, X.y)}'
assert abs(Y.x - 0.0) < 1e-8 and abs(Y.y - 0.0) < 1e-8, f'Unexpected foot Y coords: {(Y.x, Y.y)}'
assert abs(Z.x - 0.0) < 1e-8 and abs(Z.y - 0.0) < 1e-8, f'Unexpected foot Z coords: {(Z.x, Z.y)}'
print('Orthocenter:', H.name, H.x, H.y)
print_relations('Orthocenter', rels)
# Zoom to triangle + orthocenter for nicer view
try:
zoom_to_points(G, [A, B, C, H], pad_frac=0.25)
except Exception:
G.ax.relim(); G.ax.autoscale_view()
assert_diagram_predicates_ok(G, "construct_orthocenter")
G.show_construction('Orthocenter Construction')
def test_construct_orthocenter2_edge(G):
# Right triangle at A: orthocenter should be at A
A = geometric.Point('A', 0.0, 0.0)
B = geometric.Point('B', 6.0, 0.0)
C = geometric.Point('C', 0.0, 6.0)
# Draw base points and triangle
G.draw_points({'A': A, 'B': B, 'C': C})
G.draw_lines({'AB': line_from_points(A, B), 'BC': line_from_points(B, C), 'CA': line_from_points(C, A)})
H, X, Y, Z, rels = G.construct_orthocenter2(A, B, C, name=None)
assert H is not None, 'construct_orthocenter returned None for valid input'
# For this right triangle the orthocenter should coincide with A
assert abs(H.x - A.x) < 1e-8 and abs(H.y - A.y) < 1e-8, f'Unexpected orthocenter coords: {(H.x, H.y)}'
# Expected feet: X is projection of A onto BC -> (3,3); Y proj of B onto AC -> (0,0); Z proj of C onto AB -> (0,0)
assert abs(X.x - 3.0) < 1e-8 and abs(X.y - 3.0) < 1e-8, f'Unexpected foot X coords: {(X.x, X.y)}'
assert abs(Y.x - 0.0) < 1e-8 and abs(Y.y - 0.0) < 1e-8, f'Unexpected foot Y coords: {(Y.x, Y.y)}'
assert abs(Z.x - 0.0) < 1e-8 and abs(Z.y - 0.0) < 1e-8, f'Unexpected foot Z coords: {(Z.x, Z.y)}'
print('Orthocenter:', H.name, H.x, H.y)
print_relations('Orthocenter', rels)
# Zoom to triangle + orthocenter for nicer view
try:
zoom_to_points(G, [A, B, C, H], pad_frac=0.25)
except Exception:
G.ax.relim(); G.ax.autoscale_view()
assert_diagram_predicates_ok(G, "construct_orthocenter2_edge")
G.show_construction('Orthocenter Construction')
def test_construct_orthocenter2(G):
# Non-right triangle where orthocenter is distinct from vertices
A = geometric.Point('A', 0.0, 0.0)
B = geometric.Point('B', 4.0, 0.0)
C = geometric.Point('C', 1.0, 3.0)
# Draw base points and triangle
G.draw_points({'A': A, 'B': B, 'C': C})
G.draw_lines({'AB': line_from_points(A, B), 'BC': line_from_points(B, C), 'CA': line_from_points(C, A)})
H, X, Y, Z, rels = G.construct_orthocenter2(A, B, C, name=None)
assert H is not None, 'construct_orthocenter2 returned None for valid input'
assert X is not None and Y is not None and Z is not None, 'construct_orthocenter2 did not return foot points'
# Expect perpendicular relations for the three feet
assert isinstance(rels, list) and any(isinstance(r, predicate.Perp) for r in rels), 'Expected Perp relations in result'
# Because triangle is non-right, orthocenter should be distinct and Col relations should appear
assert any(isinstance(r, predicate.Col) for r in rels), 'Expected Col relations in result'
print('Orthocenter2:', H.name, H.x, H.y)
print_relations('Orthocenter2', rels)
# Numeric check: X should lie on BC
ax = X.x - B.x
ay = X.y - B.y
cx = C.x - B.x
cy = C.y - B.y
cross = ax * cy - ay * cx
assert abs(cross) < 1e-8, f'Constructed foot X not on BC: cross={cross}'
try:
zoom_to_points(G, [A, B, C, H, X, Y, Z], pad_frac=0.25)
except Exception:
G.ax.relim(); G.ax.autoscale_view()
assert_diagram_predicates_ok(G, "construct_orthocenter2")
G.show_construction('Orthocenter2 Construction')
def test_construct_external_angle_bisector(G):
# Triangle for external bisector test
A = geometric.Point('A', 0.0, 0.0)
B = geometric.Point('B', 1.0, 0.0)
C = geometric.Point('C', 2.0, 1.0)
# Draw base points and triangle
G.draw_points({'A': A, 'B': B, 'C': C})
G.draw_lines({'AB': line_from_points(A, B), 'BC': line_from_points(B, C), 'CA': line_from_points(C, A)})
X, rels = G.construct_external_angle_bisector(A, B, C, pname=None)
assert X is not None, 'construct_external_angle_bisector returned None'
# Numeric check: X should lie on line AC (collinear)
ax = X.x - A.x
ay = X.y - A.y
cx = C.x - A.x
cy = C.y - A.y
cross = ax * cy - ay * cx
assert abs(cross) < 1e-8, f'Constructed external bisector point not on AC: cross={cross}'
# Relations should include Eqangle and Eqratio
assert isinstance(rels, list) and any(isinstance(r, predicate.Eqangle) for r in rels), 'Expected Eqangle in relations'
assert any(isinstance(r, predicate.Eqratio) for r in rels), 'Expected Eqratio in relations'
print('External bisector X:', X.name, X.x, X.y)
print_relations('External bisector', rels)
# Zoom and display
try:
zoom_to_points(G, [A, B, C, X], pad_frac=0.25)
except Exception:
G.ax.relim(); G.ax.autoscale_view()
assert_diagram_predicates_ok(G, "construct_external_angle_bisector")
G.show_construction('External Angle Bisector Construction')
def test_construct_excenter(G):
# Triangle for excenter test (excenter opposite A)
A = geometric.Point('A', 0.0, 0.0)
B = geometric.Point('B', 4.0, 0.0)
C = geometric.Point('C', 0.0, 3.0)
# Draw base points and triangle
G.draw_points({'A': A, 'B': B, 'C': C})
G.draw_lines({'AB': line_from_points(A, B), 'BC': line_from_points(B, C), 'CA': line_from_points(C, A)})
center, rels = G.construct_excenter(A, B, C, name=None)
assert center is not None, 'construct_excenter returned None'
# Relations should include Eqangle predicates (analogous to incenter)
assert isinstance(rels, list) and any(isinstance(r, predicate.Eqangle) for r in rels), 'Expected Eqangle relations in result'
# Verify by reconstructing via external bisectors at B and C
extB, _ = G.construct_external_angle_bisector(A, B, C, pname=None)
extC, _ = G.construct_external_angle_bisector(A, C, B, pname=None)
center2, _ = G.construct_intersect_lines(B, extB, C, extC, name=None)
assert center2 is not None, 'Failed to compute excenter by intersecting external bisectors'
# Numeric check: centers should match
assert abs(center.x - center2.x) < 1e-8 and abs(center.y - center2.y) < 1e-8, f'Excenter mismatch: {center} vs {center2}'
print('Excenter:', center.name if hasattr(center, 'name') else '<anon>', center.x, center.y)
print_relations('Excenter', rels)
try:
zoom_to_points(G, [A, B, C, center], pad_frac=0.25)
except Exception:
G.ax.relim(); G.ax.autoscale_view()
assert_diagram_predicates_ok(G, "construct_excenter")
G.show_construction('Excenter Construction')
def test_construct_excenter2(G):
# Triangle for excenter2 test (excenter opposite A)
A = geometric.Point('A', 0.0, 0.0)
B = geometric.Point('B', 4.0, 0.0)
C = geometric.Point('C', 0.0, 3.0)
# Draw base points and triangle
G.draw_points({'A': A, 'B': B, 'C': C})
G.draw_lines({'AB': line_from_points(A, B), 'BC': line_from_points(B, C), 'CA': line_from_points(C, A)})
J, X, Y, Z, rels = G.construct_excenter2(A, B, C, name=None)
assert J is not None, 'construct_excenter2 returned None'
assert X is not None and Y is not None and Z is not None, 'construct_excenter2 did not return tangency points'
# Expect Eqangle relations analogous to construct_excenter
assert isinstance(rels, list) and any(isinstance(r, predicate.Eqangle) for r in rels), 'Expected Eqangle relations in result'
# Expect perpendicular relations for the tangency feet
assert any(isinstance(r, predicate.Perp) for r in rels), 'Expected Perp relations for tangency points'
print('Excenter2:', J.name, J.x, J.y)
print_relations('Excenter2', rels)
try:
zoom_to_points(G, [A, B, C, J, X, Y, Z], pad_frac=0.25)
except Exception:
G.ax.relim(); G.ax.autoscale_view()
assert_diagram_predicates_ok(G, "construct_excenter2")
G.show_construction('Excenter2 Construction')
def test_construct_excenter2_generic(G):
"""Variation: scalene triangle with excenter opposite A and tangency feet."""
A = geometric.Point('A', -0.5, -0.5)
B = geometric.Point('B', 5.0, 0.5)
C = geometric.Point('C', 1.0, 4.0)
G.draw_points({'A': A, 'B': B, 'C': C})
G.draw_lines({'AB': line_from_points(A, B), 'BC': line_from_points(B, C), 'CA': line_from_points(C, A)})
J, X, Y, Z, rels = G.construct_excenter2(A, B, C, name=None)
assert J is not None and X is not None and Y is not None and Z is not None, 'construct_excenter2 (generic) returned None'
assert isinstance(rels, list) and any(isinstance(r, predicate.Eqangle) for r in rels), 'Expected Eqangle in generic excenter2'
assert any(isinstance(r, predicate.Perp) for r in rels), 'Expected Perp relations in generic excenter2'
print('Excenter2 (generic):', J.name, J.x, J.y)
print_relations('Excenter2 (generic)', rels)
try:
zoom_to_points(G, [A, B, C, J, X, Y, Z], pad_frac=0.25)
except Exception:
G.ax.relim(); G.ax.autoscale_view()
assert_diagram_predicates_ok(G, "construct_excenter2_generic")
G.show_construction('Excenter2 Construction (generic)')
def test_edgecase_intersect_at_vertex(G):
"""If two lines meet at an existing vertex, the intersection should return that same Point object."""
A = geometric.Point('A', 0.0, 0.0)
B = geometric.Point('B', 1.0, 0.0)
C = geometric.Point('C', 0.0, 1.0)
G.draw_points({'A': A, 'B': B, 'C': C})
# Lines AB and AC intersect at A
P, rels = G.construct_intersect_lines(A, B, A, C, name=None)
assert P is A, 'Expected intersection to return the existing vertex object A (identity)'
# Both collinear relations should include A
assert isinstance(rels, list) and all(isinstance(r, predicate.Col) for r in rels), 'Expected Col relations'
for r in rels:
pts = r.get_points()
assert any(p is A for p in pts), 'Col relation does not reference the canonical vertex A'
print('Edgecase intersect-at-vertex: P is A (identity)', P.name, P.x, P.y)
print_relations('Intersect-at-vertex', rels)
assert_diagram_predicates_ok(G, "edgecase_intersect_at_vertex")
def test_edgecase_orthocenter_identity(G):
"""For a right triangle at A, orthocenter should be the very same Point object A."""
A = geometric.Point('A', 0.0, 0.0)
B = geometric.Point('B', 3.0, 0.0)
C = geometric.Point('C', 0.0, 4.0)
G.draw_points({'A': A, 'B': B, 'C': C})
H, X, Y, Z, rels = G.construct_orthocenter(A, B, C, name=None)
assert H is A, 'Expected orthocenter to be the same object as vertex A for right triangle'
# Relations should reference A where appropriate (Perp involving A)
assert any(isinstance(r, predicate.Perp) for r in rels), 'Expected Perp relations'
perps = [r for r in rels if isinstance(r, predicate.Perp)]
assert any(A in r.get_points() or any(p is A for p in r.get_points()) for r in perps), 'Perp relations do not reference canonical A'
print('Edgecase orthocenter-identity: H is A (identity)', H.name, H.x, H.y)
print_relations('Orthocenter-identity', rels)
assert_diagram_predicates_ok(G, "edgecase_orthocenter_identity")
def main():
# Works
print("\nTesting construct_angle_bisector...")
G = GeometricConstructor(GeometricProblem())
test_construct_angle_bisector(G)
print("\nTesting construct_angle_bisector_generic...")
G = GeometricConstructor(GeometricProblem())
test_construct_angle_bisector_generic(G)
#Please verify the eqangle relations.
print("\nTesting construct_incenter...")
G = GeometricConstructor(GeometricProblem())
test_construct_incenter(G)
print("\nTesting construct_incenter_generic...")
G = GeometricConstructor(GeometricProblem())
test_construct_incenter_generic(G)
#Works
print("\nTesting construct_midpoint...")
G = GeometricConstructor(GeometricProblem())
test_construct_midpoint(G)
#Works
print("\nTesting construct_circle...")
G = GeometricConstructor(GeometricProblem())
test_construct_circle(G)
#Works
print("\nTesting intersect_lines...")
G = GeometricConstructor(GeometricProblem())
test_intersect_lines(G)
#Please verify the eqangle relations.
print("\nTesting construct_incenter2...")
G = GeometricConstructor(GeometricProblem())
test_construct_incenter2(G)
print("\nTesting construct_incenter2_generic...")
G = GeometricConstructor(GeometricProblem())
test_construct_incenter2_generic(G)
#Works
print("\nTesting construct_foot...")
G = GeometricConstructor(GeometricProblem())
test_construct_foot(G)
#Works
print("\nTesting construct_tangents...")
G = GeometricConstructor(GeometricProblem())
test_construct_tangents(G)
#Works
print("\nTesting construct_tangents2...")
G = GeometricConstructor(GeometricProblem())
test_construct_tangents2(G)
#Works
print("\nTesting construct_on_dia...")
G = GeometricConstructor(GeometricProblem())
test_construct_on_dia(G)
#Works (note coll is implied)
print("\nTesting construct_mirror...")
G = GeometricConstructor(GeometricProblem())
test_construct_mirror(G)
#Works
print("\nTesting construct_reflection...")
G = GeometricConstructor(GeometricProblem())
test_construct_reflection(G)
#Looks OK
print("\nTesting construct_centroid...")
G = GeometricConstructor(GeometricProblem())
test_construct_centroid(G)
# Works
print("\nTesting construct_orthocenter...")
G = GeometricConstructor(GeometricProblem())
test_construct_orthocenter(G)
# Works
print("\nTesting construct_orthocenter2_edge...")
G = GeometricConstructor(GeometricProblem())
test_construct_orthocenter2_edge(G)
# Works (?)
print("\nTesting construct_orthocenter2...")
G = GeometricConstructor(GeometricProblem())
test_construct_orthocenter2(G)
# Unchecked
print("\nTesting construct_external_angle_bisector...")
G = GeometricConstructor(GeometricProblem())
test_construct_external_angle_bisector(G)
# Looks ok?
print("\nTesting construct_excenter...")
G = GeometricConstructor(GeometricProblem())
test_construct_excenter(G)
# Looks ok?
print("\nTesting construct_excenter2...")
G = GeometricConstructor(GeometricProblem())
test_construct_excenter2(G)
print("\nTesting construct_excenter2_generic...")
G = GeometricConstructor(GeometricProblem())
test_construct_excenter2_generic(G)
if __name__ == '__main__':
main()