linecros.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: Vector library
  5. *
  6. * AUTHOR(S): Original author CERL, probably Dave Gerdes.
  7. * Update to GRASS 5.7 Radim Blazek.
  8. *
  9. * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
  10. *
  11. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #include <stdio.h>
  19. /***************************************************************
  20. * test_for_intersection (ax1,ay1,ax2,ay2,bx1,by1,bx2,by2)
  21. * double ax1,ax2,ay1,ay2;
  22. * double bx1,bx2,by1,by2;
  23. *
  24. * returns
  25. * 0 no intersection at all
  26. * 1 the line segments intersect at only one point
  27. * -1 the line segments intersect at many points, i.e., overlapping
  28. * segments from the same line
  29. *
  30. * find_intersection (ax1,ay1,ax2,ay2,bx1,by1,bx2,by2,x,y)
  31. * double ax1,ax2,ay1,ay2;
  32. * double bx1,bx2,by1,by2;
  33. * double *x,*y;
  34. *
  35. * returns
  36. * 0 no intersection
  37. * 1 x,y set to (unique) intersection
  38. * -1 lines overlap, no unique intersection
  39. *
  40. * Based on the following:
  41. *
  42. * (ax2-ax1)r1 - (bx2-bx1)r2 = ax2 - ax1
  43. * (ay2-ay1)r1 - (by2-by1)r2 = ay2 - ay1
  44. *
  45. * Solving for r1 and r2, if r1 and r2 are between 0 and 1,
  46. * then line segments (ax1,ay1)(ax2,ay2) and (bx1,by1)(bx2,by2)
  47. * intersect
  48. ****************************************************************/
  49. #define D ((ax2-ax1)*(by1-by2) - (ay2-ay1)*(bx1-bx2))
  50. #define D1 ((bx1-ax1)*(by1-by2) - (by1-ay1)*(bx1-bx2))
  51. #define D2 ((ax2-ax1)*(by1-ay1) - (ay2-ay1)*(bx1-ax1))
  52. int
  53. dig_test_for_intersection(double ax1, double ay1,
  54. double ax2, double ay2,
  55. double bx1, double by1, double bx2, double by2)
  56. {
  57. register double d, d1, d2;
  58. double t;
  59. int switched;
  60. if (ax1 > ax2 || (ax1 == ax2 && ay1 > ay2)) {
  61. t = ax1;
  62. ax1 = ax2;
  63. ax2 = t;
  64. t = ay1;
  65. ay1 = ay2;
  66. ay2 = t;
  67. }
  68. if (bx1 > bx2 || (bx1 == bx2 && by1 > by2)) {
  69. t = bx1;
  70. bx1 = bx2;
  71. bx2 = t;
  72. t = by1;
  73. by1 = by2;
  74. by2 = t;
  75. }
  76. switched = 0;
  77. if (bx1 < ax1)
  78. switched = 1;
  79. else if (bx1 == ax1) {
  80. if (bx2 < ax2)
  81. switched = 1;
  82. else if (bx2 == ax2) {
  83. if (by1 < ay1)
  84. switched = 1;
  85. else if (by1 == ay1) {
  86. if (by2 < ay2)
  87. switched = 1;
  88. }
  89. }
  90. }
  91. if (switched) {
  92. t = ax1;
  93. ax1 = bx1;
  94. bx1 = t;
  95. t = ax2;
  96. ax2 = bx2;
  97. bx2 = t;
  98. t = ay1;
  99. ay1 = by1;
  100. by1 = t;
  101. t = ay2;
  102. ay2 = by2;
  103. by2 = t;
  104. }
  105. d = D;
  106. d1 = D1;
  107. d2 = D2;
  108. if (d > 0)
  109. return (d1 >= 0 && d2 >= 0 && d >= d1 && d >= d2);
  110. if (d < 0)
  111. return (d1 <= 0 && d2 <= 0 && d <= d1 && d <= d2);
  112. /* lines are parallel */
  113. if (d1 || d2)
  114. return 0;
  115. /* segments are colinear. check for overlap */
  116. /* Collinear vertical */
  117. if (ax1 == ax2) {
  118. if (ay1 > ay2) {
  119. t = ay1;
  120. ay1 = ay2;
  121. ay2 = t;
  122. }
  123. if (by1 > by2) {
  124. t = by1;
  125. by1 = by2;
  126. by2 = t;
  127. }
  128. if (ay1 > by2)
  129. return 0;
  130. if (ay2 < by1)
  131. return 0;
  132. /* there is overlap */
  133. if (ay1 == by2 || ay2 == by1)
  134. return 1; /* endpoints only */
  135. return -1; /* true overlap */
  136. }
  137. else {
  138. if (ax1 > ax2) {
  139. t = ax1;
  140. ax1 = ax2;
  141. ax2 = t;
  142. }
  143. if (bx1 > bx2) {
  144. t = bx1;
  145. bx1 = bx2;
  146. bx2 = t;
  147. }
  148. if (ax1 > bx2)
  149. return 0;
  150. if (ax2 < bx1)
  151. return 0;
  152. /* there is overlap */
  153. if (ax1 == bx2 || ax2 == bx1)
  154. return 1; /* endpoints only */
  155. return -1; /* true overlap */
  156. }
  157. return 0; /* should not be reached */
  158. }
  159. int
  160. dig_find_intersection(double ax1, double ay1,
  161. double ax2, double ay2,
  162. double bx1, double by1,
  163. double bx2, double by2, double *x, double *y)
  164. {
  165. register double d, r1, r2;
  166. double t;
  167. int switched;
  168. if (ax1 > ax2 || (ax1 == ax2 && ay1 > ay2)) {
  169. t = ax1;
  170. ax1 = ax2;
  171. ax2 = t;
  172. t = ay1;
  173. ay1 = ay2;
  174. ay2 = t;
  175. }
  176. if (bx1 > bx2 || (bx1 == bx2 && by1 > by2)) {
  177. t = bx1;
  178. bx1 = bx2;
  179. bx2 = t;
  180. t = by1;
  181. by1 = by2;
  182. by2 = t;
  183. }
  184. switched = 0;
  185. if (bx1 < ax1)
  186. switched = 1;
  187. else if (bx1 == ax1) {
  188. if (bx2 < ax2)
  189. switched = 1;
  190. else if (bx2 == ax2) {
  191. if (by1 < ay1)
  192. switched = 1;
  193. else if (by1 == ay1) {
  194. if (by2 < ay2)
  195. switched = 1;
  196. }
  197. }
  198. }
  199. if (switched) {
  200. t = ax1;
  201. ax1 = bx1;
  202. bx1 = t;
  203. t = ax2;
  204. ax2 = bx2;
  205. bx2 = t;
  206. t = ay1;
  207. ay1 = by1;
  208. by1 = t;
  209. t = ay2;
  210. ay2 = by2;
  211. by2 = t;
  212. }
  213. d = D;
  214. if (d) {
  215. r1 = D1 / d;
  216. r2 = D2 / d;
  217. if (r1 < 0 || r1 > 1 || r2 < 0 || r2 > 1) {
  218. return 0;
  219. }
  220. *x = ax1 + r1 * (ax2 - ax1);
  221. *y = ay1 + r1 * (ay2 - ay1);
  222. return 1;
  223. }
  224. /* lines are parallel */
  225. if (D1 || D2) {
  226. return 0;
  227. }
  228. /* segments are colinear. check for overlap */
  229. /* Collinear vertical */
  230. if (ax1 == ax2) {
  231. if (ay1 > by2)
  232. return 0;
  233. if (ay2 < by1)
  234. return 0;
  235. /* there is overlap */
  236. if (ay1 == by2) {
  237. *x = ax1;
  238. *y = ay1;
  239. return 1; /* endpoints only */
  240. }
  241. if (ay2 == by1) {
  242. *x = ax2;
  243. *y = ay2;
  244. return 1; /* endpoints only */
  245. }
  246. /* overlap, no single intersection point */
  247. if (ay1 > by1 && ay1 < by2) {
  248. *x = ax1;
  249. *y = ay1;
  250. }
  251. else {
  252. *x = ax2;
  253. *y = ay2;
  254. }
  255. return -1;
  256. }
  257. else {
  258. if (ax1 > bx2)
  259. return 0;
  260. if (ax2 < bx1)
  261. return 0;
  262. /* there is overlap */
  263. if (ax1 == bx2) {
  264. *x = ax1;
  265. *y = ay1;
  266. return 1; /* endpoints only */
  267. }
  268. if (ax2 == bx1) {
  269. *x = ax2;
  270. *y = ay2;
  271. return 1; /* endpoints only */
  272. }
  273. /* overlap, no single intersection point */
  274. if (ax1 > bx1 && ax1 < bx2) {
  275. *x = ax1;
  276. *y = ay1;
  277. }
  278. else {
  279. *x = ax2;
  280. *y = ay2;
  281. }
  282. return -1;
  283. }
  284. return 0; /* should not be reached */
  285. }