12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- /*
- * \file bres_line.c
- *
- * \brief GIS Library - Bresenham line routines.
- *
- * (C) 2001-2008 by the GRASS Development Team
- *
- * This program is free software under the GNU General Public License
- * (>=v2). Read the file COPYING that comes with GRASS for details.
- *
- * \author GRASS GIS Development Team
- *
- * \date 1999-2008
- */
- #include <grass/gis.h>
- /**
- * \brief Bresenham line algorithm.
- *
- * Draws a line from <b>x1,y1</b> to <b>x2,y2</b> using Bresenham's
- * algorithm. A routine to plot points must be provided, as is defined
- * as: point(x, y) plot a point at x,y.
- *
- * This routine does not require a previous call to
- * <i>G_setup_plot()</i> to function correctly, and is independent of
- * all following routines.
- *
- * \param[in] x0
- * \param[in] y0
- * \param[in] x1
- * \param[in] y1
- * \param[in] point pointer to point plotting function
- * \return
- */
- void G_bresenham_line(int x0, int y0, int x1, int y1, int (*point) (int, int))
- {
- int dx, dy;
- int xinc, yinc;
- int res1;
- int res2;
- xinc = 1;
- yinc = 1;
- if ((dx = x1 - x0) < 0) {
- xinc = -1;
- dx = -dx;
- }
- if ((dy = y1 - y0) < 0) {
- yinc = -1;
- dy = -dy;
- }
- res1 = 0;
- res2 = 0;
- if (dx > dy) {
- while (x0 != x1) {
- point(x0, y0);
- if (res1 > res2) {
- res2 += dx - res1;
- res1 = 0;
- y0 += yinc;
- }
- res1 += dy;
- x0 += xinc;
- }
- }
- else if (dx < dy) {
- while (y0 != y1) {
- point(x0, y0);
- if (res1 > res2) {
- res2 += dy - res1;
- res1 = 0;
- x0 += xinc;
- }
- res1 += dx;
- y0 += yinc;
- }
- }
- else {
- while (x0 != x1) {
- point(x0, y0);
- y0 += yinc;
- x0 += xinc;
- }
- }
- point(x1, y1);
- }
|