max_distance.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /****************************************************************
  2. *
  3. * MODULE: v.edit
  4. *
  5. * PURPOSE: Editing vector map.
  6. *
  7. * AUTHOR(S): GRASS Development Team
  8. * Wolf Bergenheim, Jachym Cepicky, Martin Landa
  9. *
  10. * COPYRIGHT: (C) 2006-2008 by the GRASS Development Team
  11. *
  12. * This program is free software under the
  13. * GNU General Public License (>=v2).
  14. * Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. * TODO: 3D support
  18. ****************************************************************/
  19. #include "global.h"
  20. /**
  21. \brief Set distance based on the current resolution
  22. This code comes from v.what/main.c
  23. \param[in] maxdistance max distance
  24. \return result max distance
  25. */
  26. double max_distance(double maxdistance)
  27. {
  28. struct Cell_head window;
  29. double ew_dist1, ew_dist2, ns_dist1, ns_dist2;
  30. double xres, yres, maxd;
  31. if (maxdistance < 0.0) {
  32. G_get_window(&window);
  33. G_begin_distance_calculations();
  34. ew_dist1 =
  35. G_distance(window.east, window.north, window.west, window.north);
  36. /* EW Dist at South Edge */
  37. ew_dist2 =
  38. G_distance(window.east, window.south, window.west, window.south);
  39. /* NS Dist at East edge */
  40. ns_dist1 =
  41. G_distance(window.east, window.north, window.east, window.south);
  42. /* NS Dist at West edge */
  43. ns_dist2 =
  44. G_distance(window.west, window.north, window.west, window.south);
  45. xres = ((ew_dist1 + ew_dist2) / 2) / window.cols;
  46. yres = ((ns_dist1 + ns_dist2) / 2) / window.rows;
  47. if (xres > yres)
  48. maxd = xres;
  49. else
  50. maxd = yres;
  51. /*
  52. G_important_message (_("Threshold distance set to %g map units (based on 2D resolution)"), maxd);
  53. */
  54. }
  55. else {
  56. maxd = maxdistance;
  57. }
  58. G_debug(3, "max_distance(): threshold is %g", maxd);
  59. return maxd;
  60. }
  61. /**
  62. \brief Creates bounding box (polygon)
  63. Based on center point; size (2 * maxdist)
  64. \param[in] east,north coordinates of center
  65. \param[in] maxdist size of bounding box
  66. \param[out] result bounding box
  67. \return
  68. */
  69. void coord2bbox(double east, double north, double maxdist,
  70. struct line_pnts *box)
  71. {
  72. /* TODO: 3D */
  73. Vect_reset_line(box);
  74. Vect_append_point(box, east - maxdist, north - maxdist, 0);
  75. Vect_append_point(box, east + maxdist, north - maxdist, 0);
  76. Vect_append_point(box, east + maxdist, north + maxdist, 0);
  77. Vect_append_point(box, east - maxdist, north + maxdist, 0);
  78. Vect_append_point(box, box->x[0], box->y[0], box->z[0]);
  79. return;
  80. }