| 1 |
|
| 2 |
import os, sys |
| 3 |
TOPDIR = os.path.abspath(os.path.join(os.path.dirname(__file__),"../..")) |
| 4 |
sys.path.insert(0, TOPDIR) |
| 5 |
|
| •6 |
import tools.unittests, unittest |
| 7 |
from random import random, randint |
| 8 |
import pytest |
| 9 |
from sympy import sympify as symp |
| 10 |
|
| 11 |
from wxgeometrie.geolib.tests.geotestlib import rand_pt, rand_cercle |
| 12 |
|
| 13 |
from wxgeometrie.geolib import ( |
| 14 |
Glisseur_arc_cercle, Glisseur_cercle, Glisseur_demidroite, |
| 15 |
Glisseur_droite, Point, Demidroite, Droite, |
| 16 |
Glisseur_segment, Point_equidistant, Centre, |
| 17 |
Centre_cercle_inscrit, Centre_cercle_circonscrit, |
| 18 |
Orthocentre, Projete_demidroite, Projete_segment, |
| 19 |
Projete_arc_cercle, Projete_cercle, Segment, |
| 20 |
Projete_droite, Centre_gravite, Label_point, |
| 21 |
Point_reflexion, Point_homothetie, Cercle, |
| 22 |
Point_rotation, Point_translation, Point_final, |
| 23 |
Arc_points, Arc_cercle, Triangle, Milieu, |
| 24 |
Barycentre, Mediatrice, Droite_equation, |
| 25 |
Cercle_equation, Polygone, Rotation, Translation, |
| 26 |
Vecteur, Vecteur_libre, Representant, Reflexion, |
| 27 |
Homothetie, Nuage, Fonction, Point_interpolation, |
| 28 |
) |
| 29 |
|
| •30 |
class GeolibTest(tools.unittests.TestCase): |
| 31 |
def test_Point(self): |
| 32 |
A = Point(1, 2) |
| 33 |
self.assertIsInstance(A.etiquette, Label_point) |
| 34 |
self.assertEqual(A.x, 1) |
| 35 |
self.assertEqual(A.y, 2) |
| 36 |
self.assertEqual(type(A.coordonnees), tuple) |
| 37 |
A.x = 5 |
| 38 |
A.y = 7 |
| 39 |
self.assertEqual(A.coordonnees, (5, 7)) |
| 40 |
self.assertEqual(A.mode_affichage, 'nom') |
| 41 |
|
| 42 |
d = Droite(rand_pt(), rand_pt()) |
| 43 |
B = Point(d) |
| 44 |
self.assertIsInstance(B, Glisseur_droite) |
| 45 |
c = Cercle(A, 3) |
| 46 |
C = Point(c) |
| 47 |
self.assertIsInstance(C, Glisseur_cercle) |
| 48 |
d = Demidroite(rand_pt(), rand_pt()) |
| 49 |
B = Point(d) |
| 50 |
self.assertIsInstance(B, Glisseur_demidroite) |
| 51 |
s = Segment(rand_pt(), rand_pt()) |
| 52 |
B = Point(s) |
| 53 |
self.assertIsInstance(B, Glisseur_segment) |
| 54 |
a = Arc_points(Point(), Point(1, 1), Point(1, 2)) |
| 55 |
B = Point(a) |
| 56 |
self.assertIsInstance(B, Glisseur_arc_cercle) |
| 57 |
|
| 58 |
def test_Point_sympy(self): |
| 59 |
A = Point('1/2', '2/3') |
| 60 |
self.assertEqual(A.x, symp('1/2')) |
| 61 |
self.assertAlmostEqual(A.coordonnees, (1/2, 2/3)) |
| 62 |
|
| 63 |
|
| 64 |
def test_Milieu(self): |
| 65 |
A = Point(1,2) |
| 66 |
B = Point(2,4) |
| 67 |
I = Milieu(A,B) |
| 68 |
self.assertTrue(I.x == (A.x+B.x)/2 and I.y == (A.y+B.y)/2) |
| 69 |
self.assertEqual(type(I.coordonnees), tuple) |
| 70 |
C = Point('1/3', '1') |
| 71 |
D = Point('1', '1') |
| 72 |
J = Milieu(C, D) |
| 73 |
self.assertEqual(J.x, symp('2/3')) |
| 74 |
|
| 75 |
|
| 76 |
def test_Barycentre(self): |
| 77 |
M = Point(3, 4) |
| 78 |
A = Point(1,2) |
| 79 |
B = Point(2,4) |
| 80 |
I = Milieu(A,B) |
| 81 |
K = Barycentre(M, A, B, (I, -4)) |
| 82 |
self.assertEqual(K.x, 0) |
| 83 |
self.assertEqual(K.y, 2) |
| 84 |
self.assertFalse(Barycentre(M, A, (B, -2)).existe) |
| 85 |
self.assertEqual(type(K.coordonnees), tuple) |
| 86 |
|
| 87 |
G = Barycentre(points_ponderes = (M, A, B, (I, -4))) |
| 88 |
self.assertEqual(K.coordonnees, G.coordonnees) |
| 89 |
self.assertNotIn("point_ponderes", G.style()) |
| 90 |
|
| 91 |
def test_Point_final(self): |
| 92 |
A = Point(random(),random()) |
| 93 |
C = Point(random(),random()) |
| 94 |
u = Vecteur_libre(random(),random()) |
| 95 |
v = Vecteur(Point(random(), random()), Point(random(), random())) |
| 96 |
w = Representant(Vecteur(Point(random(), random()), Point(random(), random())), A) |
| 97 |
F = Point_final(C, (u, v, w), (-2, 1.5, 4)) |
| 98 |
self.assertEqual(type(F.coordonnees), tuple) |
| 99 |
self.assertAlmostEqual(F.x, C.x -2*u.x+1.5*v.x+4*w.x) |
| 100 |
self.assertAlmostEqual(F.y, C.y -2*u.y+1.5*v.y+4*w.y) |
| 101 |
|
| 102 |
def test_Point_translation(self): |
| 103 |
u = Vecteur_libre(1.56, 2.78) |
| 104 |
v = Vecteur(Point(1.1457, 2.7895), Point(2.458, -8.25)) |
| 105 |
A = Point(5.256, -7.231) |
| 106 |
tu = Translation(u) |
| 107 |
tv = Translation(v) |
| 108 |
Au = Point_translation(A, tu) |
| 109 |
Av = Point_translation(A, tv) |
| 110 |
self.assertAlmostEqual(Au.coordonnees, (6.816, -4.451)) |
| 111 |
self.assertAlmostEqual(Av.coordonnees, (6.5683, -18.2705)) |
| 112 |
|
| 113 |
def test_Point_rotation(self): |
| 114 |
A = Point(1, -4) |
| 115 |
r = Rotation(Point(-2, 7.56), 4.25) |
| 116 |
B = Point_rotation(A, r) |
| 117 |
self.assertAlmostEqual(r(A).coordonnees, B.coordonnees) |
| 118 |
self.assertAlmostEqual(B.coordonnees, (-13.684339450863803, 10.031803308717693)) |
| 119 |
A = Point('1', '0') |
| 120 |
r = Rotation(('0', '0'), 'pi/4') |
| 121 |
B = Point_rotation(A, r) |
| 122 |
C = Point('sqrt(2)/2', 'sqrt(2)/2') |
| 123 |
self.assertTrue(C.x == B.x and C.y == B.y) |
| 124 |
|
| 125 |
def test_Point_homothetie(self): |
| 126 |
A = Point(1, -2) |
| 127 |
h = Homothetie(A, -3) |
| 128 |
M = Point(2, 7) |
| 129 |
self.assertAlmostEqual(Point_homothetie(M, h).coordonnees, (-2, -29)) |
| 130 |
? |
| 131 |
|
| 132 |
def test_Point_reflexion(self): |
| 133 |
d = Droite_equation(1, 6, -2) |
| 134 |
r = Reflexion(d) |
| 135 |
M = Point(random()+randint(0,50)-randint(0,50), random()+randint(0,50)-randint(0,50)) |
| 136 |
m = Mediatrice(M, Point_reflexion(M, r)) |
| 137 |
self.assertAlmostEqual(d.equation_reduite, m.equation_reduite) |
| 138 |
|
| 139 |
def test_Projete_droite(self): |
| 140 |
d = Droite_equation(1.25, -7.12, 2.15) |
| 141 |
A = Point(1.52, 2.14) |
| 142 |
M = Projete_droite(A, d) |
| 143 |
self.assertIn(M, d) |
| 144 |
self.assertTrue(Droite(A, M)._perpendiculaire(d)) |
| 145 |
|
| 146 |
def test_Projete_cercle(self): |
| 147 |
c = Cercle_equation(1, -2, -20) |
| 148 |
self.assertTrue(c.existe) |
| 149 |
A = Point(randint(0,50) - randint(0,50) + random(), randint(0,50) - randint(0,50) + random()) |
| 150 |
P = Projete_cercle(A, c) |
| 151 |
self.assertTrue(P.existe) |
| 152 |
self.assertIn(P, c) |
| 153 |
self.assertIn(P, Demidroite(c.centre, A)) |
| 154 |
A.coordonnees = c.centre.coordonnees |
| 155 |
self.assertFalse(P.existe) |
| 156 |
|
| 157 |
def test_Projete_arc_cercle(self): |
| 158 |
O = Point(23.15, -12.75) |
| 159 |
A = Point(-12.5, 7.14) |
| 160 |
B = Point(7.15, 8.64) |
| 161 |
a = Arc_cercle(O, A, B) |
| 162 |
M = rand_pt() |
| 163 |
P = Projete_arc_cercle(M, a) |
| 164 |
self.assertIn(P, a) |
| 165 |
M.coordonnees = a.centre.coordonnees |
| 166 |
self.assertFalse(P.existe) |
| 167 |
M.coordonnees = -17.826266675199999, 11.760911186 |
| 168 |
self.assertIs(type(P.coordonnees), tuple) |
| 169 |
self.assertAlmostEqual(A.coordonnees, P.coordonnees) |
| 170 |
self.assertTrue(A.confondu(P)) |
| 171 |
|
| 172 |
def test_Projete_segment(self): |
| 173 |
s = Segment(rand_pt(), rand_pt()) |
| 174 |
M = rand_pt() |
| 175 |
self.assertIn(Projete_segment(M, s), s) |
| 176 |
A = Point(0, 1) |
| 177 |
B = Point(2, 1) |
| 178 |
s = Segment(A, B) |
| 179 |
M = Point(1, 7.15) |
| 180 |
P = Projete_segment(M, s) |
| 181 |
self.assertAlmostEqual(Milieu(A, B).coordonnees, P.coordonnees) |
| 182 |
M.x = .5 |
| 183 |
self.assertAlmostEqual(Barycentre((A, 3), (B, 1)).coordonnees, P.coordonnees) |
| 184 |
M.x = -1 |
| 185 |
self.assertAlmostEqual(A.coordonnees, P.coordonnees) |
| 186 |
M.x = 3 |
| 187 |
self.assertAlmostEqual(B.coordonnees, P.coordonnees) |
| 188 |
|
| 189 |
|
| 190 |
def test_Projete_demidroite(self): |
| 191 |
d = Demidroite(rand_pt(), rand_pt()) |
| 192 |
M = rand_pt() |
| 193 |
self.assertIn(Projete_demidroite(M, d), d) |
| 194 |
A = Point(0, 1) |
| 195 |
B = Point(2, 1) |
| 196 |
s = Demidroite(A, B) |
| 197 |
M = Point(1, 7.15) |
| 198 |
P = Projete_demidroite(M, s) |
| 199 |
self.assertAlmostEqual(Milieu(A, B).coordonnees, P.coordonnees) |
| 200 |
M.x = .5 |
| 201 |
self.assertAlmostEqual(Barycentre((A, 3), (B, 1)).coordonnees, P.coordonnees) |
| 202 |
M.x = -1 |
| 203 |
self.assertAlmostEqual(A.coordonnees, P.coordonnees) |
| 204 |
M.x = 3 |
| 205 |
self.assertAlmostEqual(Barycentre((A, -1), (B, 3)).coordonnees, P.coordonnees) |
| 206 |
|
| 207 |
|
| 208 |
def test_Centre_gravite(self): |
| 209 |
A = rand_pt() |
| 210 |
B = rand_pt() |
| 211 |
C = rand_pt() |
| 212 |
I = Milieu(B, C) |
| 213 |
J = Milieu(A, C) |
| 214 |
K = Milieu(A, B) |
| 215 |
G = Centre_gravite(Triangle(A, B, C)) |
| 216 |
self.assertAlmostEqual(Segment(A, G).longueur, 2*Segment(I, G).longueur) |
| 217 |
self.assertAlmostEqual(Segment(B, G).longueur, 2*Segment(J, G).longueur) |
| 218 |
self.assertAlmostEqual(Segment(C, G).longueur, 2*Segment(K, G).longueur) |
| 219 |
|
| 220 |
def test_Orthocentre(self): |
| 221 |
A, B, C = rand_pt(), rand_pt(), rand_pt() |
| 222 |
p = Polygone(A, B, C) |
| 223 |
H = Orthocentre(p) |
| 224 |
self.assertTrue(Droite(A, H).perpendiculaire(Droite(B, C))) |
| 225 |
self.assertTrue(Droite(B, H).perpendiculaire(Droite(A, C))) |
| 226 |
self.assertTrue(Droite(C, H).perpendiculaire(Droite(B, A))) |
| 227 |
|
| 228 |
def test_Centre_cercle_circonscrit(self): |
| 229 |
A, B, C = rand_pt(), rand_pt(), rand_pt() |
| 230 |
p = Polygone(A, B, C) |
| 231 |
O= Centre_cercle_circonscrit(p) |
| 232 |
self.assertTrue(O.confondu(Point_equidistant(A, B, C))) |
| 233 |
|
| 234 |
def test_Centre_cercle_inscrit(self): |
| 235 |
A, B, C = rand_pt(), rand_pt(), rand_pt() |
| 236 |
p = Polygone(A, B, C) |
| 237 |
I = Centre_cercle_inscrit(p) |
| 238 |
P = Projete_segment(I, Segment(B, C)) |
| 239 |
Q = Projete_segment(I, Segment(A, C)) |
| 240 |
R = Projete_segment(I, Segment(A, B)) |
| 241 |
c = Cercle(I, P) |
| 242 |
self.assertTrue(P in c and Q in c and R in c) |
| 243 |
|
| 244 |
def test_Centre(self): |
| 245 |
c = rand_cercle() |
| 246 |
O = Centre(c) |
| 247 |
M = Glisseur_cercle(c) |
| 248 |
self.assertAlmostEqual(Segment(O, M).longueur, c.rayon) |
| 249 |
|
| 250 |
def test_Point_equidistant(self): |
| 251 |
A = rand_pt() |
| 252 |
B = rand_pt() |
| 253 |
C = rand_pt() |
| 254 |
P = Point_equidistant(A, B, C) |
| 255 |
self.assertAlmostEqual(Segment(A, P).longueur, Segment(B, P).longueur) |
| 256 |
self.assertAlmostEqual(Segment(A, P).longueur, Segment(C, P).longueur) |
| 257 |
self.assertIn(P, Mediatrice(A, B)) |
| 258 |
|
| 259 |
def test_Glisseur_droite(self): |
| 260 |
A = rand_pt() |
| 261 |
B = rand_pt() |
| 262 |
d = Droite(A, B) |
| 263 |
M = Glisseur_droite(d) |
| 264 |
self.assertIn(M, d) |
| 265 |
M.k = 0 |
| 266 |
self.assertEqual(M.k, 0) |
| 267 |
self.assertAlmostEqual(M.coordonnees, A.coordonnees) |
| 268 |
P = Point(*M.coordonnees) |
| 269 |
M.k = 1 |
| 270 |
self.assertEqual(M.k, 1) |
| 271 |
self.assertAlmostEqual(M.coordonnees, B.coordonnees) |
| 272 |
M.k = 2 |
| 273 |
self.assertEqual(M.k, 2) |
| 274 |
M.k = -1 |
| 275 |
self.assertEqual(M.k, -1) |
| 276 |
Q = Point(*M.coordonnees) |
| 277 |
self.assertAlmostEqual(Droite(P, Q).equation_reduite, d.equation_reduite) |
| 278 |
M.k = 1.7 |
| 279 |
M(*M.coordonnees) |
| 280 |
self.assertAlmostEqual(M.k, 1.7) |
| 281 |
|
| 282 |
|
| 283 |
def test_Glisseur_segment(self): |
| 284 |
A = rand_pt() |
| 285 |
B = rand_pt() |
| 286 |
s = Segment(A, B) |
| 287 |
M = Glisseur_segment(s) |
| 288 |
self.assertIn(M, s) |
| 289 |
M.k = 0 |
| 290 |
self.assertEqual(M.k, 0) |
| 291 |
self.assertAlmostEqual(M.coordonnees, A.coordonnees) |
| 292 |
P = Point(*M.coordonnees) |
| 293 |
M.k = 1 |
| 294 |
self.assertEqual(M.k, 1) |
| 295 |
self.assertAlmostEqual(M.coordonnees, B.coordonnees) |
| 296 |
M.k = 2 |
| 297 |
self.assertEqual(M.k, 1) |
| 298 |
self.assertIn(M, s) |
| 299 |
M.k = -1 |
| 300 |
self.assertEqual(M.k, 0) |
| 301 |
self.assertIn(M, s) |
| 302 |
M.k = 1 |
| 303 |
Q = Point(*M.coordonnees) |
| 304 |
self.assertAlmostEqual(Droite(P, Q).equation_reduite, s.equation_reduite) |
| 305 |
M.k = 1.7 |
| 306 |
M(*M.coordonnees) |
| 307 |
self.assertAlmostEqual(M.k, 1) |
| 308 |
|
| 309 |
def test_Glisseur_demidroite(self): |
| 310 |
A = rand_pt() |
| 311 |
B = rand_pt() |
| 312 |
d = Demidroite(A, B) |
| 313 |
M = Glisseur_demidroite(d) |
| 314 |
self.assertIn(M, d) |
| 315 |
M.k = 0 |
| 316 |
self.assertEqual(M.k, 0) |
| 317 |
self.assertAlmostEqual(M.coordonnees, A.coordonnees) |
| 318 |
P = Point(*M.coordonnees) |
| 319 |
M.k = 1 |
| 320 |
self.assertEqual(M.k, 1) |
| 321 |
self.assertAlmostEqual(M.coordonnees, B.coordonnees) |
| 322 |
M.k = 2 |
| 323 |
self.assertEqual(M.k, 2) |
| 324 |
M.k = -1 |
| 325 |
self.assertEqual(M.k, 0) |
| 326 |
M.k = 1 |
| 327 |
Q = Point(*M.coordonnees) |
| 328 |
self.assertAlmostEqual(Droite(P, Q).equation_reduite, d.equation_reduite) |
| 329 |
M.k = 1.7 |
| 330 |
M(*M.coordonnees) |
| 331 |
self.assertAlmostEqual(M.k, 1.7) |
| 332 |
|
| 333 |
def test_Glisseur_cercle(self): |
| 334 |
c = rand_cercle() |
| 335 |
M = Glisseur_cercle(c) |
| 336 |
self.assertIn(M, c) |
| 337 |
O = c.centre |
| 338 |
M.coordonnees = O.coordonnees |
| 339 |
u |
| 340 |
|
| 341 |
def test_Glisseur_arc_cercle(self): |
| 342 |
A = rand_pt() |
| 343 |
B = rand_pt() |
| 344 |
C = rand_pt() |
| 345 |
a = Arc_cercle(A, B, C) |
| 346 |
M = Glisseur_arc_cercle(a) |
| 347 |
self.assertIn(M, a) |
| 348 |
O = a.centre |
| 349 |
M.coordonnees = O.coordonnees |
| 350 |
u |
| 351 |
|
| 352 |
|
| 353 |
def test_Point_interpolation(self): |
| 354 |
A = Point(1, 2) |
| 355 |
P = Point_interpolation(A) |
| 356 |
self.assertIs(P.point, A) |
| 357 |
self.assertIsNone(P.derivee) |
| 358 |
|
| 359 |
@unittest.expectedFailure |
| 360 |
def test_Glisseur_courbe(self): |
| 361 |
f = Fonction('x^2+3x-1') |
| 362 |
M = Point(f, 2) |
| 363 |
self.assertEqual(f(2), 2**+3+2-1) |
| 364 |
self.assertEqual(M.y, f(2)) |
| 365 |
|
| 366 |
def test_Nuage(self): |
| 367 |
A = rand_pt() |
| 368 |
B = rand_pt() |
| 369 |
C = rand_pt() |
| 370 |
D = rand_pt() |
| 371 |
while D in (A, B, C): |
| 372 |
D = rand_pt() |
| 373 |
n = Nuage(A, B, C) |
| 374 |
self.assertEqual(n.points, (A, B, C)) |
| 375 |
self.assertIn(A, n) |
| 376 |
self.assertIn(B, n) |
| 377 |
self.assertIn(C, n) |
| 378 |
self.assertNotIn(D, n) |
| 379 |
|
| 380 |
@unittest.expectedFailure |
| 381 |
def test_NuageFonction(self): |
| 382 |
f = Fonction('x^2+3') |
| 383 |
m = Nuage(f, 0.5, 1, 2, 3, 4) |
| 384 |
self.assertIn((2, 2**2+3), m) |
| 385 |
|
| 386 |
def test_Centre_alias(self): |
| 387 |
"Centre et Centre_gravite sont interchangeables." |
| 388 |
A = rand_pt() |
| 389 |
B = rand_pt() |
| 390 |
C = rand_pt() |
| 391 |
G = Centre(Triangle(A, B, C)) |
| 392 |
self.assertAlmostEqual(G.x, (A.x + B.x + C.x)/3) |
| 393 |
self.assertAlmostEqual(G.y, (A.y + B.y + C.y)/3) |
| 394 |
ce = Cercle(A, 1) |
| 395 |
G = Centre_gravite(ce) |
| 396 |
self.assertAlmostEqual(G.xy, A.xy) |