GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-7d4cd0c030
vector/vedit/distance.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/vedit/distance.c
3 
4  \brief Vedit library - distance calculation
5 
6  (C) 2007-2008 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Martin Landa <landa.martin gmail.com>
12  */
13 
14 #include <grass/vedit.h>
15 
16 /*!
17  \brief Calculate distances between two lines
18 
19  \todo LL projection
20 
21  \param Points1 first line geometry
22  \param Points2 second line geometry
23  \param with_z WITH_Z for 3D data
24  \param[out] mindistidx index of minimal distance
25 
26  \return minimal distance between two lines (their nodes)
27  */
28 double Vedit_get_min_distance(struct line_pnts *Points1,
29  struct line_pnts *Points2, int with_z,
30  int *mindistidx)
31 {
32  unsigned int i;
33  double distances[4];
34 
35  /*
36  distances[0] = first-first
37  distances[1] = first-last
38  distances[2] = last-first
39  distances[3] = last-last
40  */
41 
42  distances[0] = Vect_points_distance(Points1->x[0], Points1->y[0],
43  Points1->z[0], Points2->x[0],
44  Points2->y[0], Points2->z[0], with_z);
45 
46  distances[1] = Vect_points_distance(
47  Points1->x[0], Points1->y[0], Points1->z[0],
48  Points2->x[Points2->n_points - 1], Points2->y[Points2->n_points - 1],
49  Points2->z[Points2->n_points - 1], with_z);
50 
51  distances[2] = Vect_points_distance(
52  Points1->x[Points1->n_points - 1], Points1->y[Points1->n_points - 1],
53  Points1->z[Points1->n_points - 1], Points2->x[0], Points2->y[0],
54  Points2->z[0], with_z);
55 
56  distances[3] = Vect_points_distance(
57  Points1->x[Points1->n_points - 1], Points1->y[Points1->n_points - 1],
58  Points1->z[Points1->n_points - 1], Points2->x[Points2->n_points - 1],
59  Points2->y[Points2->n_points - 1], Points2->z[Points2->n_points - 1],
60  with_z);
61 
62  /* find the minimal distance between first or last point of both lines */
63  *mindistidx = 0;
64  for (i = 0; i < sizeof(distances) / sizeof(double); i++) {
65  if (distances[i] >= 0.0 && distances[i] < distances[*mindistidx])
66  *mindistidx = i;
67  }
68 
69  G_debug(3, "Vedit_get_min_distance(): dists=%f,%f,%f,%f", distances[0],
70  distances[1], distances[2], distances[3]);
71 
72  return distances[*mindistidx];
73 }
int G_debug(int, const char *,...) __attribute__((format(printf
double Vect_points_distance(double, double, double, double, double, double, int)
Calculate distance of 2 points.
Definition: line.c:866
Feature geometry info - coordinates.
Definition: dig_structs.h:1651
double * y
Array of Y coordinates.
Definition: dig_structs.h:1659
double * x
Array of X coordinates.
Definition: dig_structs.h:1655
int n_points
Number of points.
Definition: dig_structs.h:1667
double * z
Array of Z coordinates.
Definition: dig_structs.h:1663
double Vedit_get_min_distance(struct line_pnts *Points1, struct line_pnts *Points2, int with_z, int *mindistidx)
Calculate distances between two lines.