plot.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. /*!
  2. * \file lib/gis/plot.c
  3. *
  4. * \brief GIS Library - Plotting functions.
  5. *
  6. * Plot lines and filled polygons. Input space is current
  7. * window. Output space and output functions are user
  8. * defined. Converts input east,north lines and polygons to output x,y
  9. * and calls user supplied line drawing routines to do the plotting.
  10. *
  11. * Handles global wrap-around for lat-lon locations.
  12. *
  13. * Does not perform window clipping.
  14. * Clipping must be done by the line draw routines supplied by the user.
  15. *
  16. * Note:
  17. * Hopefully, cartographic style projection plotting will be added later.
  18. *
  19. * (C) 2001-2008, 2013 by the GRASS Development Team
  20. *
  21. * This program is free software under the GNU General Public License
  22. * (>=v2). Read the file COPYING that comes with GRASS for details.
  23. *
  24. * \author Original author CERL
  25. */
  26. #include <stdlib.h>
  27. #include <math.h>
  28. #include <grass/gis.h>
  29. static void fastline(double, double, double, double);
  30. static void slowline(double, double, double, double);
  31. static void plot_line(double, double, double, double, void (*)());
  32. static double wrap_east(double, double);
  33. static int edge(double, double, double, double);
  34. static int edge_point(double, int);
  35. static int edge_order(const void *, const void *);
  36. static void row_solid_fill(int, double, double);
  37. static void row_dotted_fill(int, double, double);
  38. static int ifloor(double);
  39. static int iceil(double);
  40. struct point {
  41. double x;
  42. int y;
  43. };
  44. #define POINT struct point
  45. static struct state {
  46. struct Cell_head window;
  47. double xconv, yconv;
  48. double left, right, top, bottom;
  49. int ymin, ymax;
  50. int dotted_fill_gap;
  51. POINT *P;
  52. int np;
  53. int npalloc;
  54. void (*row_fill)(int, double, double);
  55. int (*move)(int, int);
  56. int (*cont)(int, int);
  57. } state;
  58. static struct state *st = &state;
  59. #define OK 0
  60. #define TOO_FEW_EDGES 2
  61. #define NO_MEMORY 1
  62. #define OUT_OF_SYNC -1
  63. /*!
  64. * \brief Initialize plotting routines
  65. *
  66. * Initializes the plotting capability. This routine must be called
  67. * once before calling the G_plot_*() routines described below. The
  68. * parameters <i>t, b, l, r</i> are the top, bottom, left, and right
  69. * of the output x,y coordinate space. They are not integers, but
  70. * doubles to allow for subpixel registration of the input and output
  71. * coordinate spaces. The input coordinate space is assumed to be the
  72. * current GRASS region, and the routines supports both planimetric
  73. * and latitude-longitude coordinate systems.
  74. * <b>Move</b> and <b>Cont</b> are subroutines that will draw lines in x,y
  75. * space. They will be called as follows:
  76. * - Move(x, y) move to x,y (no draw)
  77. * - Cont(x, y) draw from previous position to x,y. Cont(~) is responsible for clipping
  78. *
  79. * \param t,b,l,r top, bottom, left, right
  80. * \param move Move function
  81. * \param Cont Cont function
  82. */
  83. void G_setup_plot(double t, double b, double l, double r,
  84. int (*Move) (int, int), int (*Cont) (int, int))
  85. {
  86. G_get_set_window(&st->window);
  87. st->left = l;
  88. st->right = r;
  89. st->top = t;
  90. st->bottom = b;
  91. st->xconv = (st->right - st->left) / (st->window.east - st->window.west);
  92. st->yconv = (st->bottom - st->top) / (st->window.north - st->window.south);
  93. if (st->top < st->bottom) {
  94. st->ymin = iceil(st->top);
  95. st->ymax = ifloor(st->bottom);
  96. }
  97. else {
  98. st->ymin = iceil(st->bottom);
  99. st->ymax = ifloor(st->top);
  100. }
  101. st->move = Move;
  102. st->cont = Cont;
  103. }
  104. /*!
  105. * \brief Set row_fill routine to row_solid_fill or row_dotted_fill
  106. *
  107. * After calling this function, G_plot_polygon() and G_plot_area()
  108. * fill shapes with solid or dotted lines. If gap is greater than
  109. * zero, this value will be used for row_dotted_fill. Otherwise,
  110. * row_solid_fill is used.
  111. *
  112. * \param gap
  113. */
  114. void G_setup_fill(int gap)
  115. {
  116. if (gap > 0) {
  117. st->row_fill = row_dotted_fill;
  118. st->dotted_fill_gap = gap + 1;
  119. }
  120. else
  121. st->row_fill = row_solid_fill;
  122. }
  123. #define X(e) (st->left + st->xconv * ((e) - st->window.west))
  124. #define Y(n) (st->top + st->yconv * (st->window.north - (n)))
  125. #define EAST(x) (st->window.west + ((x)-st->left)/st->xconv)
  126. #define NORTH(y) (st->window.north - ((y)-st->top)/st->yconv)
  127. /*!
  128. * \brief Converts east,north to x,y
  129. *
  130. * The map coordinates <i>east,north</i> are converted
  131. * to pixel coordinates <i>x,y</i>.
  132. *
  133. * \param east easting
  134. * \param north nothing
  135. * \param x x coordinate
  136. * \param y y coordinate
  137. */
  138. void G_plot_where_xy(double east, double north, int *x, int *y)
  139. {
  140. *x = ifloor(X(G_adjust_easting(east, &st->window)) + 0.5);
  141. *y = ifloor(Y(north) + 0.5);
  142. }
  143. /*!
  144. * \brief Converts x,y to east,north
  145. *
  146. * The pixel coordinates <i>x,y</i> are converted to map
  147. * coordinates <i>east,north</i>.
  148. *
  149. * \param x x coordinate
  150. * \param y y coordinate
  151. * \param east easting
  152. * \param north northing
  153. */
  154. void G_plot_where_en(int x, int y, double *east, double *north)
  155. {
  156. *east = G_adjust_easting(EAST(x), &st->window);
  157. *north = NORTH(y);
  158. }
  159. /*!
  160. \brief Plot point
  161. \param east easting
  162. \param north northing
  163. */
  164. void G_plot_point(double east, double north)
  165. {
  166. int x, y;
  167. G_plot_where_xy(east, north, &x, &y);
  168. st->move(x, y);
  169. st->cont(x, y);
  170. }
  171. /*!
  172. * \brief Plot line between latlon coordinates (fastline)
  173. *
  174. * A line from <i>east1,north1</i> to <i>east2,north2</i> is plotted
  175. * in output x,y coordinates (e.g. pixels for graphics.) This routine
  176. * handles global wrap-around for latitude-longitude databases.
  177. *
  178. * \param east1, north1 first point (start line node)
  179. * \param east2, north2 second point (end line node)
  180. */
  181. void G_plot_line(double east1, double north1, double east2, double north2)
  182. {
  183. plot_line(east1, north1, east2, north2, fastline);
  184. }
  185. /*!
  186. * \brief Plot line between latlon coordinates (slowline)
  187. *
  188. * A line from <i>east1,north1</i> to <i>east2,north2</i> is plotted
  189. * in output x,y coordinates (e.g. pixels for graphics.) This routine
  190. * handles global wrap-around for latitude-longitude databases.
  191. *
  192. * \param east1, north1 first point (start line node)
  193. * \param east2, north2 second point (end line node)
  194. */
  195. void G_plot_line2(double east1, double north1, double east2, double north2)
  196. {
  197. plot_line(east1, north1, east2, north2, slowline);
  198. }
  199. /* fastline converts double rows/cols to ints then plots
  200. * this is ok for graphics, but not the best for vector to raster
  201. */
  202. static void fastline(double x1, double y1, double x2, double y2)
  203. {
  204. st->move(ifloor(x1 + 0.5), ifloor(y1 + 0.5));
  205. st->cont(ifloor(x2 + 0.5), ifloor(y2 + 0.5));
  206. }
  207. /* NOTE (shapiro):
  208. * I think the adding of 0.5 in slowline is not correct
  209. * the output window (left, right, top, bottom) should already
  210. * be adjusted for this: left=-0.5; right = window.cols-0.5;
  211. */
  212. static void slowline(double x1, double y1, double x2, double y2)
  213. {
  214. double dx, dy;
  215. double m, b;
  216. int xstart, xstop, ystart, ystop;
  217. dx = x2 - x1;
  218. dy = y2 - y1;
  219. if (fabs(dx) > fabs(dy)) {
  220. m = dy / dx;
  221. b = y1 - m * x1;
  222. if (x1 > x2) {
  223. xstart = iceil(x2 - 0.5);
  224. xstop = ifloor(x1 + 0.5);
  225. }
  226. else {
  227. xstart = iceil(x1 - 0.5);
  228. xstop = ifloor(x2 + 0.5);
  229. }
  230. if (xstart <= xstop) {
  231. ystart = ifloor(m * xstart + b + 0.5);
  232. st->move(xstart, ystart);
  233. while (xstart <= xstop) {
  234. st->cont(xstart++, ystart);
  235. ystart = ifloor(m * xstart + b + 0.5);
  236. }
  237. }
  238. }
  239. else {
  240. if (dx == dy) /* they both might be 0 */
  241. m = 1.;
  242. else
  243. m = dx / dy;
  244. b = x1 - m * y1;
  245. if (y1 > y2) {
  246. ystart = iceil(y2 - 0.5);
  247. ystop = ifloor(y1 + 0.5);
  248. }
  249. else {
  250. ystart = iceil(y1 - 0.5);
  251. ystop = ifloor(y2 + 0.5);
  252. }
  253. if (ystart <= ystop) {
  254. xstart = ifloor(m * ystart + b + 0.5);
  255. st->move(xstart, ystart);
  256. while (ystart <= ystop) {
  257. st->cont(xstart, ystart++);
  258. xstart = ifloor(m * ystart + b + 0.5);
  259. }
  260. }
  261. }
  262. }
  263. static void plot_line(double east1, double north1, double east2, double north2,
  264. void (*line)(double, double, double, double))
  265. {
  266. double x1, x2, y1, y2;
  267. y1 = Y(north1);
  268. y2 = Y(north2);
  269. if (st->window.proj == PROJECTION_LL) {
  270. if (east1 > east2)
  271. while ((east1 - east2) > 180)
  272. east2 += 360;
  273. else if (east2 > east1)
  274. while ((east2 - east1) > 180)
  275. east1 += 360;
  276. while (east1 > st->window.east) {
  277. east1 -= 360.0;
  278. east2 -= 360.0;
  279. }
  280. while (east1 < st->window.west) {
  281. east1 += 360.0;
  282. east2 += 360.0;
  283. }
  284. x1 = X(east1);
  285. x2 = X(east2);
  286. line(x1, y1, x2, y2);
  287. if (east2 > st->window.east || east2 < st->window.west) {
  288. while (east2 > st->window.east) {
  289. east1 -= 360.0;
  290. east2 -= 360.0;
  291. }
  292. while (east2 < st->window.west) {
  293. east1 += 360.0;
  294. east2 += 360.0;
  295. }
  296. x1 = X(east1);
  297. x2 = X(east2);
  298. line(x1, y1, x2, y2);
  299. }
  300. }
  301. else {
  302. x1 = X(east1);
  303. x2 = X(east2);
  304. line(x1, y1, x2, y2);
  305. }
  306. }
  307. static double wrap_east(double e0, double e1)
  308. {
  309. while (e0 - e1 > 180)
  310. e1 += 360.0;
  311. while (e1 - e0 > 180)
  312. e1 -= 360.0;
  313. return e1;
  314. }
  315. /*!
  316. * \brief Plot filled polygon with n vertices
  317. *
  318. * The polygon, described by the <i>n</i> vertices
  319. * <i>east,north</i>, is plotted in the output x,y space as a filled polygon.
  320. *
  321. * \param x coordinates of vertices
  322. * \param y coordinates of vertices
  323. * \param n number of vertices
  324. *
  325. * \return 0 on success
  326. * \return 2 n < 3
  327. * \return -1 weird internal error
  328. * \return 1 no memory
  329. */
  330. int G_plot_polygon(const double *x, const double *y, int n)
  331. {
  332. int i;
  333. int pole;
  334. double x0, x1;
  335. double y0, y1;
  336. double shift, E, W = 0L;
  337. double e0, e1;
  338. int shift1, shift2;
  339. if (!st->row_fill)
  340. st->row_fill = row_solid_fill;
  341. if (n < 3)
  342. return TOO_FEW_EDGES;
  343. /* traverse the perimeter */
  344. st->np = 0;
  345. shift1 = 0;
  346. /* global wrap-around for lat-lon, part1 */
  347. if (st->window.proj == PROJECTION_LL) {
  348. /*
  349. pole = G_pole_in_polygon(x,y,n);
  350. */
  351. pole = 0;
  352. e0 = x[n - 1];
  353. E = W = e0;
  354. x0 = X(e0);
  355. y0 = Y(y[n - 1]);
  356. if (pole && !edge(x0, y0, x0, Y(90.0 * pole)))
  357. return NO_MEMORY;
  358. for (i = 0; i < n; i++) {
  359. e1 = wrap_east(e0, x[i]);
  360. if (e1 > E)
  361. E = e1;
  362. if (e1 < W)
  363. W = e1;
  364. x1 = X(e1);
  365. y1 = Y(y[i]);
  366. if (!edge(x0, y0, x1, y1))
  367. return NO_MEMORY;
  368. x0 = x1;
  369. y0 = y1;
  370. e0 = e1;
  371. }
  372. if (pole && !edge(x0, y0, x0, Y(90.0 * pole)))
  373. return NO_MEMORY;
  374. shift = 0; /* shift into window */
  375. while (E + shift > st->window.east)
  376. shift -= 360.0;
  377. while (E + shift < st->window.west)
  378. shift += 360.0;
  379. shift1 = X(x[n - 1] + shift) - X(x[n - 1]);
  380. }
  381. else {
  382. x0 = X(x[n - 1]);
  383. y0 = Y(y[n - 1]);
  384. for (i = 0; i < n; i++) {
  385. x1 = X(x[i]);
  386. y1 = Y(y[i]);
  387. if (!edge(x0, y0, x1, y1))
  388. return NO_MEMORY;
  389. x0 = x1;
  390. y0 = y1;
  391. }
  392. }
  393. /* check if perimeter has odd number of points */
  394. if (st->np & 1) {
  395. G_warning("Weird internal error: perimeter has odd number of points");
  396. return OUT_OF_SYNC;
  397. }
  398. /* sort the edge points by col(x) and then by row(y) */
  399. qsort(st->P, st->np, sizeof(POINT), edge_order);
  400. /* plot */
  401. for (i = 1; i < st->np; i += 2) {
  402. if (st->P[i].y != st->P[i - 1].y) {
  403. G_warning("Weird internal error: edge leaves row");
  404. return OUT_OF_SYNC;
  405. }
  406. st->row_fill(st->P[i].y, st->P[i - 1].x + shift1, st->P[i].x + shift1);
  407. }
  408. if (st->window.proj == PROJECTION_LL) { /* now do wrap-around, part 2 */
  409. shift = 0;
  410. while (W + shift < st->window.west)
  411. shift += 360.0;
  412. while (W + shift > st->window.east)
  413. shift -= 360.0;
  414. shift2 = X(x[n - 1] + shift) - X(x[n - 1]);
  415. if (shift2 != shift1) {
  416. for (i = 1; i < st->np; i += 2) {
  417. st->row_fill(st->P[i].y, st->P[i - 1].x + shift2, st->P[i].x + shift2);
  418. }
  419. }
  420. }
  421. return OK;
  422. }
  423. /*!
  424. * \brief Plot multiple polygons
  425. *
  426. * Like G_plot_polygon(), except it takes a set of polygons, each with
  427. * npts[<i>i</i>] vertices, where the number of polygons is specified
  428. * with the <i>rings</i> argument. It is especially useful for
  429. * plotting vector areas with interior islands.
  430. *
  431. * \param xs pointer to pointer for X's
  432. * \param ys pointer to pointer for Y's
  433. * \param rpnts array of ints w/ num points per ring
  434. * \param rings number of rings
  435. *
  436. * \return 0 on success
  437. * \return 2 n < 3
  438. * \return -1 weird internal error
  439. * \return 1 no memory
  440. */
  441. int G_plot_area(double *const *xs, double *const *ys, int *rpnts, int rings)
  442. {
  443. int i, j, n;
  444. int pole;
  445. double x0, x1, *x;
  446. double y0, y1, *y;
  447. double shift, E, W = 0L;
  448. double e0, e1;
  449. int *shift1 = NULL, shift2;
  450. if (!st->row_fill)
  451. st->row_fill = row_solid_fill;
  452. /* traverse the perimeter */
  453. st->np = 0;
  454. shift1 = (int *)G_calloc(sizeof(int), rings);
  455. for (j = 0; j < rings; j++) {
  456. n = rpnts[j];
  457. if (n < 3)
  458. return TOO_FEW_EDGES;
  459. x = xs[j];
  460. y = ys[j];
  461. /* global wrap-around for lat-lon, part1 */
  462. if (st->window.proj == PROJECTION_LL) {
  463. /*
  464. pole = G_pole_in_polygon(x,y,n);
  465. */
  466. pole = 0;
  467. e0 = x[n - 1];
  468. E = W = e0;
  469. x0 = X(e0);
  470. y0 = Y(y[n - 1]);
  471. if (pole && !edge(x0, y0, x0, Y(90.0 * pole)))
  472. return NO_MEMORY;
  473. for (i = 0; i < n; i++) {
  474. e1 = wrap_east(e0, x[i]);
  475. if (e1 > E)
  476. E = e1;
  477. if (e1 < W)
  478. W = e1;
  479. x1 = X(e1);
  480. y1 = Y(y[i]);
  481. if (!edge(x0, y0, x1, y1))
  482. return NO_MEMORY;
  483. x0 = x1;
  484. y0 = y1;
  485. e0 = e1;
  486. }
  487. if (pole && !edge(x0, y0, x0, Y(90.0 * pole)))
  488. return NO_MEMORY;
  489. shift = 0; /* shift into window */
  490. while (E + shift > st->window.east)
  491. shift -= 360.0;
  492. while (E + shift < st->window.west)
  493. shift += 360.0;
  494. shift1[j] = X(x[n - 1] + shift) - X(x[n - 1]);
  495. }
  496. else {
  497. x0 = X(x[n - 1]);
  498. y0 = Y(y[n - 1]);
  499. for (i = 0; i < n; i++) {
  500. x1 = X(x[i]);
  501. y1 = Y(y[i]);
  502. if (!edge(x0, y0, x1, y1))
  503. return NO_MEMORY;
  504. x0 = x1;
  505. y0 = y1;
  506. }
  507. }
  508. } /* for() */
  509. /* check if perimeter has odd number of points */
  510. if (st->np & 1) {
  511. G_warning("Weird internal error: perimeter has odd number of points");
  512. return OUT_OF_SYNC;
  513. }
  514. /* sort the edge points by col(x) and then by row(y) */
  515. qsort(st->P, st->np, sizeof(POINT), &edge_order);
  516. /* plot */
  517. for (j = 0; j < rings; j++) {
  518. for (i = 1; i < st->np; i += 2) {
  519. if (st->P[i].y != st->P[i - 1].y) {
  520. G_warning("Weird internal error: edge leaves row");
  521. return OUT_OF_SYNC;
  522. }
  523. st->row_fill(st->P[i].y, st->P[i - 1].x + shift1[j], st->P[i].x + shift1[j]);
  524. }
  525. if (st->window.proj == PROJECTION_LL) { /* now do wrap-around, part 2 */
  526. n = rpnts[j];
  527. x = xs[j];
  528. y = ys[j];
  529. shift = 0;
  530. while (W + shift < st->window.west)
  531. shift += 360.0;
  532. while (W + shift > st->window.east)
  533. shift -= 360.0;
  534. shift2 = X(x[n - 1] + shift) - X(x[n - 1]);
  535. if (shift2 != shift1[j]) {
  536. for (i = 1; i < st->np; i += 2) {
  537. st->row_fill(st->P[i].y, st->P[i - 1].x + shift2, st->P[i].x + shift2);
  538. }
  539. }
  540. }
  541. }
  542. G_free(shift1);
  543. return OK;
  544. }
  545. static int edge(double x0, double y0, double x1, double y1)
  546. {
  547. double m, d;
  548. double x;
  549. int ystart, ystop;
  550. int exp;
  551. /* tolerance to avoid FPE */
  552. d = GRASS_EPSILON;
  553. if (y0 != y1) {
  554. if (fabs(y0) > fabs(y1))
  555. d = fabs(y0);
  556. else
  557. d = fabs(y1);
  558. d = frexp(d, &exp);
  559. exp -= 53;
  560. d = ldexp(d, exp);
  561. }
  562. if (fabs(y0 - y1) < d)
  563. return 1;
  564. if (y0 < y1) {
  565. ystart = iceil(y0);
  566. ystop = ifloor(y1);
  567. if (ystop == y1)
  568. ystop--; /* if line stops at row center, don't include point */
  569. }
  570. else {
  571. ystart = iceil(y1);
  572. ystop = ifloor(y0);
  573. if (ystop == y0)
  574. ystop--; /* if line stops at row center, don't include point */
  575. }
  576. if (ystart > ystop)
  577. return 1; /* does not cross center line of row */
  578. m = (x0 - x1) / (y0 - y1);
  579. x = m * (ystart - y0) + x0;
  580. while (ystart <= ystop) {
  581. if (!edge_point(x, ystart++))
  582. return 0;
  583. x += m;
  584. }
  585. return 1;
  586. }
  587. static int edge_point(double x, int y)
  588. {
  589. if (y < st->ymin || y > st->ymax)
  590. return 1;
  591. if (st->np >= st->npalloc) {
  592. if (st->npalloc > 0) {
  593. st->npalloc *= 2;
  594. st->P = (POINT *) G_realloc(st->P, st->npalloc * sizeof(POINT));
  595. }
  596. else {
  597. st->npalloc = 32;
  598. st->P = (POINT *) G_malloc(st->npalloc * sizeof(POINT));
  599. }
  600. if (st->P == NULL) {
  601. st->npalloc = 0;
  602. return 0;
  603. }
  604. }
  605. st->P[st->np].x = x;
  606. st->P[st->np++].y = y;
  607. return 1;
  608. }
  609. static int edge_order(const void *aa, const void *bb)
  610. {
  611. const struct point *a = aa, *b = bb;
  612. if (a->y < b->y)
  613. return (-1);
  614. if (a->y > b->y)
  615. return (1);
  616. if (a->x < b->x)
  617. return (-1);
  618. if (a->x > b->x)
  619. return (1);
  620. return (0);
  621. }
  622. static void row_solid_fill(int y, double x1, double x2)
  623. {
  624. int i1, i2;
  625. i1 = iceil(x1);
  626. i2 = ifloor(x2);
  627. if (i1 <= i2) {
  628. st->move(i1, y);
  629. st->cont(i2, y);
  630. }
  631. }
  632. static void row_dotted_fill(int y, double x1, double x2)
  633. {
  634. int i1, i2, i;
  635. if (y != iceil(y / st->dotted_fill_gap) * st->dotted_fill_gap)
  636. return;
  637. i1 = iceil(x1 / st->dotted_fill_gap) * st->dotted_fill_gap;
  638. i2 = ifloor(x2);
  639. if (i1 <= i2) {
  640. for (i = i1; i <= i2; i += st->dotted_fill_gap) {
  641. st->move(i, y);
  642. st->cont(i, y);
  643. }
  644. }
  645. }
  646. static int ifloor(double x)
  647. {
  648. int i;
  649. i = (int)x;
  650. if (i > x)
  651. i--;
  652. return i;
  653. }
  654. static int iceil(double x)
  655. {
  656. int i;
  657. i = (int)x;
  658. if (i < x)
  659. i++;
  660. return i;
  661. }
  662. /*!
  663. * \brief Plot f(east1) to f(east2)
  664. *
  665. * The function <i>f(east)</i> is plotted from <i>east1</i> to
  666. * <i>east2</i>. The function <i>f(east)</i> must return the map
  667. * northing coordinate associated with east.
  668. *
  669. * \param f plotting function
  670. * \param east1 easting (first point)
  671. * \param east2 easting (second point)
  672. */
  673. void G_plot_fx(double (*f) (double), double east1, double east2)
  674. {
  675. double east, north, north1;
  676. double incr;
  677. incr = fabs(1.0 / st->xconv);
  678. east = east1;
  679. north = f(east1);
  680. if (east1 > east2) {
  681. while ((east1 -= incr) > east2) {
  682. north1 = f(east1);
  683. G_plot_line(east, north, east1, north1);
  684. north = north1;
  685. east = east1;
  686. }
  687. }
  688. else {
  689. while ((east1 += incr) < east2) {
  690. north1 = f(east1);
  691. G_plot_line(east, north, east1, north1);
  692. north = north1;
  693. east = east1;
  694. }
  695. }
  696. G_plot_line(east, north, east2, f(east2));
  697. }