plot.c 18 KB

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