plot.c 18 KB

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