GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gis/intersect.c
Go to the documentation of this file.
1 
2 /**************************************************************
3  * find interesection between two lines defined by points on the lines
4  * line segment A is (ax1,ay1) to (ax2,ay2)
5  * line segment B is (bx1,by1) to (bx2,by2)
6  * returns
7  * -1 segment A and B do not intersect (parallel without overlap)
8  * 0 segment A and B do not intersect but extensions do intersect
9  * 1 intersection is a single point
10  * 2 intersection is a line segment (colinear with overlap)
11  * x,y intersection point
12  * ra - ratio that the intersection divides A
13  * rb - ratio that the intersection divides B
14  *
15  * B2
16  * /
17  * /
18  * r=p/(p+q) : A1---p-------*--q------A2
19  * /
20  * /
21  * B1
22  *
23  **************************************************************/
24 
25 /**************************************************************
26 *
27 * A point P which lies on line defined by points A1=(x1,y1) and A2=(x2,y2)
28 * is given by the equation r * (x2,y2) + (1-r) * (x1,y1).
29 * if r is between 0 and 1, p lies between A1 and A2.
30 *
31 * Suppose points on line (A1, A2) has equation
32 * (x,y) = ra * (ax2,ay2) + (1-ra) * (ax1,ay1)
33 * or for x and y separately
34 * x = ra * ax2 - ra * ax1 + ax1
35 * y = ra * ay2 - ra * ay1 + ay1
36 * and the points on line (B1, B2) are represented by
37 * (x,y) = rb * (bx2,by2) + (1-rb) * (bx1,by1)
38 * or for x and y separately
39 * x = rb * bx2 - rb * bx1 + bx1
40 * y = rb * by2 - rb * by1 + by1
41 *
42 * when the lines intersect, the point (x,y) has to
43 * satisfy a system of 2 equations:
44 * ra * ax2 - ra * ax1 + ax1 = rb * bx2 - rb * bx1 + bx1
45 * ra * ay2 - ra * ay1 + ay1 = rb * by2 - rb * by1 + by1
46 *
47 * or
48 *
49 * (ax2 - ax1) * ra - (bx2 - bx1) * rb = bx1 - ax1
50 * (ay2 - ay1) * ra - (by2 - by1) * rb = by1 - ay1
51 *
52 * by Cramer's method, one can solve this by computing 3
53 * determinants of matrices:
54 *
55 * M = (ax2-ax1) (bx1-bx2)
56 * (ay2-ay1) (by1-by2)
57 *
58 * M1 = (bx1-ax1) (bx1-bx2)
59 * (by1-ay1) (by1-by2)
60 *
61 * M2 = (ax2-ax1) (bx1-ax1)
62 * (ay2-ay1) (by1-ay1)
63 *
64 * Which are exactly the determinants D, D2, D1 below:
65 *
66 * D ((ax2-ax1)*(by1-by2) - (ay2-ay1)*(bx1-bx2))
67 *
68 * D1 ((bx1-ax1)*(by1-by2) - (by1-ay1)*(bx1-bx2))
69 *
70 * D2 ((ax2-ax1)*(by1-ay1) - (ay2-ay1)*(bx1-ax1))
71 ***********************************************************************/
72 
73 
74 #define D ((ax2-ax1)*(by1-by2) - (ay2-ay1)*(bx1-bx2))
75 #define D1 ((bx1-ax1)*(by1-by2) - (by1-ay1)*(bx1-bx2))
76 #define D2 ((ax2-ax1)*(by1-ay1) - (ay2-ay1)*(bx1-ax1))
77 
78 #define SWAP(x,y) {int t; t=x; x=y; y=t;}
79 
80 int G_intersect_line_segments(double ax1, double ay1, double ax2, double ay2,
81  double bx1, double by1, double bx2, double by2,
82  double *ra, double *rb, double *x, double *y)
83 {
84  double d;
85 
86  d = D;
87 
88  if (d) { /* lines are not parallel */
89  *ra = D1 / d;
90  *rb = D2 / d;
91 
92  *x = ax1 + (*ra) * (ax2 - ax1);
93  *y = ay1 + (*ra) * (ay2 - ay1);
94  return (*ra >= 0.0 && *ra <= 1.0 && *rb >= 0.0 && *rb <= 1.0);
95  }
96 
97  if (D1 || D2)
98  return -1; /* lines are parallel, not colinear */
99 
100  if (ax1 > ax2) {
101  SWAP(ax1, ax2)
102  }
103  if (bx1 > bx2) {
104  SWAP(bx1, bx2)
105  }
106  if (ax1 > bx2)
107  return -1;
108  if (ax2 < bx1)
109  return -1;
110 
111  /* there is overlap */
112  if (ax1 == bx2) {
113  *x = ax1;
114  *y = ay1;
115  return 1; /* at endpoints only */
116  }
117  if (ax2 == bx1) {
118  *x = ax2;
119  *y = ay2;
120  return 1; /* at endpoints only */
121  }
122 
123  return 2; /* colinear with overlap on an interval, not just a single point */
124 }
int G_intersect_line_segments(double ax1, double ay1, double ax2, double ay2, double bx1, double by1, double bx2, double by2, double *ra, double *rb, double *x, double *y)
Definition: gis/intersect.c:80
#define D2
Definition: gis/intersect.c:76
int y
Definition: plot.c:34
#define D1
Definition: gis/intersect.c:75
#define D
Definition: gis/intersect.c:74
#define SWAP(x, y)
Definition: gis/intersect.c:78