GRASS 8 Programmer's Manual 8.6.0dev(2026)-e8fd76f1e2
Loading...
Searching...
No Matches
linecros.c
Go to the documentation of this file.
1/*****************************************************************************
2 *
3 * MODULE: Vector library
4 *
5 * AUTHOR(S): Original author CERL, probably Dave Gerdes.
6 * Update to GRASS 5.7 Radim Blazek.
7 *
8 * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
9 *
10 * SPDX-FileCopyrightText: 2001 GRASS Development Team
11 * SPDX-License-Identifier: GPL-2.0-or-later
12 *
13 *****************************************************************************/
14
15#include <stdio.h>
16
17/***************************************************************
18 * test_for_intersection (ax1,ay1,ax2,ay2,bx1,by1,bx2,by2)
19 * double ax1,ax2,ay1,ay2;
20 * double bx1,bx2,by1,by2;
21 *
22 * returns
23 * 0 no intersection at all
24 * 1 the line segments intersect at only one point
25 * -1 the line segments intersect at many points, i.e., overlapping
26 * segments from the same line
27 *
28 * find_intersection (ax1,ay1,ax2,ay2,bx1,by1,bx2,by2,x,y)
29 * double ax1,ax2,ay1,ay2;
30 * double bx1,bx2,by1,by2;
31 * double *x,*y;
32 *
33 * returns
34 * 0 no intersection
35 * 1 x,y set to (unique) intersection
36 * -1 lines overlap, no unique intersection
37 *
38 * Based on the following:
39 *
40 * (ax2-ax1)r1 - (bx2-bx1)r2 = ax2 - ax1
41 * (ay2-ay1)r1 - (by2-by1)r2 = ay2 - ay1
42 *
43 * Solving for r1 and r2, if r1 and r2 are between 0 and 1,
44 * then line segments (ax1,ay1)(ax2,ay2) and (bx1,by1)(bx2,by2)
45 * intersect
46 ****************************************************************/
47
48#define D ((ax2 - ax1) * (by1 - by2) - (ay2 - ay1) * (bx1 - bx2))
49
50#define D1 ((bx1 - ax1) * (by1 - by2) - (by1 - ay1) * (bx1 - bx2))
51
52#define D2 ((ax2 - ax1) * (by1 - ay1) - (ay2 - ay1) * (bx1 - ax1))
53
54int dig_test_for_intersection(double ax1, double ay1, double ax2, double ay2,
55 double bx1, double by1, double bx2, double by2)
56{
57 register double d, d1, d2;
58 double t;
59 int switched;
60
61 if (ax1 > ax2 || (ax1 == ax2 && ay1 > ay2)) {
62 t = ax1;
63 ax1 = ax2;
64 ax2 = t;
65
66 t = ay1;
67 ay1 = ay2;
68 ay2 = t;
69 }
70
71 if (bx1 > bx2 || (bx1 == bx2 && by1 > by2)) {
72 t = bx1;
73 bx1 = bx2;
74 bx2 = t;
75
76 t = by1;
77 by1 = by2;
78 by2 = t;
79 }
80
81 switched = 0;
82 if (bx1 < ax1)
83 switched = 1;
84 else if (bx1 == ax1) {
85 if (bx2 < ax2)
86 switched = 1;
87 else if (bx2 == ax2) {
88 if (by1 < ay1)
89 switched = 1;
90 else if (by1 == ay1) {
91 if (by2 < ay2)
92 switched = 1;
93 }
94 }
95 }
96
97 if (switched) {
98 t = ax1;
99 ax1 = bx1;
100 bx1 = t;
101 t = ax2;
102 ax2 = bx2;
103 bx2 = t;
104
105 t = ay1;
106 ay1 = by1;
107 by1 = t;
108 t = ay2;
109 ay2 = by2;
110 by2 = t;
111 }
112
113 d = D;
114 d1 = D1;
115 d2 = D2;
116
117 if (d > 0)
118 return (d1 >= 0 && d2 >= 0 && d >= d1 && d >= d2);
119 if (d < 0)
120 return (d1 <= 0 && d2 <= 0 && d <= d1 && d <= d2);
121
122 /* lines are parallel */
123 if (d1 || d2)
124 return 0;
125
126 /* segments are colinear. check for overlap */
127
128 /* Collinear vertical */
129 if (ax1 == ax2) {
130 if (ay1 > ay2) {
131 t = ay1;
132 ay1 = ay2;
133 ay2 = t;
134 }
135 if (by1 > by2) {
136 t = by1;
137 by1 = by2;
138 by2 = t;
139 }
140 if (ay1 > by2)
141 return 0;
142 if (ay2 < by1)
143 return 0;
144
145 /* there is overlap */
146
147 if (ay1 == by2 || ay2 == by1)
148 return 1; /* endpoints only */
149
150 return -1; /* true overlap */
151 }
152 else {
153 if (ax1 > ax2) {
154 t = ax1;
155 ax1 = ax2;
156 ax2 = t;
157 }
158 if (bx1 > bx2) {
159 t = bx1;
160 bx1 = bx2;
161 bx2 = t;
162 }
163 if (ax1 > bx2)
164 return 0;
165 if (ax2 < bx1)
166 return 0;
167
168 /* there is overlap */
169
170 if (ax1 == bx2 || ax2 == bx1)
171 return 1; /* endpoints only */
172
173 return -1; /* true overlap */
174 }
175 return 0; /* should not be reached */
176}
177
178int dig_find_intersection(double ax1, double ay1, double ax2, double ay2,
179 double bx1, double by1, double bx2, double by2,
180 double *x, double *y)
181{
182 register double d, r1, r2;
183 double t;
184 int switched;
185
186 if (ax1 > ax2 || (ax1 == ax2 && ay1 > ay2)) {
187 t = ax1;
188 ax1 = ax2;
189 ax2 = t;
190
191 t = ay1;
192 ay1 = ay2;
193 ay2 = t;
194 }
195
196 if (bx1 > bx2 || (bx1 == bx2 && by1 > by2)) {
197 t = bx1;
198 bx1 = bx2;
199 bx2 = t;
200
201 t = by1;
202 by1 = by2;
203 by2 = t;
204 }
205
206 switched = 0;
207 if (bx1 < ax1)
208 switched = 1;
209 else if (bx1 == ax1) {
210 if (bx2 < ax2)
211 switched = 1;
212 else if (bx2 == ax2) {
213 if (by1 < ay1)
214 switched = 1;
215 else if (by1 == ay1) {
216 if (by2 < ay2)
217 switched = 1;
218 }
219 }
220 }
221
222 if (switched) {
223 t = ax1;
224 ax1 = bx1;
225 bx1 = t;
226 t = ax2;
227 ax2 = bx2;
228 bx2 = t;
229
230 t = ay1;
231 ay1 = by1;
232 by1 = t;
233 t = ay2;
234 ay2 = by2;
235 by2 = t;
236 }
237
238 d = D;
239
240 if (d) {
241
242 r1 = D1 / d;
243 r2 = D2 / d;
244 if (r1 < 0 || r1 > 1 || r2 < 0 || r2 > 1) {
245 return 0;
246 }
247 *x = ax1 + r1 * (ax2 - ax1);
248 *y = ay1 + r1 * (ay2 - ay1);
249 return 1;
250 }
251
252 /* lines are parallel */
253 if (D1 || D2) {
254 return 0;
255 }
256
257 /* segments are colinear. check for overlap */
258
259 /* Collinear vertical */
260 if (ax1 == ax2) {
261 if (ay1 > by2)
262 return 0;
263 if (ay2 < by1)
264 return 0;
265
266 /* there is overlap */
267
268 if (ay1 == by2) {
269 *x = ax1;
270 *y = ay1;
271 return 1; /* endpoints only */
272 }
273 if (ay2 == by1) {
274 *x = ax2;
275 *y = ay2;
276 return 1; /* endpoints only */
277 }
278
279 /* overlap, no single intersection point */
280 if (ay1 > by1 && ay1 < by2) {
281 *x = ax1;
282 *y = ay1;
283 }
284 else {
285 *x = ax2;
286 *y = ay2;
287 }
288 return -1;
289 }
290 else {
291 if (ax1 > bx2)
292 return 0;
293 if (ax2 < bx1)
294 return 0;
295
296 /* there is overlap */
297
298 if (ax1 == bx2) {
299 *x = ax1;
300 *y = ay1;
301 return 1; /* endpoints only */
302 }
303 if (ax2 == bx1) {
304 *x = ax2;
305 *y = ay2;
306 return 1; /* endpoints only */
307 }
308
309 /* overlap, no single intersection point */
310 if (ax1 > bx1 && ax1 < bx2) {
311 *x = ax1;
312 *y = ay1;
313 }
314 else {
315 *x = ax2;
316 *y = ay2;
317 }
318 return -1;
319 }
320
321 return 0; /* should not be reached */
322}
int dig_find_intersection(double ax1, double ay1, double ax2, double ay2, double bx1, double by1, double bx2, double by2, double *x, double *y)
Definition linecros.c:178
#define D1
Definition linecros.c:50
#define D2
Definition linecros.c:52
#define D
Definition linecros.c:48
int dig_test_for_intersection(double ax1, double ay1, double ax2, double ay2, double bx1, double by1, double bx2, double by2)
Definition linecros.c:54
double t
Definition r_raster.c:37
#define x