Created
February 28, 2016 17:34
-
-
Save kp96/8d1a1582b7fc94905c0b to your computer and use it in GitHub Desktop.
BresenhamAlgorithm
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 <stdio.h> | |
#include <graphics.h> | |
#include <math.h> | |
int max(int a, int b) {return a > b ? a : b;} | |
int main(int argc, char const *argv[]) | |
{ | |
int gd = DETECT, gm; | |
initgraph(&gd, &gm, "Bresenham Line Drawing"); | |
int x1, y1, x2, y2; | |
x1 = 5, y1 = 5, x2 = 32, y2 = 54; | |
int dx = x2 - x1; | |
int dy = y2 - y1; | |
float m = (y2 - y1 * 1.0) / (x2 - x1); | |
putpixel(x1, y1, WHITE); | |
if(m <= 1) { | |
int p = 2 * dy - dx; | |
int x = x1, y = y1; | |
while(x < x2) { | |
x++; | |
if(p < 0) { | |
p += 2 * dy; | |
} | |
else { | |
p += 2 * (dy - dx); | |
y++; | |
} | |
putpixel(x, y, WHITE); | |
} | |
} | |
else { | |
int p = 2 * dx - dy; | |
int x = x1, y = y1; | |
while(y < y2) { | |
y++; | |
if(p < 0) { | |
p += 2 * dx; | |
} | |
else { | |
p += 2 * (dx - dy); | |
x++; | |
} | |
putpixel(x, y, WHITE); | |
} | |
} | |
delay(2000); | |
line(x1, y1, x2, y2); | |
getch(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment