GRASS 8 Programmer's Manual 8.6.0dev(2026)-8843f13794
Loading...
Searching...
No Matches
vector/diglib/line_dist.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 <math.h>
16
17#define ZERO(x) ((x) < tolerance && (x) > -tolerance)
18#define TOLERANCE 1.0e-10
19static double tolerance = TOLERANCE;
20
22{
23 if (t <= 0.0)
24 t = TOLERANCE;
25 tolerance = t;
26
27 return 0;
28}
29
30/*
31 * dig_distance2_point_to_line ()
32 * compute square of distance of point (x,y) to line segment (x1,y1 - x2,y2)
33 * ( works correctly for x1==x2 && y1==y2 )
34 *
35 * returns: square distance
36 * sets (if not NULL): *px, *py - nearest point on segment
37 * *pdist - distance of px,py from segment start
38 * *status = 0 if ok, -1 if t < 0 and 1 if t > 1
39 * (tells if point is w/in segment space, or
40 * past ends)
41 */
42
43double
44dig_distance2_point_to_line(double x, double y, double z, /* point */
45 double x1, double y1, double z1, /* line segment */
46 double x2, double y2, double z2,
47 int with_z, /* use z coordinate, (3D calculation) */
48 double *px, double *py,
49 double *pz, /* point on segment */
50 double *pdist, /* distance of point on segment from
51 the first point of segment */
52 int *status)
53{
54 register double dx, dy, dz;
55 register double dpx, dpy, dpz;
56 register double tpx, tpy, tpz;
57 double t;
58 int st;
59
60 st = 0;
61
62 if (!with_z) {
63 z = 0;
64 z1 = 0;
65 z2 = 0;
66 }
67
68 dx = x2 - x1;
69 dy = y2 - y1;
70 dz = z2 - z1;
71
72 if (ZERO(dx) && ZERO(dy) && ZERO(dz)) { /* line is degenerate */
73 dx = x1 - x;
74 dy = y1 - y;
75 dz = z1 - z;
76 tpx = x1;
77 tpy = y1;
78 tpz = z1;
79 }
80 else {
81 t = (dx * (x - x1) + dy * (y - y1) + dz * (z - z1)) /
82 (dx * dx + dy * dy + dz * dz);
83
84 if (t <= 0.0) { /* go to x1,y1,z1 */
85 if (t < 0.0) {
86 st = -1;
87 }
88 tpx = x1;
89 tpy = y1;
90 tpz = z1;
91 }
92 else if (t >= 1.0) { /* go to x2,y2,z2 */
93 if (t > 1.0) {
94 st = 1;
95 }
96 tpx = x2;
97 tpy = y2;
98 tpz = z2;
99 }
100 else {
101 /* go t from x1,y1,z1 towards x2,y2,z2 */
102 tpx = dx * t + x1;
103 tpy = dy * t + y1;
104 tpz = dz * t + z1;
105 }
106 dx = tpx - x;
107 dy = tpy - y;
108 dz = tpz - z;
109 }
110
111 if (px)
112 *px = tpx;
113 if (py)
114 *py = tpy;
115 if (pz)
116 *pz = tpz;
117 if (status)
118 *status = st;
119
120 if (pdist) {
121 dpx = tpx - x1;
122 dpy = tpy - y1;
123 dpz = tpz - z1;
124 *pdist = sqrt(dpx * dpx + dpy * dpy + dpz * dpz);
125 }
126
127 return (dx * dx + dy * dy + dz * dz);
128}
struct state * st
Definition parser.c:102
double t
Definition r_raster.c:37
int dig_set_distance_to_line_tolerance(double t)
#define TOLERANCE
#define ZERO(x)
double dig_distance2_point_to_line(double x, double y, double z, double x1, double y1, double z1, double x2, double y2, double z2, int with_z, double *px, double *py, double *pz, double *pdist, int *status)
#define x