plot.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <string.h>
  2. #include <grass/display.h>
  3. #include <grass/gis.h>
  4. #include <stdio.h>
  5. #include "local_proto.h"
  6. #define METERS_TO_MILES(x) ((x) * 6.213712e-04)
  7. void plot(double lon1, double lat1, double lon2, double lat2,
  8. int line_color, int text_color)
  9. {
  10. double distance;
  11. double text_x, text_y;
  12. double a, e2;
  13. int nsteps = 1000;
  14. int i;
  15. /* establish the current graphics window */
  16. D_setup(0);
  17. G_get_ellipsoid_parameters(&a, &e2);
  18. G_begin_geodesic_distance(a, e2);
  19. D_use_color(line_color);
  20. G_shortest_way(&lon1, &lon2);
  21. if (lon1 != lon2) {
  22. G_begin_geodesic_equation(lon1, lat1, lon2, lat2);
  23. D_begin();
  24. for (i = 0; i <= nsteps; i++) {
  25. double lon = lon1 + (lon2 - lon1) * i / nsteps;
  26. double lat = G_geodesic_lat_from_lon(lon);
  27. if (i == 0)
  28. D_move_abs(lon, lat);
  29. else
  30. D_cont_abs(lon, lat);
  31. }
  32. D_end();
  33. D_stroke();
  34. text_x = (lon1 + lon2) / 2;
  35. text_y = G_geodesic_lat_from_lon(text_x);
  36. }
  37. else {
  38. D_line_abs(lon1, lat1, lon2, lat2);
  39. text_x = (lon1 + lon2) / 2;
  40. text_y = (lat1 + lat2) / 2;
  41. }
  42. if (text_color != -1) {
  43. double t, b, l, r;
  44. char buf[100];
  45. D_text_size(10, 10);
  46. distance = G_geodesic_distance(lon1, lat1, lon2, lat2);
  47. sprintf(buf, "%.0f miles", METERS_TO_MILES(distance));
  48. D_pos_abs(text_x, text_y);
  49. D_get_text_box(buf, &t, &b, &l, &r);
  50. if (t - D_get_u_north() > 0)
  51. text_y -= t - D_get_u_north();
  52. if (b - D_get_u_south() < 0)
  53. text_y -= b - D_get_u_south();
  54. if (r - D_get_u_east() > 0)
  55. text_x -= r - D_get_u_east();
  56. if (l - D_get_u_west() < 0)
  57. text_x -= l - D_get_u_west();
  58. D_use_color(text_color);
  59. D_pos_abs(text_x, text_y);
  60. D_text(buf);
  61. }
  62. }