plot.c 18 KB

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