geo2d.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. from __future__ import division, absolute_import, print_function, unicode_literals
  2. from awlsim_tstlib import *
  3. initTest(__file__)
  4. from awlsim.gui.geo2d import *
  5. class Test_LineSeg2D(TestCase):
  6. def test_point(self):
  7. p = Point2D()
  8. self.assertEqual(p.x, 0)
  9. self.assertEqual(p.y, 0)
  10. self.assertFalse(p)
  11. p = Point2D(42, -42)
  12. self.assertEqual(p.x, 42)
  13. self.assertEqual(p.y, -42)
  14. self.assertEqual(p, Point2D(42, -42))
  15. self.assertNotEqual(p, Point2D(42, 42))
  16. self.assertNotEqual(p, Point2D(-42, -42))
  17. self.assertTrue(p)
  18. def test_vect(self):
  19. v = Vect2D()
  20. self.assertEqual(v.x, 0)
  21. self.assertEqual(v.y, 0)
  22. self.assertFalse(v)
  23. v = Vect2D(42, -42)
  24. self.assertEqual(v.x, 42)
  25. self.assertEqual(v.y, -42)
  26. self.assertEqual(v, Vect2D(42, -42))
  27. self.assertNotEqual(v, Vect2D(42, 42))
  28. self.assertNotEqual(v, Vect2D(-42, -42))
  29. self.assertTrue(v)
  30. def test_lineseg(self):
  31. s = LineSeg2D.fromCoords(3, 4, 3, 4)
  32. self.assertEqual(s.pointA, Point2D(3, 4))
  33. self.assertEqual(s.pointB, Point2D(3, 4))
  34. self.assertFalse(s)
  35. s = LineSeg2D.fromCoords(1, 2, 3, 4)
  36. self.assertEqual(s.pointA, Point2D(1, 2))
  37. self.assertEqual(s.pointB, Point2D(3, 4))
  38. self.assertTrue(s)
  39. self.assertEqual(LineSeg2D.fromCoords(1, 2, 3, 4), LineSeg2D.fromCoords(1, 2, 3, 4))
  40. self.assertNotEqual(LineSeg2D.fromCoords(1, 2, 3, 4), LineSeg2D.fromCoords(-1, 2, 3, 4))
  41. self.assertNotEqual(LineSeg2D.fromCoords(1, 2, 3, 4), LineSeg2D.fromCoords(1, -2, 3, 4))
  42. self.assertNotEqual(LineSeg2D.fromCoords(1, 2, 3, 4), LineSeg2D.fromCoords(1, 2, -3, 4))
  43. self.assertNotEqual(LineSeg2D.fromCoords(1, 2, 3, 4), LineSeg2D.fromCoords(1, 2, 3, -4))
  44. self.assertTrue(s)
  45. def test_intersection_base(self):
  46. inter = Inter2D()
  47. self.assertEqual(inter, Inter2D())
  48. self.assertNotEqual(inter, Inter2D(Point2D(1, 1)))
  49. self.assertFalse(inter.intersects)
  50. self.assertIsNone(inter.point)
  51. self.assertEqual(inter.vect, Vect2D())
  52. self.assertFalse(inter)
  53. inter = Inter2D(Point2D(42, -42), Vect2D(142, -142), False)
  54. self.assertEqual(inter, Inter2D(Point2D(42, -42), Vect2D(142, -142), False))
  55. self.assertNotEqual(inter, Inter2D(Point2D(42, -42), Vect2D(143, -142), False))
  56. self.assertFalse(inter.intersects)
  57. self.assertEqual(inter.point, Point2D(42, -42))
  58. self.assertEqual(inter.vect, Vect2D(142, -142))
  59. self.assertFalse(inter)
  60. inter = Inter2D(Point2D(42, -42), Vect2D(142, -142), True)
  61. self.assertEqual(inter, Inter2D(Point2D(42, -42), Vect2D(142, -142), True))
  62. self.assertNotEqual(inter, Inter2D(Point2D(42, -42), Vect2D(142, -142), False))
  63. self.assertTrue(inter.intersects)
  64. self.assertEqual(inter.point, Point2D(42, -42))
  65. self.assertEqual(inter.vect, Vect2D(142, -142))
  66. self.assertTrue(inter)
  67. inter = Inter2D(None, Vect2D(142, -142), True)
  68. self.assertFalse(inter.intersects)
  69. self.assertIsNone(inter.point)
  70. self.assertEqual(inter.vect, Vect2D(142, -142))
  71. self.assertFalse(inter)
  72. def test_intersection(self):
  73. # intersecting
  74. inter = LineSeg2D(Point2D(2, 7), Point2D(8, 1)).intersection(
  75. LineSeg2D(Point2D(9, 8), Point2D(3, 2)))
  76. self.assertTrue(inter.intersects)
  77. self.assertEqual(inter.point, Point2D(5, 4))
  78. self.assertEqual(inter.vect, Vect2D())
  79. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(5, 4), Point2D(5, 4)))
  80. # not intersecting
  81. inter = LineSeg2D(Point2D(1, 1), Point2D(4, 5)).intersection(
  82. LineSeg2D(Point2D(6, 6), Point2D(8, 2)))
  83. self.assertFalse(inter.intersects)
  84. self.assertEqual(inter.point, Point2D(5.5, 7))
  85. self.assertEqual(inter.vect, Vect2D())
  86. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(5.5, 7), Point2D(5.5, 7)))
  87. # parallel, horizontal, not intersecting
  88. inter = LineSeg2D(Point2D(3, 2), Point2D(9, 2)).intersection(
  89. LineSeg2D(Point2D(5, 3), Point2D(14, 3)))
  90. self.assertFalse(inter.intersects)
  91. self.assertIsNone(inter.point)
  92. self.assertEqual(inter.vect, Vect2D())
  93. self.assertIsNone(inter.lineSeg)
  94. # parallel, vertical, not intersecting
  95. inter = LineSeg2D(Point2D(3, 9), Point2D(3, 4)).intersection(
  96. LineSeg2D(Point2D(5, 2), Point2D(5, 5)))
  97. self.assertFalse(inter.intersects)
  98. self.assertIsNone(inter.point)
  99. self.assertEqual(inter.vect, Vect2D())
  100. self.assertIsNone(inter.lineSeg)
  101. # parallel, horizontal, intersecting
  102. inter = LineSeg2D(Point2D(3, 2), Point2D(9, 2)).intersection(
  103. LineSeg2D(Point2D(5, 2), Point2D(14, 2)))
  104. self.assertTrue(inter.intersects)
  105. self.assertEqual(inter.point, Point2D(9, 2))
  106. self.assertEqual(inter.vect, Vect2D(-4, 0))
  107. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(9, 2), Point2D(5, 2)))
  108. # parallel, horizontal, intersecting
  109. inter = LineSeg2D(Point2D(3, 2), Point2D(9, 2)).intersection(
  110. LineSeg2D(Point2D(14, 2), Point2D(5, 2)))
  111. self.assertTrue(inter.intersects)
  112. self.assertEqual(inter.point, Point2D(9, 2))
  113. self.assertEqual(inter.vect, Vect2D(-4, 0))
  114. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(9, 2), Point2D(5, 2)))
  115. # parallel, horizontal, intersecting
  116. inter = LineSeg2D(Point2D(9, 2), Point2D(3, 2)).intersection(
  117. LineSeg2D(Point2D(14, 2), Point2D(5, 2)))
  118. self.assertTrue(inter.intersects)
  119. self.assertEqual(inter.point, Point2D(9, 2))
  120. self.assertEqual(inter.vect, Vect2D(-4, 0))
  121. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(9, 2), Point2D(5, 2)))
  122. # parallel, horizontal, intersecting
  123. inter = LineSeg2D(Point2D(5, 2), Point2D(14, 2)).intersection(
  124. LineSeg2D(Point2D(3, 2), Point2D(9, 2)))
  125. self.assertTrue(inter.intersects)
  126. self.assertEqual(inter.point, Point2D(5, 2))
  127. self.assertEqual(inter.vect, Vect2D(4, 0))
  128. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(5, 2), Point2D(9, 2)))
  129. # parallel, vertical, intersecting
  130. inter = LineSeg2D(Point2D(3, 9), Point2D(3, 4)).intersection(
  131. LineSeg2D(Point2D(3, 2), Point2D(3, 5)))
  132. self.assertTrue(inter.intersects)
  133. self.assertEqual(inter.point, Point2D(3, 4))
  134. self.assertEqual(inter.vect, Vect2D(0, 1))
  135. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(3, 4), Point2D(3, 5)))
  136. # parallel, vertical, intersecting
  137. inter = LineSeg2D(Point2D(3, 2), Point2D(3, 5)).intersection(
  138. LineSeg2D(Point2D(3, 9), Point2D(3, 4)))
  139. self.assertTrue(inter.intersects)
  140. self.assertEqual(inter.point, Point2D(3, 5))
  141. self.assertEqual(inter.vect, Vect2D(0, -1))
  142. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(3, 5), Point2D(3, 4)))
  143. # parallel, vertical, intersecting
  144. inter = LineSeg2D(Point2D(60, 30), Point2D(60, 90)).intersection(
  145. LineSeg2D(Point2D(60, 30), Point2D(60, 70)))
  146. self.assertTrue(inter.intersects)
  147. self.assertEqual(inter.point, Point2D(60, 30))
  148. self.assertEqual(inter.vect, Vect2D(0, 40))
  149. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(60, 30), Point2D(60, 70)))
  150. # parallel, vertical, intersecting
  151. inter = LineSeg2D(Point2D(4, 8), Point2D(4, 2)).intersection(
  152. LineSeg2D(Point2D(4, 8), Point2D(4, 4)))
  153. self.assertTrue(inter.intersects)
  154. self.assertEqual(inter.point, Point2D(4, 8))
  155. self.assertEqual(inter.vect, Vect2D(0, -4))
  156. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(4, 8), Point2D(4, 4)))
  157. # parallel, vertical, intersecting
  158. inter = LineSeg2D(Point2D(4, 8), Point2D(4, 2)).intersection(
  159. LineSeg2D(Point2D(4, 6), Point2D(4, 2)))
  160. self.assertTrue(inter.intersects)
  161. self.assertEqual(inter.point, Point2D(4, 2))
  162. self.assertEqual(inter.vect, Vect2D(0, 4))
  163. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(4, 2), Point2D(4, 6)))
  164. # parallel, vertical, intersecting
  165. inter = LineSeg2D(Point2D(4, 8), Point2D(4, 2)).intersection(
  166. LineSeg2D(Point2D(4, 7), Point2D(4, 3)))
  167. self.assertTrue(inter.intersects)
  168. self.assertEqual(inter.point, Point2D(4, 7))
  169. self.assertEqual(inter.vect, Vect2D(0, -4))
  170. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(4, 7), Point2D(4, 3)))
  171. # parallel, vertical, intersecting
  172. inter = LineSeg2D(Point2D(4, 8), Point2D(4, 4)).intersection(
  173. LineSeg2D(Point2D(4, 8), Point2D(4, 2)))
  174. self.assertTrue(inter.intersects)
  175. self.assertEqual(inter.point, Point2D(4, 8))
  176. self.assertEqual(inter.vect, Vect2D(0, -4))
  177. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(4, 8), Point2D(4, 4)))
  178. # parallel, vertical, intersecting
  179. inter = LineSeg2D(Point2D(4, 6), Point2D(4, 2)).intersection(
  180. LineSeg2D(Point2D(4, 8), Point2D(4, 2)))
  181. self.assertTrue(inter.intersects)
  182. self.assertEqual(inter.point, Point2D(4, 6))
  183. self.assertEqual(inter.vect, Vect2D(0, -4))
  184. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(4, 6), Point2D(4, 2)))
  185. # parallel, vertical, intersecting
  186. inter = LineSeg2D(Point2D(4, 7), Point2D(4, 3)).intersection(
  187. LineSeg2D(Point2D(4, 8), Point2D(4, 2)))
  188. self.assertTrue(inter.intersects)
  189. self.assertEqual(inter.point, Point2D(4, 7))
  190. self.assertEqual(inter.vect, Vect2D(0, -4))
  191. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(4, 7), Point2D(4, 3)))
  192. # parallel, vertical, pointy intersection
  193. inter = LineSeg2D(Point2D(4, 1), Point2D(4, 5)).intersection(
  194. LineSeg2D(Point2D(4, 5), Point2D(4, 7)))
  195. self.assertTrue(inter.intersects)
  196. self.assertEqual(inter.point, Point2D(4, 5))
  197. self.assertEqual(inter.vect, Vect2D(0, 0))
  198. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(4, 5), Point2D(4, 5)))
  199. # parallel, vertical, pointy intersection
  200. inter = LineSeg2D(Point2D(4, 5), Point2D(4, 1)).intersection(
  201. LineSeg2D(Point2D(4, 5), Point2D(4, 7)))
  202. self.assertTrue(inter.intersects)
  203. self.assertEqual(inter.point, Point2D(4, 5))
  204. self.assertEqual(inter.vect, Vect2D(0, 0))
  205. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(4, 5), Point2D(4, 5)))
  206. # parallel, vertical, pointy intersection
  207. inter = LineSeg2D(Point2D(4, 5), Point2D(4, 1)).intersection(
  208. LineSeg2D(Point2D(4, 7), Point2D(4, 5)))
  209. self.assertTrue(inter.intersects)
  210. self.assertEqual(inter.point, Point2D(4, 5))
  211. self.assertEqual(inter.vect, Vect2D(0, 0))
  212. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(4, 5), Point2D(4, 5)))
  213. # parallel, vertical, pointy intersection
  214. inter = LineSeg2D(Point2D(4, 1), Point2D(4, 5)).intersection(
  215. LineSeg2D(Point2D(4, 7), Point2D(4, 5)))
  216. self.assertTrue(inter.intersects)
  217. self.assertEqual(inter.point, Point2D(4, 5))
  218. self.assertEqual(inter.vect, Vect2D(0, 0))
  219. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(4, 5), Point2D(4, 5)))
  220. # parallel, vertical, full intersection
  221. inter = LineSeg2D(Point2D(4, 1), Point2D(4, 7)).intersection(
  222. LineSeg2D(Point2D(4, 1), Point2D(4, 7)))
  223. self.assertTrue(inter.intersects)
  224. self.assertEqual(inter.point, Point2D(4, 1))
  225. self.assertEqual(inter.vect, Vect2D(0, 6))
  226. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(4, 1), Point2D(4, 7)))
  227. # parallel, vertical, full intersection
  228. inter = LineSeg2D(Point2D(4, 7), Point2D(4, 1)).intersection(
  229. LineSeg2D(Point2D(4, 7), Point2D(4, 1)))
  230. self.assertTrue(inter.intersects)
  231. self.assertEqual(inter.point, Point2D(4, 7))
  232. self.assertEqual(inter.vect, Vect2D(0, -6))
  233. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(4, 7), Point2D(4, 1)))
  234. # parallel, vertical, full intersection
  235. inter = LineSeg2D(Point2D(4, 1), Point2D(4, 7)).intersection(
  236. LineSeg2D(Point2D(4, 7), Point2D(4, 1)))
  237. self.assertTrue(inter.intersects)
  238. self.assertEqual(inter.point, Point2D(4, 1))
  239. self.assertEqual(inter.vect, Vect2D(0, 6))
  240. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(4, 1), Point2D(4, 7)))
  241. # parallel, vertical, full intersection
  242. inter = LineSeg2D(Point2D(4, 7), Point2D(4, 1)).intersection(
  243. LineSeg2D(Point2D(4, 1), Point2D(4, 7)))
  244. self.assertTrue(inter.intersects)
  245. self.assertEqual(inter.point, Point2D(4, 7))
  246. self.assertEqual(inter.vect, Vect2D(0, -6))
  247. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(4, 7), Point2D(4, 1)))
  248. # AB vertical, CD horizontal, intersecting
  249. inter = LineSeg2D(Point2D(4, 1), Point2D(4, 8)).intersection(
  250. LineSeg2D(Point2D(2, 5), Point2D(12, 5)))
  251. self.assertTrue(inter.intersects)
  252. self.assertEqual(inter.point, Point2D(4, 5))
  253. self.assertEqual(inter.vect, Vect2D())
  254. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(4, 5), Point2D(4, 5)))
  255. # AB vertical, CD horizontal, not intersecting
  256. inter = LineSeg2D(Point2D(x=180, y=130), Point2D(x=180, y=110)).intersection(
  257. LineSeg2D(Point2D(x=236, y=110), Point2D(x=240, y=110)))
  258. self.assertFalse(inter.intersects)
  259. self.assertEqual(inter.point, Point2D(180, 110))
  260. self.assertEqual(inter.vect, Vect2D())
  261. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(180, 110), Point2D(180, 110)))
  262. # AB horizontal, CD vertical, not intersecting
  263. inter = LineSeg2D(Point2D(9, 5), Point2D(4, 5)).intersection(
  264. LineSeg2D(Point2D(8, 6), Point2D(8, 11)))
  265. self.assertFalse(inter.intersects)
  266. self.assertEqual(inter.point, Point2D(8, 5))
  267. self.assertEqual(inter.vect, Vect2D())
  268. self.assertEqual(inter.lineSeg, LineSeg2D(Point2D(8, 5), Point2D(8, 5)))