-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgraph_bresenham.h
More file actions
53 lines (49 loc) · 1.37 KB
/
graph_bresenham.h
File metadata and controls
53 lines (49 loc) · 1.37 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
#ifndef GRAPH_BRESENHAM_H
#define GRAPH_BRESENHAM_H
#include <cmath>
namespace graph
{
// Call plot(_, _) to draw pixels on line from (x, y) inclusive to
// (x2, y2) exclusive.
void draw_line(int x, int y, int x2, int y2, void(*plot)(int, int))
{
int dx = std::abs(x2 - x);
int dy = -std::abs(y2 - y);
int sx = (x < x2 ? 1 : -1);
int sy = (y < y2 ? 1 : -1);
int error = dx + dy;
while (x != x2 || y != y2)
{
plot(x, y);
int error2 = 2 * error;
if (error2 > dy)
{
x += sx;
error += dy;
}
if (error2 < dx)
{
y += sy;
error += dx;
}
}
}
// Call plot(_, _) to draw pixels on circle at origin with given radius.
void draw_circle(int radius, void(*plot)(int, int))
{
int x = radius;
int y = 0;
int error = 3 - 2 * radius;
while (x >= y)
{
plot(x, y); plot(x, -y); plot(-x, y); plot(-x, -y);
plot(y, x); plot(y, -x); plot(-y, x); plot(-y, -x);
if (error > 0)
{
error -= 4 * (--x);
}
error += 4 * (++y) + 2;
}
}
} // namespace graph
#endif // GRAPH_BRESENHAM_H