grass  7.8.6 About: GRASS (Geographic Resources Analysis Support System) is a raster- and vector-based GIS, image processing system, graphics production system and spatial modeling system.   Fossies Dox: grass-7.8.6.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)
line_dist.c
Go to the documentation of this file.
1#include <grass/gis.h>
2/* compute square of distance of point (x,y) to line segment (x1,y1 - x2,y2) */
3
4#define ZERO(x) x < tolerance && x > -tolerance
5#define TOLERANCE 1.0e-10
6static double tolerance = TOLERANCE;
7
9{
10 if (t <= 0.0)
11 t = TOLERANCE;
12 tolerance = t;
13}
14
15double G_distance2_point_to_line(double x, double y, /* point */
16 double x1, double y1, double x2, double y2)
17{ /* line segment */
18 double dx, dy, t;
19
20 dx = x2 - x1;
21 dy = y2 - y1;
22
23 if (ZERO(dx) && ZERO(dy)) { /* line is degenerate */
24 dx = x1 - x;
25 dy = y1 - y;
26 return dx * dx + dy * dy; /* compute distance x,y to x1,y1 */
27 }
28
29 t = (dx * (x - x1) + dy * (y - y1)) / (dx * dx + dy * dy);
30
31 if (t < 0.0) { /* go to x1,y1 */
32 dx = x - x1;
33 dy = y - y1;
34 }
35 else if (t > 1.0) { /* go to x2,y2 */
36 dx = x - x2;
37 dy = y - y2;
38 }
39 else { /* go t from x1,y1 towards x2,y2 */
40
41 dx = x - (dx * t + x1);
42 dy = y - (dy * t + y1);
43 }
44 return dx * dx + dy * dy;
45}
void G_set_distance_to_line_tolerance(double t)
Definition: line_dist.c:8
#define TOLERANCE
Definition: line_dist.c:5
#define ZERO(x)
Definition: line_dist.c:4
static double tolerance
Definition: line_dist.c:6
double G_distance2_point_to_line(double x, double y, double x1, double y1, double x2, double y2)
Definition: line_dist.c:15
static uint16 x1
Definition: lrand48.c:35
static uint16 x2
Definition: lrand48.c:35
double t
Definition: r_raster.c:39
#define x