-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbline.c
73 lines (64 loc) · 1.7 KB
/
bline.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
Copyright (C) 2010 Stephen M. Cameron
Author: Stephen M. Cameron
This file is part of Spacenerds In Space.
Spacenerds in Space is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Spacenerds in Space is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Spacenerds in Space; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "bline.h"
/*
* Bresenham's line drawing algorithm.
*/
void bline(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, plotting_function plot_func, void *context)
{
int dx, dy, i, e;
int incx, incy, inc1, inc2;
dx = x2 - x1;
if (dx < 0)
dx = -dx;
dy = y2 - y1;
if (dy < 0)
dy = -dy;
incx = (x2 < x1) ? -1 : 1;
incy = (y2 < y1) ? -1 : 1;
if (dx > dy) {
plot_func(x1, y1, context);
e = 2 * dy - dx;
inc1 = 2 * (dy - dx);
inc2 = 2 * dy;
for (i = 0; i < dx; i++) {
if (e >= 0) {
y1 += incy;
e += inc1;
} else {
e += inc2;
}
x1 += incx;
plot_func(x1, y1, context);
}
} else {
plot_func(x1, y1, context);
e = 2 * dx - dy;
inc1 = 2 * (dx - dy);
inc2 = 2 * dx;
for (i = 0; i < dy; i++) {
if (e >= 0) {
x1 += incx;
e += inc1;
} else {
e += inc2;
}
y1 += incy;
plot_func(x1, y1, context);
}
}
}