linecros.c 4.8 KB

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