void drawLine Point Point int int dx abs dy abs int s1 sign s2 sign in

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void drawLine(Point a, Point b) {
int x = a.x, y = a.y;
int dx = abs(b.x - a.x), dy = abs(b.y - a.y);
int s1 = sign(b.x - a.x), s2 = sign(b.y - a.y);
int ss = s1 == s2 ? 1 : -1;
bool swaped = false;
if (dy > dx) {
std::swap(dx, dy);
swaped = true;
}
int e = 2*dy - dx;
for (int i = 1; i <= dx; i++) {
buff.plot(x, y);
buff.plot(x, y - ss*s2);
buff.plot(x + ss*s1, y);
while (e >= 0) {
x += swaped ? s1 : (y += s2, 0);
e -= 2*dx;
}
x += swaped ? (y += s2, 0) : s1;
e += 2*dy;
}
}