linecros.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. t = ax1;
  72. ax1 = ax2;
  73. ax2 = t;
  74. }
  75. if (bx1 > bx2) {
  76. t = bx1;
  77. bx1 = bx2;
  78. bx2 = t;
  79. }
  80. if (ax1 > bx2)
  81. return 0;
  82. if (ax2 < bx1)
  83. return 0;
  84. /* there is overlap */
  85. if (ax1 == bx2 || ax2 == bx1)
  86. return 1; /* endpoints only */
  87. return -1; /* true overlap */
  88. }
  89. int
  90. dig_find_intersection(double ax1, double ay1,
  91. double ax2, double ay2,
  92. double bx1, double by1,
  93. double bx2, double by2, double *x, double *y)
  94. {
  95. register double d, r1, r2;
  96. double t;
  97. d = D;
  98. if (d) {
  99. r1 = D1 / d;
  100. r2 = D2 / d;
  101. if (r1 < 0 || r1 > 1 || r2 < 0 || r2 > 1) {
  102. return 0;
  103. }
  104. *x = ax1 + r1 * (ax2 - ax1);
  105. *y = ay1 + r1 * (ay2 - ay1);
  106. return 1;
  107. }
  108. /* lines are parallel */
  109. if (D1 || D2) {
  110. return 0;
  111. }
  112. /* segments are colinear. check for overlap */
  113. if (ax1 > ax2) {
  114. t = ax1;
  115. ax1 = ax2;
  116. ax2 = t;
  117. }
  118. if (bx1 > bx2) {
  119. t = bx1;
  120. bx1 = bx2;
  121. bx2 = t;
  122. }
  123. if (ax1 > bx2)
  124. return 0;
  125. if (ax2 < bx1)
  126. return 0;
  127. /* there is overlap */
  128. if (ax1 == bx2) {
  129. *x = ax1;
  130. *y = ay1;
  131. return 1; /* endpoints only */
  132. }
  133. if (ax2 == bx1) {
  134. *x = ax2;
  135. *y = ay2;
  136. return 1; /* endpoints only */
  137. }
  138. return -1; /* overlap, no single intersection point */
  139. }