-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbresenham.h
61 lines (50 loc) · 1.38 KB
/
bresenham.h
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
#ifndef BRESENHAM_H
#define BRESENHAM_H
#include<stdio.h>
#include <vector>
static const int
PIXEL_SIZE = 1000,
GAP_SIZE = 1,
WIN_SIZE = 500,
RDIM = WIN_SIZE / (PIXEL_SIZE + GAP_SIZE);
template <typename Cell>
std::vector<Cell> lineBresenham(Cell a0, Cell a1) {
int dx, dy;
int stepx, stepy;
std::vector<Cell> result;
dx = a1.x - a0.x;
dy = a1.y - a0.y;
if (dy<0) {dy=-dy; stepy=-1;} else {stepy=1;}
if (dx<0) {dx=-dx; stepx=-1;} else {stepx=1;}
dy <<= 1;
dx <<= 1;
if ((0 <= a0.x) && (a0.x < RDIM) && (0 <= a0.y) && (a0.y < RDIM))
result.push_back(a0);
if (dx > dy) {
int fraction = dy - (dx >> 1);
while (a0.x != a1.x) {
a0.x += stepx;
if (fraction >= 0) {
a0.y += stepy;
fraction -= dx;
}
fraction += dy;
if ((0 <= a0.x) && (a0.x < RDIM) && (0 <= a0.y) && (a0.y < RDIM))
result.push_back(a0);
}
} else {
int fraction = dx - (dy >> 1);
while (a0.y != a1.y) {
if (fraction >= 0) {
a0.x += stepx;
fraction -= dy;
}
a0.y += stepy;
fraction += dx;
if ((0 <= a0.x) && (a0.x < RDIM) && (0 <= a0.y) && (a0.y < RDIM))
result.push_back(a0);
}
}
return result;
}
#endif // BRESENHAM_H