-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
1095 lines (962 loc) · 45 KB
/
core.py
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
"""
Core module.
Normally, do not add new construction methods here, do this in scene.py instead.
"""
from enum import Enum, auto, unique
import itertools
import re
import sympy as sp
from typing import List
from .figure import Figure
from .reason import Reason
from .util import LazyComment, Comment, divide
class CoreScene:
layers = ('user', 'auxiliary', 'invisible')
@staticmethod
def layers_by(max_layer):
return CoreScene.layers[0:CoreScene.layers.index(max_layer) + 1]
class Object:
"""
Common ancestor for all geometric objects like point, line, circle
"""
def __init__(self, scene, **kwargs):
assert isinstance(scene, CoreScene)
label = kwargs.get('label')
if label:
assert scene.get(label) is None, 'Object with label `%s` already exists' % label
else:
pattern = self.__class__.prefix + '%d'
for index in itertools.count():
label = pattern % index
if scene.get(label) is None:
self.label = label
self.auto_label = True
break
self.layer = kwargs.get('layer', 'user')
assert self.layer in CoreScene.layers
self.extra_labels = set()
self.scene = scene
self.__dict__.update(kwargs)
scene.add(self)
def with_extra_args(self, **kwargs):
if self.scene.is_frozen:
return self
layer = kwargs.get('layer', 'user')
if self.layer not in CoreScene.layers_by(layer):
self.layer = layer
for key in kwargs:
if key == 'layer':
continue
value = kwargs[key]
if key == 'label' and value and value != self.label:
if hasattr(self, 'auto_label'):
self.label = value
delattr(self, 'auto_label')
else:
self.extra_labels.add(value)
elif not hasattr(self, key):
self.__dict__[key] = value
return self
@property
def name(self):
return self.label
def __str__(self):
return self.name
@property
def description(self):
dct = {}
for key in ('layer', 'extra_labels', 'all_points', 'comment'):
value = self.__dict__.get(key)
if value is None:
continue
if isinstance(value, Enum):
dct[key] = value.name
elif isinstance(value, CoreScene.Object):
dct[key] = value.label
elif isinstance(value, (list, tuple, set)):
if value:
dct[key] = [elt.label if isinstance(elt, CoreScene.Object) else str(elt) for elt in value]
else:
dct[key] = str(value)
if self.name == self.label:
return '%s %s %s' % (self.__class__.__name__, self, dct)
else:
return '%s %s %s %s' % (self.__class__.__name__, self.label, self.name, dct)
class Point(Object, Figure):
prefix = 'Pt_'
class Origin(Enum):
free = auto()
translated = auto()
perp = auto()
line = auto()
circle = auto()
line_x_line = auto()
circle_x_line = auto()
circle_x_circle = auto()
def __init__(self, scene, origin, **kwargs):
assert isinstance(origin, CoreScene.Point.Origin), 'origin must be a Point.Origin, not %s' % type(origin)
CoreScene.Object.__init__(self, scene, origin=origin, **kwargs)
self.__vectors = {}
self.__perpendiculars = {}
def translated_point(self, vector, coef=1, **kwargs):
self.scene.assert_vector(vector)
if coef == 0:
return self
if coef == 1 and vector.start == self:
return vector.end
if coef == -1 and vector.end == self:
return vector.start
for pt in self.scene.points():
if pt.origin == CoreScene.Point.Origin.translated and pt.base == self and pt.delta == vector and pt.coef == coef:
return pt
if 'comment' not in kwargs:
kwargs = dict(kwargs)
if coef == 1:
pattern = 'translation of $%{point:pt}$ by vector $%{vector:vector}$'
else:
pattern = 'translation of $%{point:pt}$ by vector $%{multiplier:coef} %{vector:vector}$'
kwargs['comment'] = Comment(
pattern,
{'pt': self, 'coef': coef, 'vector': vector}
)
new_point = CoreScene.Point(
self.scene,
CoreScene.Point.Origin.translated,
base=self, delta=vector, coef=coef, **kwargs
)
if self in {vector.start, vector.end}:
new_point.collinear_constraint(vector.start, vector.end)
if coef > 0:
self.vector(new_point).parallel_constraint(vector, guaranteed=True)
else:
new_point.vector(self).parallel_constraint(vector, guaranteed=True)
self.segment(new_point).ratio_constraint(vector.as_segment, sp.Abs(coef), guaranteed=True)
return new_point
def symmetric_point(self, centre, **kwargs):
symmetric = CoreScene.Point(
self.scene, CoreScene.Point.Origin.translated,
base=centre, delta=self.vector(centre), coef=1, **kwargs
)
symmetric.collinear_constraint(self, centre, guaranteed=True)
from .property import MiddleOfSegmentProperty
self.scene.add_property(MiddleOfSegmentProperty(centre, self.segment(symmetric)))
return symmetric
def perpendicular_line(self, line, **kwargs):
"""
Constructs a line through the point, perpendicular to the given line.
"""
self.scene.assert_line(line)
existing = self.__perpendiculars.get(line)
if existing:
return existing.with_extra_args(**kwargs)
new_point = CoreScene.Point(
self.scene,
CoreScene.Point.Origin.perp,
point=self, line=line,
layer='invisible'
)
if 'comment' not in kwargs:
kwargs = dict(kwargs)
kwargs['comment'] = Comment(
'perpendicular from $%{point:pt}$ to $%{line:line}$',
{'pt': self, 'line': line}
)
new_line = self.line_through(new_point, **kwargs)
if self not in line:
crossing = new_line.intersection_point(line, layer='auxiliary', comment=Comment(
'foot of the perpendicular from $%{point:pt}$ to $%{line:line}$',
{'pt': self, 'line': line}
))
line.perpendicular_constraint(new_line, guaranteed=True)
self.__perpendiculars[line] = new_line
return new_line
def line_through(self, point, **kwargs):
self.scene.assert_point(point)
assert self != point, 'Cannot create a line by a single point'
self.not_equal_constraint(point)
for existing in self.scene.lines():
if self in existing and point in existing:
return existing.with_extra_args(**kwargs)
if 'comment' not in kwargs:
kwargs = dict(kwargs)
kwargs['comment'] = Comment(
'Line through $%{point:pt0}$ and $%{point:pt1}$',
{'pt0': self, 'pt1': point}
)
line = CoreScene.Line(self.scene, point0=self, point1=point, **kwargs)
if not self.scene.is_frozen:
for cnstr in self.scene.constraints(Constraint.Kind.collinear):
if len([pt for pt in line.all_points if pt in cnstr.params]) == 2:
for pt in cnstr.params:
if pt not in line.all_points:
line.all_points.append(pt)
return line
def circle_through(self, point, **kwargs):
if 'comment' not in kwargs:
kwargs = dict(kwargs)
kwargs['comment'] = Comment(
'Circle with centre $%{point:centre}$ through $%{point:pt}$',
{'centre': self, 'pt': point}
)
return self.circle_with_radius(self.segment(point), **kwargs)
def circle_with_radius(self, radius, **kwargs):
self.scene.assert_segment(radius)
assert radius.points[0] != radius.points[1], 'Cannot create a circle of zero radius'
if 'comment' not in kwargs:
kwargs = dict(kwargs)
kwargs['comment'] = Comment(
'Circle with centre $%{point:centre}$ with radius $%{segment:radius}$',
{'centre': self, 'radius': radius}
)
return CoreScene.Circle(
self.scene, centre=self, radius=radius, **kwargs
)
def vector(self, point):
assert self != point, 'Cannot create vector from a single point'
vec = self.__vectors.get(point)
if vec is None:
vec = CoreScene.Vector(self, point)
self.__vectors[point] = vec
return vec
def segment(self, point):
assert self != point, 'Cannot create segment from a single point'
return self.scene._get_segment(self, point)
def angle(self, point0, point1):
assert point0 != point1, 'Angle endpoints should be different'
return self.scene._get_angle(self.vector(point0), self.vector(point1))
def belongs_to(self, line_or_circle):
self.scene.assert_line_or_circle(line_or_circle)
if not self.scene.is_frozen and self not in line_or_circle.all_points:
line_or_circle.all_points.append(self)
def not_equal_constraint(self, A, **kwargs):
"""
The current point does not coincide with A.
"""
if self.scene.is_frozen:
return
for cnstr in self.scene.constraints(Constraint.Kind.not_equal):
if set(cnstr.params) == {self, A}:
cnstr.update(kwargs)
return
self.scene.constraint(Constraint.Kind.not_equal, self, A, **kwargs)
def not_collinear_constraint(self, A, B, **kwargs):
"""
The current point is not collinear with A and B.
"""
for cnstr in self.scene.constraints(Constraint.Kind.not_collinear):
if set(cnstr.params) == {self, A, B}:
cnstr.update(kwargs)
return
self.scene.constraint(Constraint.Kind.not_collinear, self, A, B, **kwargs)
self.not_equal_constraint(A, guaranteed=True, **kwargs)
self.not_equal_constraint(B, guaranteed=True, **kwargs)
A.not_equal_constraint(B, guaranteed=True, **kwargs)
def collinear_constraint(self, A, B, **kwargs):
"""
The current point is collinear with A and B.
"""
cnstr = self.scene.constraint(Constraint.Kind.collinear, self, A, B, **kwargs)
if not self.scene.is_frozen:
for line in self.scene.lines():
if len([pt for pt in line.all_points if pt in cnstr.params]) == 2:
for pt in cnstr.params:
if pt not in line.all_points:
line.all_points.append(pt)
return cnstr
def distance_constraint(self, A, distance, **kwargs):
"""
Distance to the point A equals to the given distance.
The given distance must be a non-negative number
"""
if isinstance(A, str):
A = self.scene.get(A)
return self.segment(A).length_constraint(distance, **kwargs)
def opposite_side_constraint(self, point, line, **kwargs):
"""
The current point lies on the opposite side to the line than the given point.
"""
if isinstance(point, CoreScene.Line) and isinstance(line, CoreScene.Point):
point, line = line, point
for cnstr in self.scene.constraints(Constraint.Kind.opposite_side):
if line == cnstr.params[2] and set(cnstr.params[0:2]) == {self, point}:
cnstr.update(kwargs)
return
#self.not_collinear_constraint(line.point0, line.point1, **kwargs)
#point.not_collinear_constraint(line.point0, line.point1, **kwargs)
self.scene.constraint(Constraint.Kind.opposite_side, self, point, line, **kwargs)
def same_side_constraint(self, point, line, **kwargs):
"""
The point lies on the same side to the line as the given point.
"""
if isinstance(point, CoreScene.Line) and isinstance(line, CoreScene.Point):
point, line = line, point
for cnstr in self.scene.constraints(Constraint.Kind.same_side):
if line == cnstr.params[2] and set(cnstr.params[0:2]) == {self, point}:
cnstr.update(kwargs)
return
self.not_collinear_constraint(line.point0, line.point1, **kwargs)
point.not_collinear_constraint(line.point0, line.point1, **kwargs)
self.scene.constraint(Constraint.Kind.same_side, self, point, line, **kwargs)
def same_direction_constraint(self, A, B, **kwargs):
"""
Vectors (self, A) and (self, B) have the same direction
"""
for cnstr in self.scene.constraints(Constraint.Kind.same_direction):
if self == cnstr.params[0] and set(cnstr.params[1:3]) == {A, B}:
cnstr.update(kwargs)
return
self.not_equal_constraint(A)
self.not_equal_constraint(B)
A.belongs_to(self.line_through(B, layer='auxiliary'))
self.scene.constraint(Constraint.Kind.same_direction, self, A, B, **kwargs)
def inside_constraint(self, obj, **kwargs):
"""
The point is inside the object (angle or segment)
"""
if isinstance(obj, CoreScene.Segment):
self.collinear_constraint(*obj.points, **kwargs)
self.scene.constraint(Constraint.Kind.inside_segment, self, obj, **kwargs)
elif isinstance(obj, CoreScene.Angle) and obj.vertex:
self.scene.constraint(Constraint.Kind.inside_angle, self, obj, **kwargs)
else:
assert False, 'Cannot declare point lying inside %s' % obj
def inside_triangle_constraint(self, triangle, **kwargs):
"""
The point is inside the triangle
"""
triangle.points[0].not_collinear_constraint(*triangle.points[1:])
if 'comment' not in kwargs:
kwargs = dict(kwargs)
kwargs['comment'] = Comment(
'point $%{point:pt}$ is inside $%{triangle:triangle}$',
{'pt': self, 'triangle': triangle}
)
for angle in triangle.angles:
self.inside_constraint(angle, **kwargs)
from .property import SameOrOppositeSideProperty
for vertex, side in zip(triangle.points, triangle.sides):
self.scene.add_property(SameOrOppositeSideProperty(side, vertex, self, True))
class Line(Object):
prefix = 'Ln_'
def __init__(self, scene, **kwargs):
CoreScene.Object.__init__(self, scene, **kwargs)
self.all_points = [self.point0, self.point1]
@property
def name(self):
if hasattr(self, 'auto_label') and self.auto_label:
for points in itertools.combinations(self.all_points, 2):
if points[0].layer == 'user' and points[1].layer == 'user':
return '(%s %s)' % (points[0].name, points[1].name)
return super().name
def free_point(self, **kwargs):
if 'comment' not in kwargs:
kwargs = dict(kwargs)
kwargs['comment'] = Comment('point on line $%{line:line}$', {'line': self})
point = CoreScene.Point(self.scene, CoreScene.Point.Origin.line, line=self, **kwargs)
point.belongs_to(self)
return point
def intersection_point(self, obj, **kwargs):
"""
Creates an intersection point of the line and given object (line or circle).
Requires a constraint for determinate placement if the object a circle
"""
self.scene.assert_line_or_circle(obj)
assert self != obj, 'The line does not cross itself'
if 'comment' not in kwargs:
kwargs = dict(kwargs)
kwargs['comment'] = LazyComment('crossing point of %s and %s', self.label, obj.label)
if isinstance(obj, CoreScene.Circle):
crossing = CoreScene.Point(
self.scene,
CoreScene.Point.Origin.circle_x_line,
circle=obj, line=self, **kwargs
)
else:
existing = next((pt for pt in self.all_points if pt in obj), None)
if existing:
return existing.with_extra_args(**kwargs)
crossing = CoreScene.Point(
self.scene,
CoreScene.Point.Origin.line_x_line,
line0=self, line1=obj, **kwargs
)
crossing.belongs_to(self)
crossing.belongs_to(obj)
return crossing
def perpendicular_constraint(self, other, **kwargs):
"""
self ⟂ other
"""
self.point0.segment(self.point1).perpendicular_constraint(other.point0.segment(other.point1), **kwargs)
def __contains__(self, obj):
if obj is None:
return False
if isinstance(obj, CoreScene.Point):
return obj in self.all_points
if isinstance(obj, CoreScene.Vector):
return obj.start in self.all_points and obj.end in self.all_points
assert False, 'Operator not defined for %s and Line' % type(obj)
class Circle(Object):
prefix = 'Circ_'
def __init__(self, scene, **kwargs):
CoreScene.Object.__init__(self, scene, **kwargs)
self.all_points = []
if not scene.is_frozen:
if self.centre == self.radius.points[0]:
self.all_points.append(self.radius.points[1])
elif self.centre == self.radius.points[1]:
self.all_points.append(self.radius.points[0])
def centre_point(self, **kwargs):
return self.centre.with_extra_args(**kwargs)
def free_point(self, **kwargs):
if 'comment' not in kwargs:
kwargs = dict(kwargs)
kwargs['comment'] = LazyComment('point on circle %s', self.label)
point = CoreScene.Point(self.scene, CoreScene.Point.Origin.circle, circle=self, **kwargs)
point.belongs_to(self)
return point
def intersection_point(self, obj, **kwargs):
"""
Creates an intersection point of the circle and given object (line or circle).
Requires a constraint for determinate placement
"""
self.scene.assert_line_or_circle(obj)
assert self != obj, 'The circle does not cross itself'
if 'comment' not in kwargs:
kwargs = dict(kwargs)
kwargs['comment'] = LazyComment('crossing point of %s and %s', self.label, obj.label)
if isinstance(obj, CoreScene.Circle):
crossing = CoreScene.Point(
self.scene,
CoreScene.Point.Origin.circle_x_circle,
circle0=self, circle1=obj, **kwargs
)
else:
crossing = CoreScene.Point(
self.scene,
CoreScene.Point.Origin.circle_x_line,
circle=self, line=obj, **kwargs
)
crossing.belongs_to(self)
crossing.belongs_to(obj)
return crossing
def __contains__(self, obj):
if obj is None:
return False
if isinstance(obj, CoreScene.Point):
return obj in self.all_points
assert False, 'Operator not defined for %s and Circle' % type(obj)
class Vector(Figure):
def __init__(self, start, end):
assert isinstance(start, CoreScene.Point)
assert isinstance(end, CoreScene.Point)
assert start.scene == end.scene
self.start = start
self.end = end
self.points = (start, end)
self.__segment = None
@property
def as_segment(self):
if self.__segment is None:
self.__segment = self.start.segment(self.end)
return self.__segment
def angle(self, other):
angle = self.scene._get_angle(self, other)
if not self.scene.is_frozen:
for vec in (self, other):
for cnstr in vec.scene.constraints(Constraint.Kind.not_equal):
if set(cnstr.params) == set(vec.points):
break
else:
vec.as_segment.non_zero_length_constraint(comment=Comment(
'$%{vector:side}$ is side of $%{angle:angle}$',
{'side': vec, 'angle': angle}
))
return angle
@property
def scene(self):
return self.start.scene
@property
def reversed(self):
return self.end.vector(self.start)
def parallel_constraint(self, vector, **kwargs):
"""
Self and vector have the same direction.
This constraint also fulfilled if at least one of the vectors has zero length.
"""
assert isinstance(vector, CoreScene.Vector)
assert self.scene == vector.scene
return self.scene.constraint(Constraint.Kind.parallel_vectors, self, vector, **kwargs)
def __str__(self):
return '%s %s' % (self.start, self.end)
def _get_segment(self, point0, point1):
assert isinstance(point0, CoreScene.Point)
assert isinstance(point1, CoreScene.Point)
assert point0.scene == self
assert point1.scene == self
key = frozenset([point0, point1])
#key = (point0, point1)
segment = self.__segments.get(key)
if segment is None:
segment = CoreScene.Segment(point0, point1)
self.__segments[key] = segment
return segment
class Segment(Figure):
def __init__(self, pt0, pt1):
self.points = (pt0, pt1)
self.point_set = frozenset(self.points)
self.__middle_point = None
@property
def scene(self):
return self.points[0].scene
def middle_point(self, **kwargs):
"""
Constructs middle point of the segment
"""
if self.__middle_point:
return self.__middle_point.with_extra_args(**kwargs)
delta = self.points[0].vector(self.points[1])
coef = divide(1, 2)
for pt in self.scene.points():
if pt.origin == CoreScene.Point.Origin.translated:
if pt.base == self.points[0] and pt.delta == delta and pt.coef == coef:
middle = pt
break
if pt.base == self.points[1] and pt.delta == delta.reversed and pt.coef == coef:
middle = pt
break
else:
middle = CoreScene.Point(
self.scene, CoreScene.Point.Origin.translated,
base=self.points[0], delta=delta, coef=coef, **kwargs
)
middle.collinear_constraint(*self.points, guaranteed=True)
from .property import MiddleOfSegmentProperty
self.scene.add_property(MiddleOfSegmentProperty(middle, self))
self.__middle_point = middle
return middle
def free_point(self, **kwargs):
if 'comment' not in kwargs:
kwargs = dict(kwargs)
kwargs['comment'] = Comment('point on segment $%{segment:seg}$', {'seg': self})
point = self.line_through(layer='auxiliary').free_point(**kwargs)
point.inside_constraint(self)
return point
def line_through(self, **kwargs):
return self.points[0].line_through(self.points[1], **kwargs)
def perpendicular_bisector_line(self, **kwargs):
"""
Perpendicular bisector
"""
middle = self.middle_point(layer='auxiliary')
line = self.line_through(layer='auxiliary')
if kwargs.get('comment') is None:
kwargs = dict(kwargs)
kwargs['comment'] = Comment(
'perpendicular bisector of $%{segment:seg}$',
{'seg': self}
)
bisector = middle.perpendicular_line(line, **kwargs)
comment=Comment(
'$%{line:bisector}$ is the perpendicular bisector of $%{segment:seg}$',
{'bisector': bisector, 'seg': self}
)
bisector.perpendicular_constraint(line, comment=comment)
return bisector
def perpendicular_constraint(self, other, **kwargs):
"""
self ⟂ other
"""
for cnstr in self.scene.constraints(Constraint.Kind.perpendicular):
if set(cnstr.params) == {self, other}:
cnstr.update(kwargs)
return
self.scene.constraint(Constraint.Kind.perpendicular, self, other, **kwargs)
def ratio_constraint(self, segment, coef, **kwargs):
"""
|self| == |segment| * coef
coef is a non-zero number
"""
assert isinstance(segment, CoreScene.Segment)
assert self.scene == segment.scene
assert coef != 0
for cnstr in self.scene.constraints(Constraint.Kind.length_ratio):
if set(cnstr.params) == {self, segment, coef}:
cnstr.update(kwargs)
return
comment = kwargs.get('comment')
if not comment:
kwargs = dict(kwargs)
if coef == 1:
pattern = '$|%{segment:seg0}| = |%{segment:seg1}|$'
else:
pattern = '$|%{segment:seg0}| = %{multiplier:coef} |%{segment:seg1}|$'
kwargs['comment'] = Comment(
pattern, {'seg0': self, 'seg1': segment, 'coef': coef}
)
return self.scene.constraint(Constraint.Kind.length_ratio, self, segment, coef, **kwargs)
def congruent_constraint(self, segment, **kwargs):
"""
|self| == |vector|
"""
self.ratio_constraint(segment, 1, **kwargs)
def non_zero_length_constraint(self, **kwargs):
"""
|self| > 0
"""
self.points[0].not_equal_constraint(self.points[1], **kwargs)
def length_constraint(self, length, **kwargs):
"""
|self| == length
"""
if length > 0:
self.non_zero_length_constraint(**kwargs)
#TODO: equal_constraint otherwise?
self.scene.constraint(Constraint.Kind.distance, self, length, **kwargs)
def __str__(self):
return '%s %s' % self.points
def _get_angle(self, vector0, vector1):
assert isinstance(vector0, CoreScene.Vector)
assert isinstance(vector1, CoreScene.Vector)
assert vector0.scene == self
assert vector1.scene == self
key = frozenset([vector0, vector1])
angle = self.__angles.get(key)
if angle is None:
angle = CoreScene.Angle(vector0, vector1)
if angle.vertex is None and angle.pseudo_vertex:
if angle.vectors[0].end == angle.vectors[1].start:
from .property import SumOfTwoAnglesProperty
#TODO add comment
self.add_property(SumOfTwoAnglesProperty(
angle, angle.vectors[0].reversed.angle(angle.vectors[1]), 180
))
elif angle.vectors[0].start == angle.vectors[1].end:
from .property import SumOfTwoAnglesProperty
#TODO add comment
self.add_property(SumOfTwoAnglesProperty(
angle, angle.vectors[0].angle(angle.vectors[1].reversed), 180
))
elif angle.vectors[0].end == angle.vectors[1].end:
#TODO vertical angles
pass
self.__angles[key] = angle
return angle
class Angle(Figure):
def __init__(self, vector0, vector1):
assert vector0 != vector1 and vector0 != vector1.reversed
self.vectors = (vector0, vector1)
self.vertex = vector0.start if vector0.start == vector1.start else None
if self.vertex:
self.pseudo_vertex = self.vertex
else:
self.pseudo_vertex = next((p for p in vector0.points if p in vector1.points), None)
self.point_set = frozenset([*vector0.points, *vector1.points])
self.__bisector = None
@property
def scene(self):
return self.vectors[0].scene
@property
def endpoints(self):
assert self.vertex, 'Cannot locate endpoints of angle with no vertex'
return (self.vectors[0].end, self.vectors[1].end)
def bisector_line(self, **kwargs):
assert self.pseudo_vertex, 'Cannot construct bisector of angle %s with no vertex' % self
if self.__bisector:
return self.__bisector.with_extra_args(**kwargs)
v = self.pseudo_vertex
vec0 = self.vectors[0]
e0 = vec0.end if v == vec0.start else v.translated_point(vec0, layer='invisible')
vec1 = self.vectors[1]
e1 = vec1.end if v == vec1.start else v.translated_point(vec1, layer='invisible')
circle = v.circle_through(e0, layer='invisible')
line = v.line_through(e1, layer='invisible')
X = circle.intersection_point(line, layer='invisible')
v.same_direction_constraint(X, e1)
Y = X.translated_point(v.vector(e0), layer='invisible')
self.point_on_bisector_constraint(Y, guaranteed=True)
if kwargs.get('comment') is None:
kwargs = dict(kwargs)
kwargs['comment'] = Comment('bisector of $%{angle:angle}$', {'angle': self})
self.__bisector = v.line_through(Y, **kwargs)
return self.__bisector
def point_on_bisector_constraint(self, point, **kwargs):
bisector = self.pseudo_vertex.vector(point)
if kwargs.get('comment') is None:
kwargs = dict(kwargs)
kwargs['comment'] = Comment(
'$%{ray:bisector}$ is the bisector of $%{angle:angle}$',
{'bisector': bisector, 'angle': self}
)
angle0 = self.vectors[0].angle(bisector)
angle1 = self.vectors[1].angle(bisector)
if self.vertex:
point.inside_constraint(self, **kwargs)
self.ratio_constraint(angle0, 2, **kwargs)
self.ratio_constraint(angle1, 2, **kwargs)
angle0.ratio_constraint(angle1, 1, **kwargs)
def ratio_constraint(self, angle, ratio, **kwargs):
# self = angle * ratio
self.scene.assert_angle(angle)
self.scene.constraint(Constraint.Kind.angles_ratio, self, angle, ratio, **kwargs)
def value_constraint(self, degree, **kwargs):
if kwargs.get('comment') is None:
kwargs = dict(kwargs)
kwargs['comment'] = Comment(
'$%{anglemeasure:angle} = %{degree:degree}$',
{'angle': self, 'degree': degree}
)
self.scene.constraint(Constraint.Kind.angle_value, self, degree, **kwargs)
def is_acute_constraint(self, **kwargs):
self.scene.constraint(Constraint.Kind.acute_angle, self, **kwargs)
def is_obtuse_constraint(self, **kwargs):
self.scene.constraint(Constraint.Kind.obtuse_angle, self, **kwargs)
def is_right_constraint(self, **kwargs):
self.vectors[0].as_segment.line_through().perpendicular_constraint(
self.vectors[1].as_segment.line_through(),
**kwargs
)
def __str__(self):
if self.vertex:
return '\\angle %s %s %s' % (self.vectors[0].end, self.vertex, self.vectors[1].end)
return '\\angle(%s, %s)' % self.vectors
class Triangle(Figure):
def __init__(self, pt0, pt1, pt2):
self.points = (pt0, pt1, pt2)
self.__sides = None
self.__angles = None
self.__permutations = None
@property
def scene(self):
return self.points[0].scene
@property
def is_equilateral(self):
for cnstr in self.scene.constraints(Constraint.Kind.equilateral):
if set(cnstr.params[0].points) == set(self.points):
return True
# TODO: check implicit equilateral constraints, e.g. congruency of sides
return False
@property
def sides(self):
if self.__sides is None:
self.__sides = (
self.points[1].segment(self.points[2]),
self.points[0].segment(self.points[2]),
self.points[0].segment(self.points[1])
)
return self.__sides
@property
def angles(self):
if self.__angles is None:
self.__angles = (
self.points[0].angle(self.points[1], self.points[2]),
self.points[1].angle(self.points[0], self.points[2]),
self.points[2].angle(self.points[0], self.points[1])
)
return self.__angles
@property
def permutations(self):
if self.__permutations is None:
self.__permutations = (
(self.points[0], self.points[1], self.points[2]),
(self.points[0], self.points[2], self.points[1]),
(self.points[1], self.points[0], self.points[2]),
(self.points[1], self.points[2], self.points[0]),
(self.points[2], self.points[0], self.points[1]),
(self.points[2], self.points[1], self.points[0])
)
return self.__permutations
def __str__(self):
return '\\bigtriangleup %s %s %s' % self.points
class Polygon(Figure):
def __init__(self, *points):
self.points = tuple(points)
self.__sides = None
self.__angles = None
def __str__(self):
return ' '.join(['%s'] * len(self.points)) % self.points
@property
def scene(self):
return self.points[0].scene
@property
def sides(self):
if self.__sides is None:
pts = self.points
self.__sides = tuple(p0.segment(p1) for (p0, p1) in zip(pts, pts[1:] + (pts[0], )))
return self.__sides
@property
def angles(self):
if self.__angles is None:
pts = self.points + self.points[:2]
self.__angles = tuple(pts[i + 1].angle(pts[i], pts[i + 2]) for i in range(0, len(self.points)))
return self.__angles
def __init__(self):
self.__objects = []
self.validation_constraints = []
self.adjustment_constraints = []
self.__properties = set()
self.__frozen = False
self.__angles = {} # {vector, vector} => angle
self.__segments = {} # {point, point} => angle
def add_property(self, prop):
if prop not in self.__properties:
self.__properties.add(prop)
@property
def properties(self):
return list(self.__properties)
def constraint(self, kind, *args, **kwargs):
cns = Constraint(kind, self, *args, **kwargs)
if not self.__frozen:
if kind.stage == Stage.validation:
self.validation_constraints.append(cns)
else:
self.adjustment_constraints.append(cns)
return cns
def equilateral_constraint(self, triangle, **kwargs):
if 'comment' not in kwargs:
kwargs = dict(kwargs)
kwargs['comment'] = Comment(
'$%{triangle:equilateral}$ is equilateral',
{'equilateral': triangle}
)
self.constraint(Constraint.Kind.equilateral, triangle, **kwargs)
from .property import EquilateralTriangleProperty
self.add_property(EquilateralTriangleProperty(triangle))
def quadrilateral_constraint(self, A, B, C, D, **kwargs):
"""
ABDC is a quadrilateral.
I.e., the polygonal chain ABCD does not cross itself and contains no 180º angles.
"""
self.constraint(Constraint.Kind.quadrilateral, A, B, C, D, **kwargs)
def convex_polygon_constraint(self, *points, **kwargs):
"""
*points (in given order) is a convex polygon.
"""
assert len(points) > 3
self.constraint(Constraint.Kind.convex_polygon, points, **kwargs)
def points(self, max_layer='invisible'):
return [p for p in self.__objects if isinstance(p, CoreScene.Point) and p.layer in CoreScene.layers_by(max_layer)]
def lines(self, max_layer='invisible'):
return [l for l in self.__objects if isinstance(l, CoreScene.Line) and l.layer in CoreScene.layers_by(max_layer)]
def circles(self, max_layer='invisible'):
return [c for c in self.__objects if isinstance(c, CoreScene.Circle) and c.layer in CoreScene.layers_by(max_layer)]
def constraints(self, kind):
if kind.stage == Stage.validation:
return [cnstr for cnstr in self.validation_constraints if cnstr.kind == kind]
else:
return [cnstr for cnstr in self.adjustment_constraints if cnstr.kind == kind]
def assert_type(self, obj, *args):
assert isinstance(obj, args), 'Unexpected type %s' % type(obj)
assert obj.scene == self
def assert_point(self, obj):
self.assert_type(obj, CoreScene.Point)
def assert_line(self, obj):
self.assert_type(obj, CoreScene.Line)
def assert_line_or_circle(self, obj):
self.assert_type(obj, CoreScene.Line, CoreScene.Circle)
def assert_vector(self, obj):
self.assert_type(obj, CoreScene.Vector)
def assert_segment(self, obj):
self.assert_type(obj, CoreScene.Segment)
def assert_angle(self, obj):
self.assert_type(obj, CoreScene.Angle)
def free_point(self, **kwargs):
return CoreScene.Point(self, origin=CoreScene.Point.Origin.free, **kwargs)
def existing_line(self, point0, point1):
for cnstr in self.constraints(Constraint.Kind.not_equal):
if {point0, point1} == {*cnstr.params}:
break
else:
return None
for line in self.lines():
if point0 in line and point1 in line:
return line
return None
def add(self, obj: Object):
if not self.__frozen:
self.__objects.append(obj)