GRASS 8 Programmer's Manual 8.6.0dev(2026)-8843f13794
Loading...
Searching...
No Matches
bres_line.c
Go to the documentation of this file.
1/*
2 * \file lib/gis/bres_line.c
3 *
4 * \brief GIS Library - Bresenham line routines.
5 *
6 * SPDX-FileCopyrightText: 2001-2014 GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * \author Original author CERL
10 */
11
12#include <grass/gis.h>
13
14/*!
15 * \brief Bresenham line algorithm.
16 *
17 * Draws a line from <i>x1,y1</i> to <i>x2,y2</i> using Bresenham's
18 * algorithm. A routine to plot points must be provided, as is defined
19 * as: point(x, y) plot a point at x,y.
20 *
21 * This routine does not require a previous call to G_setup_plot() to
22 * function correctly, and is independent of all following routines.
23 *
24 * \param x0,y0 first point
25 * \param x1,y1 end point
26 * \param point pointer to point plotting function
27 */
28void G_bresenham_line(int x0, int y0, int x1, int y1, int (*point)(int, int))
29{
30 int dx, dy;
31 int xinc, yinc;
32
33 int res1;
34 int res2;
35
36 xinc = 1;
37 yinc = 1;
38 if ((dx = x1 - x0) < 0) {
39 xinc = -1;
40 dx = -dx;
41 }
42
43 if ((dy = y1 - y0) < 0) {
44 yinc = -1;
45 dy = -dy;
46 }
47 res1 = 0;
48 res2 = 0;
49
50 if (dx > dy) {
51 while (x0 != x1) {
52 point(x0, y0);
53 if (res1 > res2) {
54 res2 += dx - res1;
55 res1 = 0;
56 y0 += yinc;
57 }
58 res1 += dy;
59 x0 += xinc;
60 }
61 }
62 else if (dx < dy) {
63 while (y0 != y1) {
64 point(x0, y0);
65 if (res1 > res2) {
66 res2 += dy - res1;
67 res1 = 0;
68 x0 += xinc;
69 }
70 res1 += dx;
71 y0 += yinc;
72 }
73 }
74 else {
75 while (x0 != x1) {
76 point(x0, y0);
77 y0 += yinc;
78 x0 += xinc;
79 }
80 }
81
82 point(x1, y1);
83}
void G_bresenham_line(int x0, int y0, int x1, int y1, int(*point)(int, int))
Bresenham line algorithm.
Definition bres_line.c:28