-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_polygon.py
2183 lines (1701 loc) · 70.6 KB
/
test_polygon.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
import unittest
import random
from pygame import Vector2, Vector3, Rect
import geometry
from geometry import Polygon, Circle, Line, regular_polygon
import math
p1 = (12.0, 12.0)
p2 = (32.0, 43.0)
p3 = (22.0, 4.0)
p4 = (332.0, 64.0)
_some_vertices = [(10.0, 10.0), (20.0, 20.0), (30.0, 10.0)]
def _rotate_vertices(poly: Polygon, angle: float, center=None):
"""Rotates the vertices of a polygon by the given angle."""
angle = math.radians(angle)
rotated_vertices = []
cos_a = math.cos(angle) - 1
sin_a = math.sin(angle)
cx = poly.centerx if center is None else center[0]
cy = poly.centery if center is None else center[1]
for vertex in poly.vertices:
dx = vertex[0] - cx
dy = vertex[1] - cy
rotated_vertices.append(
(
vertex[0] + dx * cos_a - dy * sin_a,
vertex[1] + dx * sin_a + dy * cos_a,
)
)
return rotated_vertices
def _calculate_bounding_box(vertices) -> Rect:
"""Calculates the bounding box of a polygon."""
min_x = min(vertices, key=lambda x: x[0])[0]
min_y = min(vertices, key=lambda x: x[1])[1]
max_x = max(vertices, key=lambda x: x[0])[0]
max_y = max(vertices, key=lambda x: x[1])[1]
return Rect(
math.floor(min_x),
math.floor(min_y),
math.ceil(max_x - min_x + 1),
math.ceil(max_y - min_y + 1),
)
def _calculate_center(poly: Polygon):
"""Calculates the center of a polygon."""
x = 0
y = 0
for vertex in poly.vertices:
x += vertex[0]
y += vertex[1]
return x / poly.verts_num, y / poly.verts_num
def _scale_polygon(vertices, num_verts, cx, cy, fac):
one_m_fac = 1 - fac
omf_cx = one_m_fac * cx
omf_cy = one_m_fac * cy
new_vertices = []
for i in range(num_verts):
x, y = vertices[i]
new_vertices.append((x * fac + omf_cx, y * fac + omf_cy))
return new_vertices
class PolygonTypeTest(unittest.TestCase):
def test_Construction_invalid_type(self):
"""Checks whether passing wrong types to the constructor
raises the appropriate errors
"""
invalid_types = (
None,
[],
"1",
"123",
(1,),
[1, 2, 3],
Vector2(1, 1),
[p1, p2, p3, 32],
[p1, p2, "(1, 1)"],
(p1, p2, "(1, 1)"),
(p1, p2, 32),
(p for p in [p1, p2, 32]),
(p for p in [p1, p2, "(1, 1)"]),
)
for value in invalid_types:
with self.assertRaises(TypeError):
po = Polygon(value)
def test_Construction_invalid_arguments_number(self):
"""Checks whether passing the wrong number of arguments to the constructor
raises the appropriate errors
"""
arguments = (([p1, p2, p3], [p1, p4, p3]),) # two args
# No args
with self.assertRaises(TypeError):
po = Polygon()
for arg_seq in arguments:
with self.assertRaises(TypeError):
po = Polygon(*arg_seq)
def test_construction_invalid_polygon(self):
"""Checks whether the constructor works correctly with invalid polygons"""
invalid_polygons = (
[],
[p1], # 1
[p1, p2], # 2
(p1,), # 1
(p1, p2), # 2
(p for p in []),
(p for p in [p1]), # generator
(p for p in [p1, p2]), # generator
)
for polygon in invalid_polygons:
with self.assertRaises(TypeError):
po = Polygon(polygon)
def test_construction_tuple(self):
"""Checks whether the constructor works correctly with tuples"""
po = Polygon((p1, p2, p3, p4))
self.assertEqual(po.vertices, [p1, p2, p3, p4])
def test_construction_list(self):
"""Checks whether the constructor works correctly with lists"""
po = Polygon([p1, p2, p3, p4])
po_2 = Polygon([p1, p2, p3])
self.assertEqual(po.vertices, [p1, p2, p3, p4])
self.assertEqual(po_2.vertices, [p1, p2, p3])
def test_construction_n_args(self):
"""Checks whether the constructor works correctly with n arguments"""
po = Polygon(p1, p2, p3, p4)
po_2 = Polygon(p1, p2, p3)
self.assertEqual(po.vertices, [p1, p2, p3, p4])
self.assertEqual(po_2.vertices, [p1, p2, p3])
def test_construction_polygon_attribute(self):
"""Ensures that you can construct a polygon from another object that has a
polygon attribute"""
# polygon attribute is a list of vertices
class PolygonObject:
def __init__(self, polygon):
self.polygon = polygon
po = PolygonObject([p1, p2, p3, p4])
po_2 = Polygon(po)
self.assertEqual(po_2.vertices, [p1, p2, p3, p4])
self.assertEqual(po_2.vertices, po.polygon)
# polygon attribute is a callable that returns a list of vertices
class PolygonObject1:
def __init__(self, polygon):
self._poly = polygon
def polygon(self):
return self._poly
po = PolygonObject1([p1, p2, p3, p4])
po_2 = Polygon(po)
self.assertEqual(po_2.vertices, [p1, p2, p3, p4])
# polygon attribute is a callable that returns a Polygon object
class PolygonObject2:
def __init__(self, polygon):
self._poly = polygon
def polygon(self):
return Polygon(self._poly)
po = PolygonObject2(Polygon([p1, p2, p3, p4]))
po_2 = Polygon(po)
self.assertEqual(po_2.vertices, po.polygon().vertices)
# polygon attribute is a polygon object
class PolygonObject3:
def __init__(self, polygon):
self.polygon = polygon
po = PolygonObject3(Polygon([p1, p2, p3, p4]))
po_2 = Polygon(po)
self.assertEqual(po_2.vertices, po.polygon.vertices)
def test_construction_frompolygon(self):
"""Checks whether the constructor works correctly with another polygon"""
po = Polygon([p1, p2, p3, p4])
po_2 = Polygon(po)
self.assertEqual(po_2.vertices, [p1, p2, p3, p4])
self.assertEqual(po_2.vertices, po.vertices)
def test_construction_generator(self):
"""Checks whether the constructor works correctly with a generator object"""
def generator():
yield p1
yield p2
yield p3
yield p4
po = Polygon(generator())
self.assertEqual(po.vertices, [p1, p2, p3, p4])
def test_construction_iterator(self):
"""Checks whether the constructor works correctly with an iterator object"""
class Iterator:
def __init__(self, points):
self.points = points
self.index = 0
def __next__(self):
if self.index >= len(self.points):
raise StopIteration
result = self.points[self.index]
self.index += 1
return result
def __iter__(self):
return self
it = Iterator([p1, p2, p3, p4])
po = Polygon(it)
self.assertEqual(po.vertices, [p1, p2, p3, p4])
def test_construction_iterable(self):
"""Checks whether the constructor works correctly with an object that implements the __iter__ method"""
it = iter([p1, p2, p3, p4])
po = Polygon(it)
self.assertEqual(po.vertices, [p1, p2, p3, p4])
def test_construction_generator_expression(self):
"""Checks whether the constructor works correctly with a generator expression"""
po = Polygon(p for p in [p1, p2, p3, p4])
self.assertEqual(po.vertices, [p1, p2, p3, p4])
def test_construction_generator_expression2(self):
"""Checks whether the constructor works correctly with a generator expression"""
po = Polygon((p for p in [p1, p2, p3, p4]))
self.assertEqual(po.vertices, [p1, p2, p3, p4])
def test_perimeter(self):
def get_perimeter(poly: geometry.Polygon) -> float:
"""Return the perimeter of the polygon."""
def distance(p1, p2):
return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)
perimeter = 0
vertices = poly.vertices
for i in range(len(vertices) - 1):
perimeter += distance(vertices[i], vertices[i + 1])
perimeter += distance(vertices[-1], vertices[0])
return perimeter
po = Polygon([p1, p2, p3, p4])
expected_perimeter = get_perimeter(po)
self.assertEqual(po.perimeter, expected_perimeter)
po = Polygon([10.0, 10.0], [20.0, 20.0], [40.0, 50.0], [70.0, 900.0])
expected_perimeter = get_perimeter(po)
self.assertEqual(po.perimeter, expected_perimeter)
po = Polygon((10.0, 45.0), (20.0, 4.5), (40.0, 77.45), (70.0, 900.0))
expected_perimeter = get_perimeter(po)
self.assertEqual(po.perimeter, expected_perimeter)
po = Polygon([[6.0, 12.0], [95.0, 634.3], [21.0, 21.0]])
expected_perimeter = get_perimeter(po)
self.assertEqual(po.perimeter, expected_perimeter)
po = Polygon(
[[6.0, 12.0], [95.0, 634.3], [21.0, 21.0], [42.0, 1.0], [9.0, 72.0]]
)
expected_perimeter = get_perimeter(po)
self.assertEqual(po.perimeter, expected_perimeter)
def test_subscript(self):
"""Checks whether reassigning a vertex works correctly"""
po = Polygon([p1, p2, p3, p4])
po[0] = (70.0, 80.0)
self.assertEqual(po[0], (70.0, 80.0))
po[1] = [45.0, 38.0]
self.assertEqual(po[1], (45.0, 38.0))
po = Polygon([p1, p2, p3, p4])
self.assertEqual(po[0], p1)
self.assertEqual(po[1], p2)
self.assertEqual(po[2], p3)
self.assertEqual(po[3], p4)
self.assertEqual(po[-3], p2)
self.assertEqual(po[-2], p3)
self.assertEqual(po[-1], p4)
invalid_indexes = [4, 7, 100, -5, -7, -100]
for invalid_index in invalid_indexes:
with self.assertRaises(IndexError):
po[invalid_index]
po[0] = po[0]
self.assertEqual(po[0], po[0])
po2 = Polygon([p1, p2, p3, p4])
valid_indexes = [0, 1, 2, 3, -1, -2, -3, -4]
for valid_index in valid_indexes:
self.assertEqual(po[valid_index], po2[valid_index])
def test_length(self):
po = Polygon((p1, p2, p3, p4))
self.assertEqual(len(po), 4)
po = Polygon([p1, p2, p3, p4, (54.0, 39.0)])
self.assertEqual(len(po), 5)
po = Polygon([p1, p2, p3, p4, (75.0, 83.0), [23.0, 12.0], [90.0, 134.0]])
self.assertEqual(len(po), 7)
def test_contains(self):
po = Polygon([p1, p2, p3, p4])
self.assertTrue(p1 in po)
self.assertTrue(p2 in po)
self.assertTrue(p3 in po)
self.assertTrue(p4 in po)
self.assertFalse([90.0, 47.0] in po)
self.assertFalse((35.0, 9.0) in po)
invalid_types = (None, [], "1", (1,), Vector3(1, 1, 1), 1, (1, 2, 3))
for value in invalid_types:
with self.assertRaises(TypeError):
value in po
invalid_values = (
[17.0, None],
["1", 47.0],
(None, None),
("123", "456"),
(17.0, "12"),
)
for value in invalid_values:
with self.assertRaises(TypeError):
value in po
def test_construction_objwithpolygonattr(self):
"""Checks whether the constructor works correctly with an object with a polygon
attribute"""
class Test:
def __init__(self, poly):
self.polygon = poly
test = Test([p1, p2, p3, p4]) # list
test_2 = Test((p1, p2, p3, p4)) # tuple
test_3 = Test(Polygon([p1, p2, p3, p4])) # polygon
po = Polygon(test)
po_2 = Polygon(test_2)
po_3 = Polygon(test_3)
self.assertEqual(po.vertices, [p1, p2, p3, p4])
self.assertEqual(po_2.vertices, [p1, p2, p3, p4])
self.assertEqual(po_3.vertices, [p1, p2, p3, p4])
def test_copy_invalid_args(self):
"""Checks whether the copy method raises the appropriate errors with invalid
args"""
args = [
(1,),
(1, 2),
(1, 2, 3),
(1, 2, 3, 4),
]
po = Polygon([p1, p2, p3, p4])
for value in args:
with self.assertRaises(TypeError):
po.copy(*value)
def test_static_normal_polygon(self):
center = (150.5, 100.1)
radius = 50.2
sides = 10
angle = 20.6
polygon_pg = geometry.regular_polygon(sides, center, radius, angle)
vertices_pg = polygon_pg.vertices
vertices = list(range(sides))
fac = math.tau / sides
radang = math.radians(angle)
for i in range(sides // 2):
ang = radang + i * fac
radi_cos_a = radius * math.cos(ang)
radi_sin_a = radius * math.sin(ang)
vertices[i] = (center[0] + radi_cos_a, center[1] + radi_sin_a)
vertices[sides // 2 + i] = (center[0] - radi_cos_a, center[1] - radi_sin_a)
self.assertEqual(vertices_pg, vertices)
invalid_types = [
None,
[],
"1",
"123",
(1,),
[1, 2, 3],
[p1, p2, p3, 32],
[p1, p2, "(1, 1)"],
]
for invalid_type in invalid_types + [(1, 2)]:
with self.assertRaises(TypeError):
geometry.regular_polygon(invalid_type, (1, 2.2), 5.5, 1)
for invalid_type in invalid_types:
with self.assertRaises(TypeError):
geometry.regular_polygon(5, invalid_type, 5.5, 1)
for invalid_type in invalid_types + [(1, 2)]:
with self.assertRaises(TypeError):
geometry.regular_polygon(5, (1, 2.2), invalid_type, 1)
for invalid_type in invalid_types + [(1, 2)]:
with self.assertRaises(TypeError):
geometry.regular_polygon(5, (1, 2.2), 5.5, invalid_type)
with self.assertRaises(TypeError):
geometry.regular_polygon(1, (1, 2.2), 5.5, 1, 5)
with self.assertRaises(TypeError):
geometry.regular_polygon()
with self.assertRaises(ValueError):
geometry.regular_polygon(-1, center, radius, angle)
with self.assertRaises(ValueError):
geometry.regular_polygon(2, center, radius, angle)
def test_copy_return_type(self):
"""Checks whether the copy method returns a polygon."""
po = Polygon([p1, p2, p3, p4])
self.assertIsInstance(po.copy(), Polygon)
self.assertEqual(type(po.copy()), Polygon)
def test_copy(self):
"""Checks whether the copy method works correctly."""
po = Polygon([p1, p2, p3, p4])
po_center_x = po.centerx
po_center_y = po.centery
po_2 = po.copy()
self.assertEqual(po_2.vertices, [p1, p2, p3, p4])
self.assertEqual(po_2.vertices, po.vertices)
self.assertEqual(po_2.centerx, po_center_x)
self.assertEqual(po_2.centery, po_center_y)
def test_center_x(self):
"""Makes sure changing center x component does change the positions of vertices properly."""
vertices = _some_vertices.copy()
poly = Polygon(vertices)
poly_centerx = poly.centerx
poly.centerx = 100.0
vertices = [list(vertex) for vertex in vertices]
for vertex in vertices:
vertex[0] += 100.0 - poly_centerx
vertices = [tuple(vertex) for vertex in vertices]
self.assertEqual(poly.vertices, vertices)
def test_center_x__invalid_value(self):
"""Ensures the function can handle the polygon center x component by invalid data types."""
poly = Polygon(_some_vertices.copy())
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
poly.centerx = value
def test_center_x__del(self):
"""Ensures that x component cannot be deleted."""
poly = Polygon(_some_vertices.copy())
with self.assertRaises(AttributeError):
del poly.centerx
def test_center_y(self):
"""Makes sure changing center y component does change the positions of vertices properly."""
vertices = _some_vertices.copy()
poly = Polygon(vertices)
poly_centery = poly.centery
poly.centery = 100.0
vertices = [list(vertex) for vertex in vertices]
for vertex in vertices:
vertex[1] += 100.0 - poly_centery
vertices = [tuple(vertex) for vertex in vertices]
self.assertEqual(poly.vertices, vertices)
def test_center_y__invalid_value(self):
"""Ensures the function can handle the polygon center y component by invalid data types."""
poly = Polygon(_some_vertices)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
poly.centery = value
def test_center_y__del(self):
"""Ensures that y component cannot be deleted."""
poly = Polygon(_some_vertices)
with self.assertRaises(AttributeError):
del poly.centery
def test_center(self):
"""Makes sure that setting new center moves the vertices properly."""
poly = Polygon(_some_vertices.copy())
poly_center = poly.center
poly.center = (200.0, 200.0)
vertices = _some_vertices.copy()
vertices = [list(vertex) for vertex in vertices]
for vertex in vertices:
vertex[0] += 200.0 - poly_center[0]
vertex[1] += 200.0 - poly_center[1]
vertices = [tuple(vertex) for vertex in vertices]
self.assertEqual(poly.vertices, vertices)
self.assertEqual(poly.centerx, 200.0)
self.assertEqual(poly.centery, 200.0)
pre_poly_vertices = poly.vertices
poly.center = poly.center
self.assertEqual(poly.vertices, pre_poly_vertices)
def test_center__invalid_value(self):
"""Ensures the function can handle the polygon center component by invalid data types."""
poly = Polygon(_some_vertices.copy())
for value in (None, [], "1", (1,), [1, 2, 3], (1, "s"), (None, 3), (2, (3,))):
with self.assertRaises(TypeError):
poly.center = value
def test_center__del(self):
"""Ensures that center component cannot be deleted."""
poly = Polygon(_some_vertices.copy())
with self.assertRaises(AttributeError):
del poly.center
def test__str__(self):
"""Checks whether the __str__ method works correctly."""
p_str = "<Polygon(3, [(1.0, 1.0), (2.0, 2.0), (3.0, 3.0)])>"
polygon = Polygon([(1.0, 1.0), (2.0, 2.0), (3.0, 3.0)])
self.assertEqual(str(polygon), p_str)
self.assertEqual(polygon.__str__(), p_str)
def test__repr__(self):
"""Checks whether the __repr__ method works correctly."""
p_repr = "<Polygon(3, [(1.0, 1.0), (2.0, 2.0), (3.0, 3.0)])>"
polygon = Polygon([(1.0, 1.0), (2.0, 2.0), (3.0, 3.0)])
self.assertEqual(repr(polygon), p_repr)
self.assertEqual(polygon.__repr__(), p_repr)
def test_as_segments(self):
"""Checks whether polygon segments are correct"""
poly = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
self.assertEqual(
poly.as_segments(),
[
Line((0, 0), (1, 0)),
Line((1, 0), (1, 1)),
Line((1, 1), (0, 1)),
Line((0, 1), (0, 0)),
],
)
poly = Polygon([(123.23, 35.6), (56.4, 87.45), (43.1, 12.3)])
self.assertEqual(
poly.as_segments(),
[
Line((123.23, 35.6), (56.4, 87.45)),
Line((56.4, 87.45), (43.1, 12.3)),
Line((43.1, 12.3), (123.23, 35.6)),
],
)
poly = Polygon([[1, 2], [3, 4], [5, 6]])
self.assertEqual(
poly.as_segments(),
[Line((1, 2), (3, 4)), Line((3, 4), (5, 6)), Line((5, 6), (1, 2))],
)
def test_move_xy(self):
"""Checks whether polygon move function works correctly with an x-y pair."""
poly = Polygon(_some_vertices.copy())
center_x = poly.centerx
center_y = poly.centery
new_poly = poly.move(10.0, 10.0)
vertices = _some_vertices.copy()
vertices = [list(vertex) for vertex in vertices]
for vertex in vertices:
vertex[0] += 10.0
vertex[1] += 10.0
vertices = [tuple(vertex) for vertex in vertices]
self.assertEqual(vertices, new_poly.vertices)
self.assertNotEqual(poly.vertices, new_poly.vertices)
self.assertEqual(poly.vertices, _some_vertices)
self.assertAlmostEqual(new_poly.centerx, center_x + 10.0)
self.assertAlmostEqual(new_poly.centery, center_y + 10.0)
def test_move_x(self):
"""Checks whether polygon move function works correctly with an x component."""
poly = Polygon(_some_vertices.copy())
center_x = poly.centerx
center_y = poly.centery
new_poly = poly.move(10.0, 0.0)
vertices = _some_vertices.copy()
vertices = [list(vertex) for vertex in vertices]
for vertex in vertices:
vertex[0] += 10.0
vertices = [tuple(vertex) for vertex in vertices]
self.assertEqual(vertices, new_poly.vertices)
self.assertNotEqual(poly.vertices, new_poly.vertices)
self.assertEqual(poly.vertices, _some_vertices)
self.assertAlmostEqual(new_poly.centerx, center_x + 10.0)
self.assertAlmostEqual(new_poly.centery, center_y)
def test_move_y(self):
"""Checks whether polygon move function works correctly with a y component."""
poly = Polygon(_some_vertices.copy())
center_x = poly.centerx
center_y = poly.centery
new_poly = poly.move(0.0, 10.0)
vertices = _some_vertices.copy()
vertices = [list(vertex) for vertex in vertices]
for vertex in vertices:
vertex[1] += 10.0
vertices = [tuple(vertex) for vertex in vertices]
self.assertEqual(vertices, new_poly.vertices)
self.assertNotEqual(poly.vertices, new_poly.vertices)
self.assertEqual(poly.vertices, _some_vertices)
self.assertAlmostEqual(new_poly.centerx, center_x)
self.assertAlmostEqual(new_poly.centery, center_y + 10.0)
def test_move_inplace(self):
"""Checks whether polygon moved by (0, 0) and is the returned polygon identical."""
poly = Polygon(_some_vertices.copy())
poly_new = poly.move(0, 0)
##self.assertAlmostEquals(poly_new, poly)
self.assertEqual(poly_new.centerx, poly.centerx)
self.assertEqual(poly_new.centery, poly.centery)
self.assertEqual(poly_new.vertices, poly.vertices)
def test_move_invalid_args(self):
"""Tests whether function can handle invalid parameter types correctly."""
vertices = _some_vertices.copy()
invalid_types = (None, [], "1", (1,), Vector3(1, 1, 3), Polygon(vertices))
poly = Polygon(vertices)
for value in invalid_types:
with self.assertRaises(TypeError):
poly.move(value)
def test_move_argnum(self):
poly = Polygon(_some_vertices.copy())
invalid_args = [(1, 1, 1), (1, 1, 1, 1)]
for arg in invalid_args:
with self.assertRaises(TypeError):
poly.move(*arg)
def test_move_return_type(self):
"""Tests whether the move function returns a Polygon type/subtype object"""
move_amounts = [
(1, 1),
(0, 1),
(1, 0),
(0, 0),
(1.0, 1.0),
(0.0, 1.0),
(1.0, 0.0),
(0.0, 0.0),
(-1, -1),
(0, -1),
(-1, 0),
(-1.0, -1.0),
(0.0, -1.0),
(-1.0, 0.0),
]
poly = Polygon(_some_vertices.copy())
for move_amount in move_amounts:
self.assertIsInstance(poly.move(*move_amount), Polygon)
self.assertIsInstance(poly.move(move_amount), Polygon)
class TestPolygon(Polygon):
pass
polysub = TestPolygon(_some_vertices.copy())
for move_amount in move_amounts:
self.assertIsInstance(polysub.move(*move_amount), TestPolygon)
self.assertIsInstance(polysub.move(move_amount), TestPolygon)
def test_move_ip_xy(self):
"""Checks whether polygon move_ip function works correctly with an x-y pair."""
vertices = _some_vertices.copy()
poly = Polygon(vertices)
center_x = poly.centerx
center_y = poly.centery
poly.move_ip(10.0, 10.0)
vertices = [list(vertex) for vertex in vertices]
for vertex in vertices:
vertex[0] += 10.0
vertex[1] += 10.0
vertices = [tuple(vertex) for vertex in vertices]
self.assertEqual(poly.vertices, vertices)
self.assertEqual(poly.centerx, center_x + 10.0)
self.assertEqual(poly.centery, center_y + 10.0)
def test_move_ip_x(self):
"""Checks whether polygon move_ip function works correctly with an x component."""
vertices = _some_vertices.copy()
poly = Polygon(vertices)
center_x = poly.centerx
center_y = poly.centery
poly.move_ip(10.0, 0.0)
vertices = [list(vertex) for vertex in vertices]
for vertex in vertices:
vertex[0] += 10.0
vertices = [tuple(vertex) for vertex in vertices]
self.assertEqual(poly.vertices, vertices)
self.assertEqual(poly.centerx, center_x + 10.0)
self.assertEqual(poly.centery, center_y)
def test_move_ip_y(self):
"""Checks whether polygon move_ip function works correctly with a y component."""
vertices = _some_vertices.copy()
poly = Polygon(vertices)
center_x = poly.centerx
center_y = poly.centery
poly.move_ip(0.0, 10.0)
vertices = [list(vertex) for vertex in vertices]
for vertex in vertices:
vertex[1] += 10.0
vertices = [tuple(vertex) for vertex in vertices]
self.assertEqual(poly.vertices, vertices)
self.assertEqual(poly.centerx, center_x)
self.assertEqual(poly.centery, center_y + 10.0)
def test_move_ip_inplace(self):
"""Ensures that moving the polygon by (0, 0) will not change its position."""
poly = Polygon(_some_vertices.copy())
vertices = _some_vertices.copy()
center_x = poly.centerx
center_y = poly.centery
poly.move_ip(0, 0)
vertices = [tuple(vertex) for vertex in vertices]
self.assertEqual(poly.vertices, vertices)
self.assertEqual(poly.centerx, center_x)
self.assertEqual(poly.centery, center_y)
def test_move_ip_return_type(self):
"""Tests whether the move_ip function returns a Polygon type/subtype object"""
move_amounts = [
(1, 1),
(0, 1),
(1, 0),
(0, 0),
(1.0, 1.0),
(0.0, 1.0),
(1.0, 0.0),
(0.0, 0.0),
(-1, -1),
(0, -1),
(-1, 0),
(-1.0, -1.0),
(0.0, -1.0),
(-1.0, 0.0),
]
poly = Polygon(_some_vertices.copy())
for move_amount in move_amounts:
self.assertEqual(type(poly.move_ip(*move_amount)), type(None))
self.assertEqual(type(poly.move_ip(move_amount)), type(None))
class TestPolygon(Polygon):
pass
polysub = TestPolygon(_some_vertices.copy())
for move_amount in move_amounts:
self.assertEqual(type(polysub.move_ip(*move_amount)), type(None))
self.assertEqual(type(polysub.move_ip(move_amount)), type(None))
def test_move_ip_invalid_args(self):
"""tests if the function correctly handles incorrect types as parameters"""
invalid_types = (
None,
[],
"1",
(1,),
Vector3(1, 1, 3),
Polygon(_some_vertices.copy()),
)
poly = Polygon(_some_vertices.copy())
for value in invalid_types:
with self.assertRaises(TypeError):
poly.move_ip(value)
def test_move_ip_argnum(self):
poly = Polygon(_some_vertices.copy())
invalid_args = [(1, 1, 1), (1, 1, 1, 1)]
for arg in invalid_args:
with self.assertRaises(TypeError):
poly.move_ip(*arg)
def test_rotate(self):
"""Tests whether the polygon rotates correctly."""
vertices = _some_vertices.copy()
gen_poly = Polygon(vertices)
angles = [
0.0,
-0.0,
1.0,
-1.0,
90.0,
-90.0,
180.0,
-180.0,
360.0,
-360.0,
720.0,
-720.0,
23.31545,
-23.31545,
]
rot_centers = [
gen_poly.center,
(0, 0),
(1, 1),
(0, 1),
(1, 0),
(0, -1),
(-1, 0),
(-1, -1),
] + vertices
for angle in angles:
for c in rot_centers:
poly = gen_poly.copy()
rotated_vertices = _rotate_vertices(poly, angle, c)
p2 = poly.rotate(angle, c)
for v1, v2 in zip(p2.vertices, rotated_vertices):
self.assertAlmostEqual(v1[0], v2[0])
self.assertAlmostEqual(v1[1], v2[1])
def test_rotate_invalid_args(self):
"""Tests whether the function can handle invalid parameter types correctly."""
invalid_types = (
None,
[],
"1",
(1,),
Vector2(1, 1),
Vector3(1, 1, 3),
Polygon(_some_vertices.copy()),
)
poly = Polygon(_some_vertices.copy())
for value in invalid_types:
with self.assertRaises(TypeError):
poly.rotate(value)
for value in [t for t in invalid_types if not isinstance(t, Vector2)]:
with self.assertRaises(TypeError):
poly.rotate(2, value)
def test_rotate_argnum(self):
"""Tests whether the function can handle invalid parameter number correctly."""
poly = Polygon(_some_vertices.copy())
invalid_args = [(1, (1, 1), (1, 1)), (1, (1, 1), (1, 1), (1, 1))]
for arg in invalid_args:
with self.assertRaises(TypeError):
poly.rotate(*arg)
def test_rotate_return_type(self):
"""Tests whether the function returns the correct type."""
poly = Polygon(_some_vertices.copy())
angles = [-0.0, 0.0, 1.0, 90.0, 180.0, 360.0, 720.0, 23.31545, -23.31545]
for angle in angles:
self.assertIsInstance(poly.rotate(angle), Polygon)
class TestPolygon(Polygon):
pass
poly2 = TestPolygon(_some_vertices.copy())
for angle in angles:
self.assertIsInstance(poly2.rotate(angle), TestPolygon)
def test_rotate_ip(self):
"""Tests whether the polygon rotates correctly."""
vertices = _some_vertices.copy()
gen_poly = Polygon(vertices)
angles = [
0.0,
-0.0,
1.0,
-1.0,
90.0,
-90.0,
180.0,
-180.0,
360.0,
-360.0,
720.0,
-720.0,
23.31545,
-23.31545,
]
rot_centers = [
gen_poly.center,
(0, 0),
(1, 1),
(0, 1),
(1, 0),
(0, -1),
(-1, 0),