Menu

[b84aab]: / src / common / g_line.c  Maximize  Restore  History

Download this file

96 lines (88 with data), 1.8 kB

 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// This file is part of SmallBASIC
//
// Bresenham's algorithm for drawing line
//
// This program is distributed under the terms of the GPL v2.0 or later
// Download the GNU Public License (GPL) from www.gnu.org
//
// Copyright(C) 2000 Nicholas Christopoulos
#include "common/sys.h"
void g_line(int x1, int y1, int x2, int y2, void(*dotproc)(int, int)) {
int dX, dY, row, col, final, G, inc1, inc2;
char pos_slope;
dX = x2 - x1;
dY = y2 - y1;
pos_slope = (dX > 0);
if (dY < 0)
pos_slope = !pos_slope;
if (ABS(dX) > ABS(dY)) {
if (dX > 0) {
col = x1;
row = y1;
final = x2;
} else {
col = x2;
row = y2;
final = x1;
}
inc1 = 2 * ABS(dY);
G = inc1 - ABS(dX);
inc2 = 2 * (ABS(dY) - ABS(dX));
if (pos_slope) {
while (col <= final) {
dotproc(col, row);
col++;
if (G >= 0) {
row++;
G += inc2;
} else
G += inc1;
}
} else {
while (col <= final) {
dotproc(col, row);
col++;
if (G > 0) {
row--;
G += inc2;
} else
G += inc1;
}
}
} /* if |dX| > |dY| */
else {
if (dY > 0) {
col = x1;
row = y1;
final = y2;
} else {
col = x2;
row = y2;
final = y1;
}
inc1 = 2 * ABS(dX);
G = inc1 - ABS(dY);
inc2 = 2 * (ABS(dX) - ABS(dY));
if (pos_slope) {
while (row <= final) {
dotproc(col, row);
row++;
if (G >= 0) {
col++;
G += inc2;
} else
G += inc1;
}
} else {
while (row <= final) {
dotproc(col, row);
row++;
if (G > 0) {
col--;
G += inc2;
} else
G += inc1;
}
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.