distance.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. \file vector/vedit/distance.c
  3. \brief Vedit library - distance calculation
  4. (C) 2007-2008 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Martin Landa <landa.martin gmail.com>
  8. \date 2007-2008
  9. */
  10. #include <grass/vedit.h>
  11. /**
  12. \brief Calculate distances between two lines
  13. \todo LL projection
  14. \param Points1 first line geometry
  15. \param Points2 second line geometry
  16. \param[out] index of minimal distance
  17. \return minimal distance betwen two lines (their nodes)
  18. */
  19. double Vedit_get_min_distance(struct line_pnts *Points1,
  20. struct line_pnts *Points2, int with_z,
  21. int *mindistidx)
  22. {
  23. unsigned int i;
  24. double distances[4];
  25. /*
  26. distances[0] = first-first
  27. distances[1] = first-last
  28. distances[2] = last-first
  29. distances[3] = last-last
  30. */
  31. distances[0] =
  32. Vect_points_distance(Points1->x[0], Points1->y[0], Points1->z[0],
  33. Points2->x[0], Points2->y[0], Points2->z[0],
  34. with_z);
  35. distances[1] =
  36. Vect_points_distance(Points1->x[0], Points1->y[0], Points1->z[0],
  37. Points2->x[Points2->n_points - 1],
  38. Points2->y[Points2->n_points - 1],
  39. Points2->z[Points2->n_points - 1], with_z);
  40. distances[2] = Vect_points_distance(Points1->x[Points1->n_points - 1],
  41. Points1->y[Points1->n_points - 1],
  42. Points1->z[Points1->n_points - 1],
  43. Points2->x[0], Points2->y[0],
  44. Points2->z[0], with_z);
  45. distances[3] = Vect_points_distance(Points1->x[Points1->n_points - 1],
  46. Points1->y[Points1->n_points - 1],
  47. Points1->z[Points1->n_points - 1],
  48. Points2->x[Points2->n_points - 1],
  49. Points2->y[Points2->n_points - 1],
  50. Points2->z[Points2->n_points - 1],
  51. with_z);
  52. /* find the minimal distance between first or last point of both lines */
  53. *mindistidx = 0;
  54. for (i = 0; i < sizeof(distances) / sizeof(double); i++) {
  55. if (distances[i] >= 0.0 && distances[i] < distances[*mindistidx])
  56. *mindistidx = i;
  57. }
  58. G_debug(3, "Vedit_get_min_distance(): dists=%f,%f,%f,%f",
  59. distances[0], distances[1], distances[2], distances[3]);
  60. return distances[*mindistidx];
  61. }