1 #------------------------------------------------------------------------------# 2 #------------------------------------------------------------------------------# 3 import unittest; 4 from geometry2d import *; 5 #------------------------------------------------------------------------------# 6 7 # Beachte: Ich habe nur soweit getestet, 8 # wie ich es fuer meine angepeilten Zwecke brauchte! 9 10 class TestVectors(unittest.TestCase): 11 def test_str(self): 12 vc1=Vectors(1,2); 13 self.assertTrue(str(vc1)=='1, 2'); 14 15 def test_eq(self): 16 vc1=Vectors(1,2); 17 self.assertEqual(vc1,Vectors(1,2)); 18 self.assertEqual(vc1,Vectors(1.0,2.001)); 19 self.assertNotEqual(vc1,Vectors(1.0,2.01)); 20 self.assertNotEqual(vc1,Vectors(1,3)); 21 22 def test_toUnitVector(self): 23 vc1=Vectors(1,2); 24 vc2=Vectors(1/sqrt(5),2/sqrt(5)); 25 vc3=vc1.toUnitVector(); 26 self.assertEqual(vc2,vc3); 27 28 def test_lengthOf(self): 29 vc1=Vectors(1,2); 30 vc3=vc1.toUnitVector(); 31 self.assertNotEqual(vc3.lengthOf(),1); 32 self.assertTrue(abs(vc3.lengthOf()-1) <epsilon); 33 34 def test_add(self): 35 vc1=Vectors(1,2); vc2=Vectors(2,4); 36 self.assertEqual(vc1+vc1,vc2); 37 38 def test_sub(self): 39 vc1=Vectors(1,2); vc2=Vectors(2,4); 40 vc3=Vectors(1-2,2-4); 41 self.assertEqual(vc1-vc2,vc3); 42 43 def test_mul(self): 44 vc1=Vectors(1,2); 45 self.assertEqual(vc1*2,Vectors(2,4)); 46 self.assertEqual(2*vc1,Vectors(2,4)); 47 48 def test_smul(self): 49 vc1=Vectors(1,2); vc2=Vectors(2,4); 50 vc1.smul(2); 51 self.assertEqual(vc1,vc2); 52 53 def test_rmul(self): 54 vc1=Vectors(1.0,2.0); vc2=Vectors(2,4); 55 vc3=vc1*2; # Uses __mul__ 56 self.assertEqual(vc3,vc2); 57 58 def test_truediv(self): 59 vc1=Vectors(1,2); vc2=Vectors(2.0,4.0); 60 vc3=vc2/2; # Uses __truediv__ 61 # PyCharm warning ??? 62 ok=(vc3==vc1); 63 self.assertTrue(ok); 64 65 def test_sdiv(self): 66 vc1=Vectors(1,2); vc2=Vectors(2,4); 67 vc2.sdiv(2); 68 self.assertEqual(vc2,vc1); 69 70 def test_dotOp(self): 71 vc1=Vectors(1,2); 72 self.assertEqual(Vectors.dotOp(vc1,vc1),5); 73 self.assertEqual(Vectors.dotOp(Vectors(1,1),Vectors(-1,1)),0); 74 75 def Test_crossOp(self): 76 vc1=Vectors(1,2); 77 self.assertEqual(Vectors.crossOp(vc1,vc1),0); 78 self.assertEqual(Vectors.crossOp(vc1,Vectors(1,3)),1); 79 80 def test_dirAngleOf(self): 81 vc4=Vectors(1,1); 82 self.assertEqual(vc4.dirAngleOf()*r2d,45); 83 vc4=Vectors(-1,1); 84 self.assertEqual(vc4.dirAngleOf()*r2d,135); 85 vc4=Vectors(-1,-1); 86 self.assertEqual(vc4.dirAngleOf()*r2d,225); 87 88 def test_sameDirectionOf(self): 89 vc1=Vectors(1,2); 90 self.assertTrue(vc1.sameDirectionOf(vc1)); 91 self.assertTrue(vc1.sameDirectionOf(Vectors(1.0,2.000001))); 92 self.assertFalse(vc1.sameDirectionOf(Vectors(1.0,2.00001))); 93 # end TestVectors 94 #------------------------------------------------------------------------------# 95 96 class TestPoints(unittest.TestCase): 97 def test_eq(self): 98 pt1=Points(1,2); pt2=Points(3,2); 99 self.assertEqual(pt1,pt1); 100 self.assertNotEqual(pt1,pt2); 101 102 def test_str(self): 103 pt1=Points(1,2); 104 self.assertEqual(str(pt1),'1, 2'); 105 106 def test_distanceOf(self): 107 pt1=Points(1,2); pt2=Points(3,2); 108 self.assertEqual(pt1.distanceOf(pt1),0.0); 109 self.assertEqual(pt1.distanceOf(pt2),2.0); 110 self.assertEqual(pt2.distanceOf(pt1),2.0); 111 112 def test_sub(self): 113 pt1=Points(1,2); pt2=Points(3,2); 114 self.assertEqual((pt2-pt1),Vectors(2,0)); 115 116 def test_add(self): 117 pt1=Points(1,2); vc1=Vectors(1,1); 118 self.assertEqual((pt1+vc1),Points(2,3)); 119 120 def test_areaOf(self): 121 pt3=Points(0,0); pt4=Points(2,0); 122 pt5=Points(1,1); 123 self.assertEqual(pt3.areaOf(pt4,pt5),1.0); 124 self.assertEqual(pt3.areaOf(pt5,pt4),-1.0); 125 # end TestPoints 126 #------------------------------------------------------------------------------# 127 128 class TestLines(unittest.TestCase): 129 def test_eq(self): 130 pt1=Points(0,0); pt2=Points(2,2); 131 pt3=Points(0,2); pt4=Points(2,0); 132 pt5=Points(-2.0,-2.0); pt6=Points(3,3); 133 L12=Lines.toLine(pt1,pt2); 134 L34=Lines.toLine(pt3,pt4); 135 L56=Lines.toLine(pt6,pt5); 136 self.assertEqual(L12,L12); 137 self.assertEqual(L12,L56); 138 self.assertNotEqual(L12,L34); 139 140 def test_intersectionOf(self): 141 pt1=Points(0,0); pt2=Points(2,2); 142 pt3=Points(0,2); pt4=Points(2,0); 143 L12=Lines.toLine(pt1,pt2); 144 L34=Lines.toLine(pt3,pt4); 145 ipt=Lines.intersectionOf(L12,L34); 146 pt5=Points(1,1); 147 self.assertEqual(ipt,pt5); 148 with self.assertRaises(InfinityPoints): 149 Lines.intersectionOf(L12,L12); 150 pt7=Points(1,0); pt8=Points(3,2); 151 L78=Lines.toLine(pt7,pt8); 152 with self.assertRaises(NoPoint): 153 Lines.intersectionOf(L12,L78); 154 155 def test_nearestPointOf(self): 156 pt1=Points(0,0); pt2=Points(2,2); 157 L12=Lines.toLine(pt1,pt2); 158 pt5=Points(1,1); 159 self.assertEqual(L12.nearestPointOf(pt5),pt5); 160 self.assertEqual(L12.nearestPointOf(Points(0,2)),pt5); 161 162 def test_isPointInside(self): 163 pt1=Points(0,0); pt2=Points(2,2); 164 L12=Lines.toLine(pt1,pt2); 165 pt3=Points(0,2); pt6=Points(3,3); 166 self.assertTrue(L12.isPointInside(pt6)); 167 self.assertFalse(L12.isPointInside(pt3)); 168 169 def test_distanceOf(self): 170 pt1=Points(0,0); pt2=Points(2,2); 171 L12=Lines.toLine(pt1,pt2); 172 pt3=Points(0,2); 173 self.assertTrue(abs(abs(L12.distanceOf(pt3))-sqrt(2))<epsilon); 174 self.assertTrue(abs(L12.distanceOf(pt3)-sqrt(2))<epsilon); 175 pt4=Points(1,1); 176 self.assertTrue(abs(L12.distanceOf(pt4))<epsilon); 177 178 def test_areParallel(self): 179 pt1=Points(0,0); pt2=Points(2,2); 180 L12=Lines.toLine(pt1,pt2); 181 self.assertFalse(Lines.areParallel(L12,L12)); 182 pt7=Points(1,0); pt8=Points(3,2); 183 L78=Lines.toLine(pt7,pt8); 184 self.assertTrue(Lines.areParallel(L12,L78)); 185 pt1a=Points(1,1); pt2a=Points(3,3); 186 L12a=Lines.toLine(pt1a,pt2a); 187 self.assertFalse(Lines.areParallel(L12,L12a)); 188 self.assertEqual(L12,L12a); 189 190 def test_getGradientVector(self): 191 pt1=Points(0,0); pt2=Points(2,2); 192 L12=Lines.toLine(pt1,pt2); 193 gv=L12.getNormalVector(); 194 self.assertTrue(str(gv),'0.7071067811865475, -0.7071067811865475'); 195 self.assertTrue(gv.isOrthogonalTo(L12.DV)); 196 # end test_getGradientVector 197 198 def test_getParallelLine(self)->'Lines': 199 pt1=Points(0,0); pt2=Points(2,2); 200 L12=Lines.toLine(pt1,pt2); 201 dist=sqrt(2); 202 Lp=L12.getParallelLine(dist); 203 pt3=Points(1,1); pt4=Points(3,3); 204 L34=Lines.toLine(pt3,pt4); 205 self.assertTrue(L34.areParallel(Lp)); 206 dist1=L12.distanceOf(pt3); 207 self.assertTrue((abs(dist1)-dist)<epsilon); 208 # end test_getParallelLine 209 # end TestLines 210 #------------------------------------------------------------------------------# 211 212 class TestRays(unittest.TestCase): 213 def test_eq(self): 214 sp1=Points(1,1); dir1=Vectors(1,1); 215 ray1=Rays(sp1,dir1); 216 self.assertEqual(ray1,ray1); 217 218 def test_str(self): 219 sp1=Points(1,1); dir1=Vectors(1,1); 220 ray1=Rays(sp1,dir1); 221 self.assertEqual(str(ray1),'SP: 1, 1 | DV: 0.7071067811865475, 0.7071067811865475'); 222 223 def test_toLine(self): 224 sp1=Points(1,1); dir1=Vectors(1,1); 225 ray1=Rays(sp1,dir1); 226 ln1=ray1.toLine(); 227 ln2=Lines(0.707106781186545,-0.7071067811865475,0.0); 228 self.assertEqual(ln1,ln2); 229 230 def test_intersectionOf(self): 231 sp1=Points(1,1); dir1=Vectors(1,1); 232 ray1=Rays(sp1,dir1); 233 sp2=Points(3,1); dir2=Vectors(-1,1); 234 ray2=Rays(sp2,dir2); 235 ip12=Rays.intersectionOf(ray1,ray2); 236 self.assertEqual(ip12,Points(2,2)); 237 sp3=Points(1,1); dir3=Vectors(-1,1); 238 ray3=Rays(sp3,dir3); 239 with self.assertRaises(NoPoint): 240 Rays.intersectionOf(ray1,ray3); 241 with self.assertRaises(InfinityPoints): 242 Rays.intersectionOf(ray1,ray1); 243 sp4=Points(1,1); dir4=Vectors(-1,-1); 244 ray4=Rays(sp4,dir4); 245 with self.assertRaises(NoPoint): 246 Rays.intersectionOf(ray4,ray1); 247 pt1a=Points(3,3); pt1b=Points(5,4); 248 ry1a=Rays.toRay(pt1a,pt1b); 249 pt2a=Points(1,1); pt2b=Points(3,2); 250 ry2a=Rays.toRay(pt2a,pt2b); 251 ry2b=Rays.toRay(pt2b,pt2a); 252 with self.assertRaises(NoPoint): 253 Rays.intersectionOf(ry1a,ry2a); 254 with self.assertRaises(NoPoint): 255 Rays.intersectionOf(ry1a,ry2b); 256 ry3a=Rays.toRay(pt1b,pt1a); 257 with self.assertRaises(NoPoint): 258 Rays.intersectionOf(ry3a,ry2a); 259 with self.assertRaises(NoPoint): 260 Rays.intersectionOf(ry3a,ry2b); 261 # end TestRays 262 #------------------------------------------------------------------------------# 263 264 class TestSectors(unittest.TestCase): 265 def test_eq(self): 266 sp1=Points(2,0); 267 dv1=Vectors(-0.7071067811865475,0.7071067811865475); 268 dv3=Vectors(-0.7071067811865476,0.7071067811865476); 269 dv2=Vectors(0.4472135954999579, 0.8944271909999159); 270 dv4=Vectors(0.447213595499958, 0.894427190999916); 271 sc1=Sectors(sp1,dv1,dv2); 272 sc2=Sectors(sp1,dv3,dv4); 273 self.assertTrue( sc1==sc2); 274 sc3=Sectors(sp1,dv1,dv2); 275 sc4=Sectors(sp1,dv4,dv3); 276 self.assertTrue( sc3==sc4); 277 278 def test_toSector(self): 279 pt0=Points(0,0); 280 pt1=Points(1,1); pt2=Points(-1,1); 281 pt3=Points(2,2); pt4=Points(-2,2) 282 sc1=Sectors.toSector(pt0,pt1,pt2); 283 sc2=Sectors.toSector(pt0,pt3,pt4); 284 self.assertEqual(sc1,sc2); 285 # end TestSectors 286 #------------------------------------------------------------------------------# 287 288 class TestSegments(unittest.TestCase): 289 def test_eq(self): 290 pt1=Points(1,1); pt2=Points(2,1); 291 sgm=Segments(pt1,pt2); 292 self.assertEqual(sgm,sgm); 293 294 def test_str(self): 295 pt1=Points(1,1); pt2=Points(2,1); 296 sgm=Segments(pt1,pt2); 297 self.assertEqual(str(sgm),'1, 1 | 2, 1'); 298 299 def test_intersectionOf_withRay(self): 300 sp1=Points(2.5,0); dir1=Vectors(-1,1); 301 ray1=Rays(sp1,dir1); 302 pt1=Points(1,1); pt2=Points(2,1); 303 sgm=Segments(pt1,pt2); 304 ip1=sgm.intersectionOf_withRay(ray1); 305 self.assertEqual(ip1,Points(1.5,1.0)); 306 self.assertEqual(sgm.distanceOf(pt2),0.0); 307 pt0=Points(0,0); dir0=Vectors(1,1); 308 ray0=Rays(pt0,dir0); 309 with self.assertRaises(NoPoint): 310 sgm.intersectionOf_withRay(ray0); 311 pt3=Points(1.5,2); 312 self.assertEqual(sgm.distanceOf(pt3),1.0); 313 pt3=Points(1.5,0); 314 self.assertEqual(sgm.distanceOf(pt3),-1.0); 315 pt1a=Points(1.4,0); 316 pt2a=Points(1.4,2); 317 sgma=Segments(pt1a,pt2a); 318 ipa=sgm.intersectionOf(sgma); 319 self.assertEqual(ipa,Points(1.4,1)); 320 pt1a=Points(1.3,0); 321 pt2b=Points(4,3); 322 sgmb=Segments(pt1a,pt2b); 323 with self.assertRaises(NoPoint): 324 sgm.intersectionOf(sgmb); 325 pt4a=Points(1,1); 326 pt4b=Points(2,2); 327 sgm4=Segments(pt4a,pt4b); 328 pt5a=Points(1.5,1.5); 329 pt5b=Points(2.5,2.5); 330 sgm5=Segments(pt5a,pt5b); 331 with self.assertRaises(InfinityPoints): 332 sgm4.intersectionOf(sgm5); 333 334 def test_distanceOf(self): 335 pt1=Points(1,1); pt2=Points(3,1); 336 sgm=Segments(pt1,pt2); 337 pta=Points(2,2); 338 self.assertEqual(1,sgm.distanceOf(pta)); 339 pta=Points(2,0); 340 self.assertEqual(-1,sgm.distanceOf(pta)); 341 pta=Points(0,1); 342 self.assertEqual(1,sgm.distanceOf(pta)); 343 pta=Points(0,0); 344 self.assertEqual(-sqrt(2),sgm.distanceOf(pta)); 345 pta=Points(0,2); 346 self.assertEqual(sqrt(2),sgm.distanceOf(pta)); 347 pta=Points(4,0); 348 self.assertEqual(-sqrt(2),sgm.distanceOf(pta)); 349 pta=Points(4,2); 350 self.assertEqual(sqrt(2),sgm.distanceOf(pta)); 351 # end TestSegments 352 #------------------------------------------------------------------------------# 353 354 class TestSectorWithSegments(unittest.TestCase): 355 def test_getMinimalDistanceSegment(self): 356 sp0=Points(0,0); 357 dva=Vectors(1,0); dvb=Vectors(0,1); 358 secwsgm=SectorWithSegments(sp0,dva,dvb); 359 sgm1=Segments(Points(5,0),Points(0,4)); 360 sgm2=Segments(Points(4,0),Points(0,4)); 361 sgm3=Segments(Points(4,0),Points(0,5)); 362 sgms=[sgm1,sgm2,sgm3]; 363 for sgm in sgms: 364 ok=secwsgm.add(sgm); 365 if not ok: raise InvalidDataError('Test data not ok!'); 366 self.assertEqual(str(secwsgm),"SP: 0, 0 | DV1: 1.0, 0.0 | DV2: 0.0, 1.0 || 5, 0 | 0, 4 | 4, 0 | 0, 4 | 4, 0 | 0, 5 | ") 367 minsgm=secwsgm.getMinimalDistanceSegment(); 368 self.assertEqual(str(minsgm),'4, 0 | 0, 4'); 369 # end SectorWithSegments 370 #------------------------------------------------------------------------------# 371 372 class TestPolygons(unittest.TestCase): 373 def test_areaOf(self): 374 pt1=Points(1,0); pt2=Points(2,1); pt3=Points(0,1); 375 pts1=[pt1,pt2,pt3]; 376 pg1=Polygons(pts1); 377 self.assertTrue(pg1.areaOf()==1.0); 378 pts2=[pt1,pt3,pt2]; 379 pg2=Polygons(pts2); 380 self.assertEqual(pg2.areaOf(),-1.0); 381 382 def test_distanceOf(self): 383 pt1=Points(1,0); pt2=Points(2,1); pt3=Points(0,1); 384 pts1=[pt1,pt2,pt3]; 385 pg1=Polygons(pts1); 386 pt4=Points(2,2); 387 self.assertEqual(pg1.distanceOf(pt4),-1.0); 388 389 def test_isPointOnside(self): 390 pt1=Points(1,1); pt2=Points(1,2); 391 pt3=Points(2,2); pt4=Points(2,1); 392 ptL1=[pt1,pt2,pt3,pt4]; 393 pg1=Polygons(ptL1); 394 ptT1=Points(1.75,1.75); 395 self.assertTrue(pg1.isPointOnside(ptT1)); 396 ptT2=Points(2.5,2.5); 397 self.assertFalse(pg1.isPointOnside(ptT2)); 398 pt5=Points(1.25,2.0); pt6=Points(1.5,1.25); pt7=Points(1.75,2.0); 399 ptL2=[pt1,pt2,pt5,pt6,pt7,pt3,pt4]; 400 pg2=Polygons(ptL2); 401 self.assertFalse(pg2.isPointOnside(ptT1)); 402 self.assertTrue(pg2.isPointOnside(pt1)); 403 # end TestPolygons 404 #------------------------------------------------------------------------------# 405 406 class TestTriangles(unittest.TestCase): 407 def test_areaOf(self): 408 pt1=Points(1,0); pt2=Points(2,1); pt3=Points(0,1); 409 tri=Triangles(pt1,pt2,pt3); 410 self.assertEqual(tri.areaOf(),1.0); 411 412 def test_isPointInside(self): 413 pt1=Points(1,0); pt2=Points(2,1); pt3=Points(0,1); 414 tri=Triangles(pt1,pt2,pt3); 415 p4=Points(3,3); 416 self.assertFalse(tri.isPointInside(p4)); 417 p4a=Points(1,2); 418 self.assertFalse(tri.isPointInside(p4a)); 419 p4b=Points(0,0); 420 self.assertFalse(tri.isPointInside(p4b)); 421 p4c=Points(2,-1); 422 self.assertFalse(tri.isPointInside(p4c)); 423 p4d=Points(1.0,-0.01); 424 self.assertFalse(tri.isPointInside(p4d)); 425 p5=Points(1.0,0.5); 426 self.assertTrue(tri.isPointInside(p5)); 427 # end TestTriangles 428 #------------------------------------------------------------------------------# 429 430 class TestCircles(unittest.TestCase): 431 def test_isPointInside(self): 432 pt1=Points(2,2); 433 cc1=Circles(pt1,1); 434 self.assertFalse(cc1.isPointInside(Points(1,1))); 435 self.assertTrue(cc1.isPointInside(Points(1.5,2))); 436 437 def test_str(self): 438 pt1=Points(2.0,1.0); 439 cc1=Circles(pt1,1.0); 440 self.assertEqual(str(cc1),'2.0, 1.0 | 1.0'); 441 442 def test_toCircle(self): 443 pt2=Points(1,1); pt3=Points(2,0); pt4=Points(2,2); 444 cc2=Circles.toCircle(pt2,pt3,pt4); 445 self.assertEqual(str(cc2),'2.0, 1.0 | 1.0'); 446 pt5=Points(1,1); pt6=Points(2,1); pt7=Points(3,1); 447 with self.assertRaises(Circles.NoCircle): 448 Circles.toCircle(pt5,pt6,pt7); 449 # end TestCircles 450 #------------------------------------------------------------------------------# 451 452 class TestPolygonTrains(unittest.TestCase): 453 def test_init(self): 454 pt1=Points(1,0); pt2=Points(2,1); pt3=Points(3,0); 455 ptList=[pt1,pt2,pt3]; 456 polyT=PolygonTrains(ptList); 457 sgms=polyT.getSegments(); 458 self.assertEqual(sgms[0],Segments(pt1,pt2)); 459 self.assertEqual(sgms[1],Segments(pt2,pt3)); 460 # end TestPolygonTrains 461 #------------------------------------------------------------------------------# 462 #------------------------------------------------------------------------------# 463 464 if __name__ == '__main__': 465 unittest.main(); 466 467 #------------------------------------------------------------------------------# 468 #------------------------------------------------------------------------------#