linecros.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. d = D;
  60. d1 = D1;
  61. d2 = D2;
  62. if (d > 0)
  63. return (d1 >= 0 && d2 >= 0 && d >= d1 && d >= d2);
  64. if (d < 0)
  65. return (d1 <= 0 && d2 <= 0 && d <= d1 && d <= d2);
  66. /* lines are parallel */
  67. if (d1 || d2)
  68. return 0;
  69. /* segments are colinear. check for overlap */
  70. if (ax1 != ax2) {
  71. /* segments are not parallel to y axis, can use x values */
  72. if (ax1 > ax2) {
  73. t = ax1;
  74. ax1 = ax2;
  75. ax2 = t;
  76. }
  77. if (bx1 > bx2) {
  78. t = bx1;
  79. bx1 = bx2;
  80. bx2 = t;
  81. }
  82. if (ax1 > bx2)
  83. return 0;
  84. if (ax2 < bx1)
  85. return 0;
  86. /* there is overlap */
  87. if (ax1 == bx2 || ax2 == bx1)
  88. return 1; /* endpoints only */
  89. return -1; /* true overlap */
  90. }
  91. else {
  92. /* segments are parallel to y axis, use y values */
  93. if (ay1 > ay2) {
  94. t = ay1;
  95. ay1 = ay2;
  96. ay2 = t;
  97. }
  98. if (by1 > by2) {
  99. t = by1;
  100. by1 = by2;
  101. by2 = t;
  102. }
  103. if (ay1 > by2)
  104. return 0;
  105. if (ay2 < by1)
  106. return 0;
  107. /* there is overlap */
  108. if (ay1 == by2 || ay2 == by1)
  109. return 1; /* endpoints only */
  110. return -1; /* true overlap */
  111. }
  112. return 0; /* should not be reached */
  113. }
  114. int
  115. dig_find_intersection(double ax1, double ay1,
  116. double ax2, double ay2,
  117. double bx1, double by1,
  118. double bx2, double by2, double *x, double *y)
  119. {
  120. register double d, r1, r2;
  121. double t;
  122. d = D;
  123. if (d) {
  124. r1 = D1 / d;
  125. r2 = D2 / d;
  126. if (r1 < 0 || r1 > 1 || r2 < 0 || r2 > 1) {
  127. return 0;
  128. }
  129. *x = ax1 + r1 * (ax2 - ax1);
  130. *y = ay1 + r1 * (ay2 - ay1);
  131. return 1;
  132. }
  133. /* lines are parallel */
  134. if (D1 || D2) {
  135. return 0;
  136. }
  137. /* segments are colinear. check for overlap */
  138. if (ax1 != ax2) {
  139. /* segments are not parallel to y axis, can use x values */
  140. if (ax1 > ax2) {
  141. /* need to swap both coords */
  142. t = ax1;
  143. ax1 = ax2;
  144. ax2 = t;
  145. t = ay1;
  146. ay1 = ay2;
  147. ay2 = t;
  148. }
  149. if (bx1 > bx2) {
  150. /* need to swap both coords */
  151. t = bx1;
  152. bx1 = bx2;
  153. bx2 = t;
  154. t = by1;
  155. by1 = by2;
  156. by2 = t;
  157. }
  158. if (ax1 > bx2)
  159. return 0;
  160. if (ax2 < bx1)
  161. return 0;
  162. /* there is overlap */
  163. if (ax1 == bx2) {
  164. *x = ax1;
  165. *y = ay1;
  166. return 1; /* endpoints only */
  167. }
  168. if (ax2 == bx1) {
  169. *x = ax2;
  170. *y = ay2;
  171. return 1; /* endpoints only */
  172. }
  173. /* overlap, no single intersection point */
  174. if (ax1 > bx1 && ax1 < bx2) {
  175. *x = ax1;
  176. *y = ay1;
  177. }
  178. else {
  179. *x = ax2;
  180. *y = ay2;
  181. }
  182. return -1;
  183. }
  184. else {
  185. /* segments are parallel to y axis, use y values */
  186. if (ay1 > ay2) {
  187. /* need to swap both coords */
  188. t = ay1;
  189. ay1 = ay2;
  190. ay2 = t;
  191. t = ax1;
  192. ax1 = ax2;
  193. ax2 = t;
  194. }
  195. if (by1 > by2) {
  196. /* need to swap both coords */
  197. t = by1;
  198. by1 = by2;
  199. by2 = t;
  200. t = by1;
  201. by1 = by2;
  202. by2 = t;
  203. }
  204. if (ay1 > by2)
  205. return 0;
  206. if (ay2 < by1)
  207. return 0;
  208. /* there is overlap */
  209. if (ay1 == by2) {
  210. *x = ax1;
  211. *y = ay1;
  212. return 1; /* endpoints only */
  213. }
  214. if (ay2 == by1) {
  215. *x = ax2;
  216. *y = ay2;
  217. return 1; /* endpoints only */
  218. }
  219. /* overlap, no single intersection point */
  220. if (ay1 > by1 && ay1 < by2) {
  221. *x = ax1;
  222. *y = ay1;
  223. }
  224. else {
  225. *x = ax2;
  226. *y = ay2;
  227. }
  228. return -1;
  229. }
  230. return 0; /* should not be reached */
  231. }