linecros.c 4.9 KB

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