-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreflexions_test.py
97 lines (84 loc) · 6.58 KB
/
reflexions_test.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
import unittest
from reflexions import segmentInTheGrid, dedans, reflection_line
from sympy import Point, Segment, Polygon
class TestReflexions(unittest.TestCase):
def setUp(self):
self.cote=20
self.grille=Polygon(Point(-self.cote, -self.cote), Point(-self.cote, self.cote), Point(self.cote, self.cote), Point(self.cote, -self.cote))
def test_about_line(self):
self.assertEqual(reflection_line(Point(0,1), Segment(Point(0,0), Point(1,0))), Point(0,-1))
self.assertEqual(reflection_line(Point(0,2), Segment(Point(0,0), Point(1,0))), Point(0,-2))
self.assertEqual(reflection_line(Point(1,0), Segment(Point(0,0), Point(0,1))), Point(-1,0))
self.assertEqual(reflection_line(Point(-1,1), Segment(Point(0,0), Point(1,1))), Point(1,-1))
class TestDedans(unittest.TestCase):
def setUp(self):
self.cote=300
self.grille=Polygon(Point(-self.cote, -self.cote), Point(-self.cote, self.cote), Point(self.cote, self.cote), Point(self.cote, -self.cote))
# Tests with Points inside the grid
def test_In(self):
self.assertTrue(dedans(Point(0,0), self.grille))
self.assertTrue(dedans(Point(self.cote/2,0), self.grille))
self.assertTrue(dedans(Point(0,self.cote/2), self.grille))
self.assertTrue(dedans(Point(self.cote/2,self.cote/2), self.grille))
self.assertTrue(dedans(Point(self.cote,0), self.grille))
self.assertTrue(dedans(Point(0,self.cote), self.grille))
self.assertTrue(dedans(Point(self.cote,self.cote), self.grille))
# Tests with Points outside the grid
def test_Out(self):
self.assertFalse(dedans(Point(2*self.cote,self.cote), self.grille))
self.assertFalse(dedans(Point(self.cote,2*self.cote), self.grille))
self.assertFalse(dedans(Point(0,2*self.cote), self.grille))
self.assertFalse(dedans(Point(2*self.cote,0), self.grille))
self.assertFalse(dedans(Point(self.cote,self.cote+1), self.grille))
class TestSegmentInTheGrid(unittest.TestCase):
def setUp(self):
self.cote=300
self.grille=Polygon(Point(-self.cote, -self.cote), Point(-self.cote, self.cote), Point(self.cote, self.cote), Point(self.cote, -self.cote))
# This test contains the arrival point out the grid and the starting point in.
def test_oneInOneOut(self):
# Here S is at the position (0,0)
# A is on the left
self.assertEqual(segmentInTheGrid(Segment(Point(-2*self.cote,0),Point(0,0)), self.grille), Segment(Point(-self.cote,0),Point(0,0)))
# A is on the right
self.assertEqual(segmentInTheGrid(Segment(Point(2*self.cote,0),Point(0,0)), self.grille), Segment(Point(self.cote,0),Point(0,0)))
# A is on the right but at half the height too
self.assertEqual(segmentInTheGrid(Segment(Point(2*self.cote,self.cote/2),Point(0,0)), self.grille), Segment(Point(self.cote,self.cote/4),Point(0,0)))
# A is on the top
self.assertEqual(segmentInTheGrid(Segment(Point(0,2*self.cote),Point(0,0)), self.grille), Segment(Point(0,self.cote),Point(0,0)))
# A is at the bottom
self.assertEqual(segmentInTheGrid(Segment(Point(0,-2*self.cote),Point(0,0)), self.grille), Segment(Point(0,-self.cote),Point(0,0)))
# This test contains the arrival point in the grid and the starting point out. It's the same tests as above but S and A are always swapped
def test_oneOutOneIn(self):
# Here A is at the position (0,0)
# S is on the left
self.assertEqual(segmentInTheGrid(Segment(Point(0,0),Point(-2*self.cote,0)), self.grille), Segment(Point(0,0), Point(-self.cote,0)))
# S is on the right
self.assertEqual(segmentInTheGrid(Segment(Point(0,0),Point(2*self.cote,0)), self.grille), Segment(Point(0,0),Point(self.cote,0)))
# S is on the right but at half the height too
self.assertEqual(segmentInTheGrid(Segment(Point(0,0),Point(2*self.cote,self.cote/2)), self.grille), Segment(Point(0,0),Point(self.cote,self.cote/4)))
# S is on the top
self.assertEqual(segmentInTheGrid(Segment(Point(0,0),Point(0,2*self.cote)), self.grille), Segment(Point(0,0),Point(0,self.cote)))
# S is at the bottom
self.assertEqual(segmentInTheGrid(Segment(Point(0,0),Point(0,-2*self.cote)), self.grille), Segment(Point(0,0),Point(0,-self.cote)))
# Test with one point out and the other one on the border. Should return Point
self.assertEqual(segmentInTheGrid(Segment(Point(0,-self.cote),Point(0,-2*self.cote)), self.grille), Point(0,-self.cote))
self.assertEqual(segmentInTheGrid(Segment(Point(self.cote, 0),Point(2*self.cote, 0)), self.grille), Point(self.cote, 0))
# This test contains the arrival point and the starting point in the grid
def test_bothIn(self):
self.assertEqual(segmentInTheGrid(Segment(Point(-self.cote/2,0),Point(self.cote/2,0)), self.grille), Segment(Point(-self.cote/2,0),Point(self.cote/2,0)))
self.assertEqual(segmentInTheGrid(Segment(Point(self.cote/2,self.cote/2),Point(-self.cote/2, -self.cote/2)), self.grille), Segment(Point(self.cote/2,self.cote/2),Point(-self.cote/2, -self.cote/2)))
# This test contains the arrival point and the starting point out the grid
def test_bothOut(self):
# No intersections
self.assertEqual(segmentInTheGrid(Segment(Point(3*self.cote,0),Point(2*self.cote,0)), self.grille), None)
self.assertEqual(segmentInTheGrid(Segment(Point(-self.cote, 2*self.cote),Point(self.cote, 2*self.cote)), self.grille), None)
self.assertEqual(segmentInTheGrid(Segment(Point(3*self.cote,0),Point(0,3*self.cote)), self.grille), None)
# Two intersections
self.assertEqual(segmentInTheGrid(Segment(Point(-self.cote,2*self.cote),Point(2*self.cote, -self.cote)), self.grille), Segment(Point(0,self.cote),(self.cote, 0)))
self.assertEqual(segmentInTheGrid(Segment(Point(2*self.cote, -self.cote),Point(-self.cote,2*self.cote)), self.grille), Segment(Point(self.cote, 0),(0, self.cote)))
self.assertEqual(segmentInTheGrid(Segment(Point(0, -self.cote),Point(0,2*self.cote)), self.grille), Segment(Point(0, -self.cote),(0, self.cote)))
self.assertEqual(segmentInTheGrid(Segment(Point(-self.cote, 0),Point(2*self.cote, 0)), self.grille), Segment(Point(-self.cote, 0),(self.cote, 0)))
self.assertEqual(segmentInTheGrid(Segment(Point(2*self.cote, 0),Point(-self.cote,0)), self.grille), Segment(Point(self.cote, 0),(-self.cote, 0)))
self.assertEqual(segmentInTheGrid(Segment(Point(2*self.cote, 0),Point(-self.cote,0)), self.grille), Segment(Point(self.cote, 0),(-self.cote, 0)))
if __name__ == '__main__':
unittest.main()