geo2d.py 12 KB

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