GRASS GIS 8 Programmer's Manual
8.2.2dev(2023)-3d2c704037
gis/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
6
static
double
tolerance =
TOLERANCE
;
7
8
void
G_set_distance_to_line_tolerance
(
double
t
)
9
{
10
if
(t <= 0.0)
11
t =
TOLERANCE
;
12
tolerance =
t
;
13
}
14
15
double
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
}
x
#define x
TOLERANCE
#define TOLERANCE
Definition:
gis/line_dist.c:5
gis.h
t
double t
Definition:
r_raster.c:39
G_distance2_point_to_line
double G_distance2_point_to_line(double x, double y, double x1, double y1, double x2, double y2)
Definition:
gis/line_dist.c:15
G_set_distance_to_line_tolerance
void G_set_distance_to_line_tolerance(double t)
Definition:
gis/line_dist.c:8
ZERO
#define ZERO(x)
Definition:
gis/line_dist.c:4
lib
gis
line_dist.c
Generated on Tue Feb 14 2023 05:56:00 for GRASS GIS 8 Programmer's Manual by
1.8.13