GRASS Programmer's Manual
6.4.1(2011)
|
00001 /* 00002 * \file bres_line.c 00003 * 00004 * \brief GIS Library - Bresenham line routines. 00005 * 00006 * (C) 2001-2008 by the GRASS Development Team 00007 * 00008 * This program is free software under the GNU General Public License 00009 * (>=v2). Read the file COPYING that comes with GRASS for details. 00010 * 00011 * \author GRASS GIS Development Team 00012 * 00013 * \date 1999-2008 00014 */ 00015 00016 #include <grass/gis.h> 00017 00018 00038 int G_bresenham_line(int x0, int y0, int x1, int y1, int (*point) (int, int)) 00039 { 00040 int dx, dy; 00041 int xinc, yinc; 00042 00043 register int res1; 00044 int res2; 00045 00046 xinc = 1; 00047 yinc = 1; 00048 if ((dx = x1 - x0) < 0) { 00049 xinc = -1; 00050 dx = -dx; 00051 } 00052 00053 if ((dy = y1 - y0) < 0) { 00054 yinc = -1; 00055 dy = -dy; 00056 } 00057 res1 = 0; 00058 res2 = 0; 00059 00060 if (dx > dy) { 00061 while (x0 != x1) { 00062 point(x0, y0); 00063 if (res1 > res2) { 00064 res2 += dx - res1; 00065 res1 = 0; 00066 y0 += yinc; 00067 } 00068 res1 += dy; 00069 x0 += xinc; 00070 } 00071 } 00072 else if (dx < dy) { 00073 while (y0 != y1) { 00074 point(x0, y0); 00075 if (res1 > res2) { 00076 res2 += dy - res1; 00077 res1 = 0; 00078 x0 += xinc; 00079 } 00080 res1 += dx; 00081 y0 += yinc; 00082 } 00083 } 00084 else { 00085 while (x0 != x1) { 00086 point(x0, y0); 00087 y0 += yinc; 00088 x0 += xinc; 00089 } 00090 } 00091 00092 point(x1, y1); 00093 00094 return 0; 00095 }