Debian Codegraph @

wxgeometrie/geolib/tests/test_lignes.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 # -*- coding: utf-8 -*-
2 import os, sys
3 TOPDIR = os.path.abspath(os.path.join(os.path.dirname(__file__),"../.."))
4 sys.path.insert(0, TOPDIR)
5
6 from math import sqrt
7 from random import random, randint
8
•9 import tools.unittests
10 from wxgeometrie.geolib.tests.geotestlib import rand_pt
11 from wxgeometrie.geolib import (
12 Tangente, Perpendiculaire, Parallele, Mediatrice,
13 Droite_vectorielle, Point, Cercle, Droite,
14 Bissectrice, Label_droite, Label_demidroite,
15 Label_segment, Droite_equation, Milieu, Segment,
16 Barycentre, Vecteur_libre, Demidroite, Demiplan,
17 )
18
19 from wxgeometrie.geolib import (
20 Interpolation_polynomiale_par_morceaux,
21 Glisseur_courbe, Tangente_glisseur_interpolation
22 )
23
•24 class GeolibTest(tools.unittests.TestCase):
25
26 def test_Segment(self):
27 A = Point(4.5, 7.3)
28 B = Point(4, 2.1)
29 s = Segment(A, B)
30 self.assertIsInstance(s.etiquette, Label_segment)
31 self.assertAlmostEqual(s.longueur, sqrt((B.x - A.x)**2 + (B.y - A.y)**2))
32 I = Milieu(s.point1, s.point2)
33 self.assertEqual(I.coordonnees, ((A.x+B.x)/2, (A.y+B.y)/2))
34 M = Barycentre((A, 1), (B, -2))
35 N = Barycentre((A, -2), (B, 1))
36 self.assertIn(I, s)
37 self.assertNotIn(M, s)
38 self.assertNotIn(N, s)
39 self.assertEqual(s.mode_affichage, 'rien')
40 K = Point(s, 0.5)
41 self.assertTrue(K.confondu(I))
42
43 def test_Demidroite(self):
44 A = Point(4.5, 7.3)
45 B = Point(4, 2.1)
46 s = Demidroite(A, B)
47 self.assertIsInstance(s.etiquette, Label_demidroite)
48 self.assertRaises(AttributeError, getattr, s, "longueur")
49 I = Milieu(s.origine, s.point)
50 self.assertEqual(I.coordonnees, ((A.x+B.x)/2, (A.y+B.y)/2))
51 M = Barycentre((A, 1), (B, -2))
52 N = Barycentre((A, -2), (B, 1))
53 self.assertIn(I, s)
54 self.assertIn(M, s)
55 self.assertNotIn(N, s)
56 self.assertEqual(s.mode_affichage, 'rien')
57
58 def test_Droite(self):
59 A = Point(4.5, 7.3)
60 B = Point(4, 2.1)
61 d = Droite(A, B)
62 self.assertIsInstance(d.etiquette, Label_droite)
63 self.assertRaises(AttributeError, getattr, d, "longueur")
64 I = Milieu(d.point1, d.point2)
65 self.assertEqual(I.coordonnees, ((A.x+B.x)/2, (A.y+B.y)/2))
66 M = Barycentre((A, 1), (B, -2))
67 N = Barycentre((A, -2), (B, 1))
68 self.assertIn(I, d)
69 self.assertIn(M, d)
70 self.assertIn(N, d)
71 self.assertIsInstance(d.equation, tuple)
72 self.assertEqual(d.mode_affichage, 'rien')
73 # Test du typage dynamique
74 d = Droite("y=x+1")
75 self.assertIn(Point(0, 1), d)
76 d = Droite(Point(1, 2), Vecteur_libre(1, 1))
77 self.assertIn(Point(1, 2), d)
78 self.assertIn(Point(2, 3), d)
79 d2 = Droite("y=-x+1")
80 self.assertIn(Point(0, 1), d2)
81 self.assertIn(Point(1, 0), d2)
82
83
84
85 def test_Droite_vectorielle(self):
86 v = Vecteur_libre(1, 7)
87 A = Point(-2, 3)
88 d = Droite_vectorielle(A, v)
89 self.assertTrue(d.vecteur is v and d.point is A)
90 self.assertAlmostEqual(v.y/v.x, -d.equation[0]/d.equation[1])
91 B = rand_pt()
92 d1 = Droite_vectorielle(B, v)
93 self.assertTrue(d.parallele(d1))
94
95 def test_Parallele(self):
96 d0 = Droite_equation(2, 1, 7)
97 A = Point(-2, 3)
98 d = Parallele(d0, A)
99 self.assertTrue(d.parallele(d0))
100 self.assertTrue(d.droite is d0 and d.point is A)
101 self.assertAlmostEqual(d0.equation[:1], d.equation[:1])
102
103 # def test_Droite_rotation(self):
104 # r = Rotation(Point(1.45, -2.59), math.pi/3)
105 # C = Point(1.458, -5.255)
106 # D = Point(3.478, -2.14788)
107 # d = Droite(C, D)
108 # # Dans ce qui suit, d1, d2 et d3 doivent correspondre à la même droite.
109 # d1 = Droite_rotation(d, r)
110 # d2 = Droite(r(C), r(D))
111 # d3 = r(d)
112 # a, b, c = d1.equation
113 # self.assertAlmostEqual(d1.equation_reduite, d2.equation_reduite)
114 # self.assertAlmostEqual(d1.equation_reduite, d3.equation_reduite)
115 # self.assertAlmostEqual(d1.equation_reduite, (-a/b, -c/b))
116 # d = Droite_rotation(Droite_equation(1, -1, 1), Rotation(Point(0, 0), math.pi/2))
117 # a, b, c = d.equation
118 # self.assertAlmostEqual(b/a, 1)
119 # self.assertAlmostEqual(c/a, 1)
120
121
122 def test_Mediatrice(self):
123 A = Point(4.5, 7.3)
124 B = Point(-4.147, 2.1)
125 s = Segment(A, B)
126 d0 = Mediatrice(s)
127 d1 = Mediatrice(A, B)
128 I = Milieu(A, B)
129 self.assertIn(I, d0)
130 self.assertIn(I, d1)
131 a, b, c = s.equation
132 a0, b0, c0 = d0.equation
133 self.assertAlmostEqual(a*a0 + b*b0, 0)
134 self.assertAlmostEqual(d0.equation, d1.equation)
135
136 def test_Perpendiculaire(self):
137 d = Droite_equation(-1, 2, 0)
138 M = Point()
139 d0 = Perpendiculaire(d, M)
140 a, b, c = d.equation
141 a0, b0, c0 = d0.equation
142 self.assertTrue(d.perpendiculaire(d0))
143 self.assertAlmostEqual(a*a0 + b*b0, 0)
144 self.assertIn(M, d0)
145
146 def test_Droite_equation(self):
147 a = randint(0,50) - randint(0,50) + 0.1 # afin que a ne soit pas nul
148 b = randint(0,50) - randint(0,50) + random()
149 c = randint(0,50) - randint(0,50) + random()
150 d, e, f = Droite_equation(a, b, c).equation
151 self.assertAlmostEqual((e/d, f/d), (b/a, c/a))
152 self.assertEqual(Droite_equation(a, 0, 0).equation[1:], (0, 0))
153 self.assertEqual((Droite_equation(0, a, 0).equation[0], Droite_equation(0, a, 0).equation[2]), (0, 0))
154 self.assertFalse(Droite_equation(0, 0, 0).existe)
155 d = Droite_equation("y=-5/2x-3/2")
156 self.assertIn(Point(0, -1.5), d)
157 self.assertIn(Point(-1, 1), d)
158 d = Droite_equation("x=2*10**2")
159 self.assertIn(Point(200, -1000), d)
160 self.assertNotIn(Point(100, -1000), d)
161 d = Droite_equation("2*x+2*y=1")
162 self.assertIn(Point(0.5, 0), d)
163 self.assertIn(Point(1, -0.5), d)
164 d = Droite_equation("x+y=1")
165 self.assertIn(Point(0, 1), d)
166 self.assertIn(Point(1, 0), d)
167 d = Droite_equation("x+2y=-2")
168 self.assertIn(Point(0, -1), d)
169 self.assertIn(Point(-2, 0), d)
170
171 def test_Bissectrice(self):
172 A = Point(1, -5)
173 B = Point(1.5, -5.3)
174 C = Point(3, -4)
175 d = Bissectrice(A, B, C)
176 a, b, c = d.equation
177 d, e = (0.0870545184921, -1.03861105199)
178 self.assertAlmostEqual(b/a, d)
179 self.assertAlmostEqual(c/a, e)
180
181 def test_Tangente(self):
182 A = Point(4.75, -2.56887)
183 O = Point(2.56874, -85.2541)
184 M = Point(7.854, -552.444)
185 c = Cercle(O, A)
186 d = Tangente(c, A)
187 self.assertIn(A, d)
188 self.assertNotIn(M, d)
189 d1 = Tangente(c, M)
190 self.assertIn(M, d1)
191 self.assertNotIn(A, d1)
192 self.assertFalse( Tangente(c, O).existe)
193
194 def test_equation_formatee(self):
•195 self.assertEqual(Droite('y=x').equation_formatee, '-x + y = 0')
196
197 def test_Demiplan(self):
198 d = Droite('y=-x+1')
199 P1 = Demiplan(d, Point(0, 0), True)
200 P2 = Demiplan(d, Point(0, 0), False)
201 self.assertIn(Point(0, 0), P1)
202 self.assertIn(Point(1, 0), P1)
203 self.assertIn(Point(0, 0), P2)
204 self.assertNotIn(Point(1, 0), P2)
205
206
207 ##def test_Tangente_courbe_interpolation(self):
208 ##A = Point(-6, -1)
209 ##B = Point(-3, 2)
210 ##C = Point(-0.5, -1)
211 ##D = Point(2.6, 1.1)
212 ##inter = Interpolation_polynomiale_par_morceaux(A, B, C, D)
213 ##T = Tangente_courbe_interpolation(inter, x= 2.)
214 ### la tangente doit passer par M par construction
215 ##self.assertAlmostEqual( T.xy(2), (2, inter.fonction(2)))
216 ### son coef dir doit être la dérivée sur le point de passage
217 ##self.assertAlmostEqual( -T.a / T.b, inter.fonction.derivative(2, 1))
218
219
220 def test_Tangente_glisseur_interpolation(self):
221 A = Point(-6, -1)
222 B = Point(-3, 2)
223 C = Point(-0.5, -1)
224 D = Point(2.6, 1.1)
225 inter = Interpolation_polynomiale_par_morceaux(A, B, C, D)
226 M = Glisseur_courbe(inter, 2)
227 T = Tangente_glisseur_interpolation(inter, M)
228 # la tangente doit passer par M par construction
229 self.assertAlmostEqual(T.xy(M.x), (M.x, M.y))
230 # son coef dir doit etre la dérivée sur le glisseur
231 self.assertAlmostEqual((float(-T.a)/float(T.b)),
232 inter.fonction(M.x, 1)) # dérivée première en M.x
233 # rebelotte après déplacement du glisseur
234 M.x = 1
235 self.assertAlmostEqual(T.xy(M.x), (M.x, M.y))
236 self.assertAlmostEqual((float(-T.a)/float(T.b)),
237 inter.fonction(M.x, 1))