plot.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /*
  2. * \file gis/plot.c
  3. *
  4. * \brief GIS Library - Plotting functions.
  5. *
  6. * Plot lines and filled polygons. Input space is database window.
  7. * Output space and output functions are user defined.
  8. * 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 databases.
  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 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 nearest(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 nearest(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 verticies
  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 = nearest(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 % 2)
  395. return OUT_OF_SYNC;
  396. /* sort the edge points by col(x) and then by row(y) */
  397. qsort(st->P, st->np, sizeof(POINT), &edge_order);
  398. /* plot */
  399. for (i = 1; i < st->np; i += 2) {
  400. if (st->P[i].y != st->P[i - 1].y)
  401. return OUT_OF_SYNC;
  402. st->row_fill(st->P[i].y, st->P[i - 1].x + shift1, st->P[i].x + shift1);
  403. }
  404. if (st->window.proj == PROJECTION_LL) { /* now do wrap-around, part 2 */
  405. shift = 0;
  406. while (W + shift < st->window.west)
  407. shift += 360.0;
  408. while (W + shift > st->window.east)
  409. shift -= 360.0;
  410. shift2 = X(x[n - 1] + shift) - X(x[n - 1]);
  411. if (shift2 != shift1) {
  412. for (i = 1; i < st->np; i += 2) {
  413. st->row_fill(st->P[i].y, st->P[i - 1].x + shift2, st->P[i].x + shift2);
  414. }
  415. }
  416. }
  417. return OK;
  418. }
  419. /*!
  420. * \brief Plot multiple polygons
  421. *
  422. * Like G_plot_polygon(), except it takes a set of polygons, each with
  423. * npts[<i>i</i>] vertices, where the number of polygons is specified
  424. * with the <i>rings</i> argument. It is especially useful for
  425. * plotting vector areas with interior islands.
  426. *
  427. * \param xs pointer to pointer for X's
  428. * \param ys pointer to pointer for Y's
  429. * \param rpnts array of ints w/ num points per ring
  430. * \param rings number of rings
  431. *
  432. * \return 0 on success
  433. * \return 2 n < 3
  434. * \return -1 weird internal error
  435. * \return 1 no memory
  436. */
  437. int G_plot_area(double *const *xs, double *const *ys, int *rpnts, int rings)
  438. {
  439. int i, j, n;
  440. int pole;
  441. double x0, x1, *x;
  442. double y0, y1, *y;
  443. double shift, E, W = 0L;
  444. double e0, e1;
  445. int *shift1 = NULL, shift2;
  446. if (!st->row_fill)
  447. st->row_fill = row_solid_fill;
  448. /* traverse the perimeter */
  449. st->np = 0;
  450. shift1 = (int *)G_calloc(sizeof(int), rings);
  451. for (j = 0; j < rings; j++) {
  452. n = rpnts[j];
  453. if (n < 3)
  454. return TOO_FEW_EDGES;
  455. x = xs[j];
  456. y = ys[j];
  457. /* global wrap-around for lat-lon, part1 */
  458. if (st->window.proj == PROJECTION_LL) {
  459. /*
  460. pole = G_pole_in_polygon(x,y,n);
  461. */
  462. pole = 0;
  463. e0 = x[n - 1];
  464. E = W = e0;
  465. x0 = X(e0);
  466. y0 = Y(y[n - 1]);
  467. if (pole && !edge(x0, y0, x0, Y(90.0 * pole)))
  468. return NO_MEMORY;
  469. for (i = 0; i < n; i++) {
  470. e1 = nearest(e0, x[i]);
  471. if (e1 > E)
  472. E = e1;
  473. if (e1 < W)
  474. W = e1;
  475. x1 = X(e1);
  476. y1 = Y(y[i]);
  477. if (!edge(x0, y0, x1, y1))
  478. return NO_MEMORY;
  479. x0 = x1;
  480. y0 = y1;
  481. e0 = e1;
  482. }
  483. if (pole && !edge(x0, y0, x0, Y(90.0 * pole)))
  484. return NO_MEMORY;
  485. shift = 0; /* shift into window */
  486. while (E + shift > st->window.east)
  487. shift -= 360.0;
  488. while (E + shift < st->window.west)
  489. shift += 360.0;
  490. shift1[j] = X(x[n - 1] + shift) - X(x[n - 1]);
  491. }
  492. else {
  493. x0 = X(x[n - 1]);
  494. y0 = Y(y[n - 1]);
  495. for (i = 0; i < n; i++) {
  496. x1 = X(x[i]);
  497. y1 = Y(y[i]);
  498. if (!edge(x0, y0, x1, y1))
  499. return NO_MEMORY;
  500. x0 = x1;
  501. y0 = y1;
  502. }
  503. }
  504. } /* for() */
  505. /* check if perimeter has odd number of points */
  506. if (st->np % 2)
  507. return OUT_OF_SYNC;
  508. /* sort the edge points by col(x) and then by row(y) */
  509. qsort(st->P, st->np, sizeof(POINT), &edge_order);
  510. /* plot */
  511. for (j = 0; j < rings; j++) {
  512. for (i = 1; i < st->np; i += 2) {
  513. if (st->P[i].y != st->P[i - 1].y)
  514. return OUT_OF_SYNC;
  515. st->row_fill(st->P[i].y, st->P[i - 1].x + shift1[j], st->P[i].x + shift1[j]);
  516. }
  517. if (st->window.proj == PROJECTION_LL) { /* now do wrap-around, part 2 */
  518. n = rpnts[j];
  519. x = xs[j];
  520. y = ys[j];
  521. shift = 0;
  522. while (W + shift < st->window.west)
  523. shift += 360.0;
  524. while (W + shift > st->window.east)
  525. shift -= 360.0;
  526. shift2 = X(x[n - 1] + shift) - X(x[n - 1]);
  527. if (shift2 != shift1[j]) {
  528. for (i = 1; i < st->np; i += 2) {
  529. st->row_fill(st->P[i].y, st->P[i - 1].x + shift2, st->P[i].x + shift2);
  530. }
  531. }
  532. }
  533. }
  534. G_free(shift1);
  535. return OK;
  536. }
  537. static int edge(double x0, double y0, double x1, double y1)
  538. {
  539. double m;
  540. double dy, x;
  541. int ystart, ystop;
  542. /* tolerance to avoid FPE */
  543. dy = y0 - y1;
  544. if (fabs(dy) < 1e-10)
  545. return 1;
  546. m = (x0 - x1) / dy;
  547. if (y0 < y1) {
  548. ystart = iceil(y0);
  549. ystop = ifloor(y1);
  550. if (ystop == y1)
  551. ystop--; /* if line stops at row center, don't include point */
  552. }
  553. else {
  554. ystart = iceil(y1);
  555. ystop = ifloor(y0);
  556. if (ystop == y0)
  557. ystop--; /* if line stops at row center, don't include point */
  558. }
  559. if (ystart > ystop)
  560. return 1; /* does not cross center line of row */
  561. x = m * (ystart - y0) + x0;
  562. while (ystart <= ystop) {
  563. if (!edge_point(x, ystart++))
  564. return 0;
  565. x += m;
  566. }
  567. return 1;
  568. }
  569. static int edge_point(double x, int y)
  570. {
  571. if (y < st->ymin || y > st->ymax)
  572. return 1;
  573. if (st->np >= st->npalloc) {
  574. if (st->npalloc > 0) {
  575. st->npalloc *= 2;
  576. st->P = (POINT *) G_realloc(st->P, st->npalloc * sizeof(POINT));
  577. }
  578. else {
  579. st->npalloc = 32;
  580. st->P = (POINT *) G_malloc(st->npalloc * sizeof(POINT));
  581. }
  582. if (st->P == NULL) {
  583. st->npalloc = 0;
  584. return 0;
  585. }
  586. }
  587. st->P[st->np].x = x;
  588. st->P[st->np++].y = y;
  589. return 1;
  590. }
  591. static int edge_order(const void *aa, const void *bb)
  592. {
  593. const struct point *a = aa, *b = bb;
  594. if (a->y < b->y)
  595. return (-1);
  596. if (a->y > b->y)
  597. return (1);
  598. if (a->x < b->x)
  599. return (-1);
  600. if (a->x > b->x)
  601. return (1);
  602. return (0);
  603. }
  604. static void row_solid_fill(int y, double x1, double x2)
  605. {
  606. int i1, i2;
  607. i1 = iceil(x1);
  608. i2 = ifloor(x2);
  609. if (i1 <= i2) {
  610. st->move(i1, y);
  611. st->cont(i2, y);
  612. }
  613. }
  614. static void row_dotted_fill(int y, double x1, double x2)
  615. {
  616. int i1, i2, i;
  617. if (y != iceil(y / st->dotted_fill_gap) * st->dotted_fill_gap)
  618. return;
  619. i1 = iceil(x1 / st->dotted_fill_gap) * st->dotted_fill_gap;
  620. i2 = ifloor(x2);
  621. if (i1 <= i2) {
  622. for (i = i1; i <= i2; i += st->dotted_fill_gap) {
  623. st->move(i, y);
  624. st->cont(i, y);
  625. }
  626. }
  627. }
  628. static int ifloor(double x)
  629. {
  630. int i;
  631. i = (int)x;
  632. if (i > x)
  633. i--;
  634. return i;
  635. }
  636. static int iceil(double x)
  637. {
  638. int i;
  639. i = (int)x;
  640. if (i < x)
  641. i++;
  642. return i;
  643. }
  644. /*!
  645. * \brief Plot f(east1) to f(east2)
  646. *
  647. * The function <i>f(east)</i> is plotted from <i>east1</i> to
  648. * <i>east2</i>. The function <i>f(east)</i> must return the map
  649. * northing coordinate associated with east.
  650. *
  651. * \param f plotting function
  652. * \param east1 easting (first point)
  653. * \param east2 easting (second point)
  654. */
  655. void G_plot_fx(double (*f) (double), double east1, double east2)
  656. {
  657. double east, north, north1;
  658. double incr;
  659. incr = fabs(1.0 / st->xconv);
  660. east = east1;
  661. north = f(east1);
  662. if (east1 > east2) {
  663. while ((east1 -= incr) > east2) {
  664. north1 = f(east1);
  665. G_plot_line(east, north, east1, north1);
  666. north = north1;
  667. east = east1;
  668. }
  669. }
  670. else {
  671. while ((east1 += incr) < east2) {
  672. north1 = f(east1);
  673. G_plot_line(east, north, east1, north1);
  674. north = north1;
  675. east = east1;
  676. }
  677. }
  678. G_plot_line(east, north, east2, f(east2));
  679. }