dense_line.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #include <math.h>
  2. #include <grass/gis.h>
  3. #include <grass/dbmi.h>
  4. #include <grass/vector.h>
  5. #include <grass/glocale.h>
  6. #include "local.h"
  7. static struct state {
  8. struct Cell_head window;
  9. double xconv, yconv;
  10. double left, right, top, bottom;
  11. int ymin, ymax;
  12. int dotted_fill_gap;
  13. int (*dot)(int, int);
  14. } state;
  15. static struct state *st = &state;
  16. #define X(e) (st->left + st->xconv * ((e) - st->window.west))
  17. #define Y(n) (st->top + st->yconv * (st->window.north - (n)))
  18. #define EAST(x) (st->window.west + ((x)-st->left)/st->xconv)
  19. #define NORTH(y) (st->window.north - ((y)-st->top)/st->yconv)
  20. void dense_line(double x1, double y1, double x2, double y2,
  21. int (*point) (int, int));
  22. static int ifloor(double x)
  23. {
  24. int i;
  25. i = (int)x;
  26. if (i > x)
  27. i--;
  28. return i;
  29. }
  30. static int iceil(double x)
  31. {
  32. int i;
  33. i = (int)x;
  34. if (i < x)
  35. i++;
  36. return i;
  37. }
  38. void setup_plot(double t, double b, double l, double r,
  39. int (*dot) (int, int))
  40. {
  41. G_get_set_window(&st->window);
  42. st->left = l;
  43. st->right = r;
  44. st->top = t;
  45. st->bottom = b;
  46. st->xconv = (st->right - st->left) / (st->window.east - st->window.west);
  47. st->yconv = (st->bottom - st->top) / (st->window.north - st->window.south);
  48. if (st->top < st->bottom) {
  49. st->ymin = iceil(st->top);
  50. st->ymax = ifloor(st->bottom);
  51. }
  52. else {
  53. st->ymin = iceil(st->bottom);
  54. st->ymax = ifloor(st->top);
  55. }
  56. st->dot = dot;
  57. }
  58. /* dense line plotting, alternative to G_plot_line2()
  59. * x1, y1, x2, y2 are col, row numbers */
  60. void plot_line_dense(double east1, double north1, double east2, double north2)
  61. {
  62. double x1, x2, y1, y2;
  63. y1 = Y(north1);
  64. y2 = Y(north2);
  65. if (st->window.proj == PROJECTION_LL) {
  66. if (east1 > east2)
  67. while ((east1 - east2) > 180)
  68. east2 += 360;
  69. else if (east2 > east1)
  70. while ((east2 - east1) > 180)
  71. east1 += 360;
  72. while (east1 > st->window.east) {
  73. east1 -= 360.0;
  74. east2 -= 360.0;
  75. }
  76. while (east1 < st->window.west) {
  77. east1 += 360.0;
  78. east2 += 360.0;
  79. }
  80. x1 = X(east1);
  81. x2 = X(east2);
  82. dense_line(x1, y1, x2, y2, st->dot);
  83. if (east2 > st->window.east || east2 < st->window.west) {
  84. while (east2 > st->window.east) {
  85. east1 -= 360.0;
  86. east2 -= 360.0;
  87. }
  88. while (east2 < st->window.west) {
  89. east1 += 360.0;
  90. east2 += 360.0;
  91. }
  92. x1 = X(east1);
  93. x2 = X(east2);
  94. dense_line(x1, y1, x2, y2, st->dot);
  95. }
  96. }
  97. else {
  98. x1 = X(east1);
  99. x2 = X(east2);
  100. dense_line(x1, y1, x2, y2, st->dot);
  101. }
  102. }
  103. /* dense line plotting, alternative to G_bresenham_line()
  104. * x1, y1, x2, y2 are col, row numbers */
  105. void dense_line(double x1, double y1, double x2, double y2,
  106. int (*point) (int, int))
  107. {
  108. int ix1, ix2, iy1, iy2, idx, idy;
  109. int xinc, yinc;
  110. double dx, dy;
  111. G_debug(2, "dense line");
  112. if (x2 < x1) {
  113. double tmp;
  114. tmp = x1;
  115. x1 = x2;
  116. x2 = tmp;
  117. tmp = y1;
  118. y1 = y2;
  119. y2 = tmp;
  120. }
  121. ix1 = (int)x1;
  122. ix2 = (int)x2;
  123. iy1 = (int)y1;
  124. iy2 = (int)y2;
  125. idx = ix2 - ix1;
  126. idy = iy2 - iy1;
  127. dx = x2 - x1;
  128. dy = y2 - y1;
  129. xinc = yinc = 1;
  130. if (idx < 0) {
  131. xinc = -1;
  132. idx = -idx;
  133. }
  134. if (idy < 0) {
  135. yinc = -1;
  136. idy = -idy;
  137. }
  138. if (dx < 0)
  139. dx = -dx;
  140. if (dy < 0)
  141. dy = -dy;
  142. if (idx == 0) {
  143. while (iy1 != iy2) {
  144. point(ix1, iy1);
  145. iy1 += yinc;
  146. }
  147. }
  148. else if (idy == 0) {
  149. while (ix1 != ix2) {
  150. point(ix1, iy1);
  151. ix1 += xinc;
  152. }
  153. }
  154. else if (dx >= dy) {
  155. double m, a, yi;
  156. int xnext;
  157. m = (y2 - y1) / (x2 - x1);
  158. a = y1 - m * x1;
  159. /* find x for y = iy1 or y = iy1 + 1 */
  160. m = (x2 - x1) / (y2 - y1);
  161. a = x1 - m * y1;
  162. yi = iy1;
  163. if (iy1 < iy2)
  164. yi += 1;
  165. xnext = a + m * yi;
  166. while (ix1 != ix2) {
  167. point(ix1, iy1);
  168. if (ix1 == xnext) {
  169. iy1 += yinc;
  170. point(ix1, iy1);
  171. if (iy1 != iy2) {
  172. yi = iy1;
  173. if (iy1 < iy2)
  174. yi += 1;
  175. xnext = a + m * yi;
  176. }
  177. else
  178. xnext = ix2;
  179. }
  180. ix1 += xinc;
  181. }
  182. if (iy1 != iy2)
  183. point(ix1, iy1);
  184. }
  185. else if (dx < dy) {
  186. double m, a, xi;
  187. int ynext;
  188. if (y2 < y1) {
  189. double tmp;
  190. tmp = x1;
  191. x1 = x2;
  192. x2 = tmp;
  193. tmp = y1;
  194. y1 = y2;
  195. y2 = tmp;
  196. ix1 = (int)x1;
  197. ix2 = (int)x2;
  198. iy1 = (int)y1;
  199. iy2 = (int)y2;
  200. yinc = 1;
  201. xinc = 1;
  202. if (x2 < x1)
  203. xinc = -1;
  204. }
  205. /* find y for x = ix1 or x = ix1 + 1 */
  206. m = (y2 - y1) / (x2 - x1);
  207. a = y1 - m * x1;
  208. xi = ix1;
  209. if (ix1 < ix2)
  210. xi += 1;
  211. ynext = a + m * xi;
  212. while (iy1 != iy2) {
  213. point(ix1, iy1);
  214. if (iy1 == ynext) {
  215. ix1 += xinc;
  216. point(ix1, iy1);
  217. if (ix1 != ix2) {
  218. xi = ix1;
  219. if (ix1 < ix2)
  220. xi += 1;
  221. ynext = a + m * xi;
  222. }
  223. else
  224. ynext = iy2;
  225. }
  226. iy1 += yinc;
  227. }
  228. if (ix1 != ix2)
  229. point(ix1, iy1);
  230. }
  231. point(ix2, iy2);
  232. }