graph_clse.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Close down the graphics processing. This gets called only at driver
  3. * termination time.
  4. */
  5. #include <grass/gis.h>
  6. #include "driverlib.h"
  7. #include "htmlmap.h"
  8. /* sreen dimensions defined in Graph_Set.c */
  9. /* point in polygon test by Randolph Franklin */
  10. /* http://www.ecse.rpi.edu/Homepages/wrf/ */
  11. /* adapted for integer coordinates */
  12. static int pnpoly(int npol, int *xp, int *yp, int x, int y)
  13. {
  14. int i, j, c = 0;
  15. for (i = 0, j = npol - 1; i < npol; j = i++) {
  16. if ((((yp[i] <= y) && (y < yp[j])) ||
  17. ((yp[j] <= y) && (y < yp[i]))) &&
  18. (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))
  19. c = !c;
  20. }
  21. return c;
  22. }
  23. void HTML_Graph_close(void)
  24. {
  25. struct MapPoly *poly, *test_poly;
  26. int i;
  27. int inside;
  28. /*
  29. * exmaine the list of polygons, if a polygon wholly exists inside of
  30. * another polygon, then remove it.
  31. *
  32. */
  33. for (poly = html.head; poly != NULL; poly = poly->next_poly) {
  34. for (test_poly = html.head; test_poly != NULL;
  35. test_poly = test_poly->next_poly) {
  36. if (poly == test_poly) {
  37. continue; /* don't check ourselves */
  38. }
  39. inside = 1;
  40. for (i = 0; i < poly->num_pts && inside; i++) {
  41. inside = pnpoly(test_poly->num_pts,
  42. test_poly->x_pts, test_poly->y_pts,
  43. poly->x_pts[i], poly->y_pts[i]);
  44. }
  45. if (inside) {
  46. poly->num_pts = 0; /* mark polygon as having no points */
  47. break;
  48. }
  49. }
  50. }
  51. /*
  52. * write any beginning prologue appropriate for the map type
  53. */
  54. switch (html.type) {
  55. case APACHE:
  56. fprintf(html.output, "#base _base_\n#default _default_\n");
  57. break;
  58. case RAW:
  59. break;
  60. case CLIENT:
  61. fprintf(html.output, "<MAP NAME=\"map\">\n");
  62. break;
  63. }
  64. /*
  65. * write the polygons in a specific format
  66. */
  67. for (poly = html.head; poly != NULL; poly = poly->next_poly) {
  68. if (poly->num_pts >= 3) {
  69. switch (html.type) {
  70. case APACHE:
  71. fprintf(html.output, "poly %s", poly->url);
  72. for (i = 0; i < poly->num_pts; i++) {
  73. fprintf(html.output, " %d,%d", poly->x_pts[i], poly->y_pts[i]);
  74. }
  75. fprintf(html.output, " %d,%d", poly->x_pts[0], poly->y_pts[0]);
  76. fprintf(html.output, "\n");
  77. break;
  78. case RAW:
  79. fprintf(html.output, "%s", poly->url);
  80. for (i = 0; i < poly->num_pts; i++) {
  81. fprintf(html.output, " %d %d", poly->x_pts[i], poly->y_pts[i]);
  82. }
  83. fprintf(html.output, " %d %d", poly->x_pts[0], poly->y_pts[0]);
  84. fprintf(html.output, "\n");
  85. break;
  86. case CLIENT:
  87. fprintf(html.output,
  88. "<AREA SHAPE=\"POLY\"\n HREF=\"%s\"\n ALT=\"%s\"\n COORDS=\"",
  89. poly->url, poly->url);
  90. for (i = 0; i < poly->num_pts; i++) {
  91. if (i > 0)
  92. fprintf(html.output, ", ");
  93. /*
  94. * don't add newlines, which confuses the weak-minded
  95. * i.e., ms internet exploder :-(
  96. * was: if (i % 8 == 0 && i != 0) fprintf(html.output,"\n ");
  97. */
  98. fprintf(html.output, "%d,%d", poly->x_pts[i], poly->y_pts[i]);
  99. }
  100. fprintf(html.output, ", %d,%d", poly->x_pts[0], poly->y_pts[0]);
  101. fprintf(html.output, "\">\n");
  102. break;
  103. }
  104. }
  105. }
  106. /* final stuff, if needed */
  107. switch (html.type) {
  108. case APACHE:
  109. break;
  110. case RAW:
  111. break;
  112. case CLIENT:
  113. fprintf(html.output,
  114. "<AREA SHAPE=\"RECT\" NOHREF COORDS=\"%d,%d %d,%d\">\n",
  115. 0, 0, screen_width, screen_height);
  116. fprintf(html.output, "</MAP>\n");
  117. break;
  118. }
  119. /*
  120. * close file
  121. */
  122. fclose(html.output);
  123. }