Last active
August 29, 2015 14:21
-
-
Save tungpun/1d3e37d8df652435f295 to your computer and use it in GitHub Desktop.
Bresenham - CG Self Review
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
int sign(double x) { | |
if (x < 0) return (-1); | |
else return 1; | |
} | |
void Bresenham(int x0, int y0, int x1, int y1) { | |
int dx, ax, sgnx, dy, ay, sgny, x, y, d; | |
dx = x1 - x0; | |
ax = abs(dx) * 2; | |
sgnx = sign(dx); | |
dy = y1 - y0; | |
ay = abs(dy) * 2; | |
sgny = sign(dy); | |
x = x0; | |
y = y0; | |
if (ax > ay) { | |
d = ay - ax/2; | |
while (true) { | |
cout << x << " " << y << endl; | |
if (x == x1) break; | |
if (d >= 0) { | |
y = y + sgny; | |
d = d - ax; | |
} | |
x = x + sgnx; | |
d = d + ay; | |
} | |
} | |
else { | |
d = ax - ay/2; | |
while (true) { | |
cout << x << " " << y << endl; | |
if (y == y1) break; | |
if (d >= 0) { | |
x = x + sgnx; | |
d = d - ay; | |
} | |
y = y + sgny; | |
d = d + ax; | |
} | |
} | |
} | |
int main() { | |
Bresenham(3, 4, 7, 10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment