Last active
February 28, 2016 17:33
-
-
Save kp96/52e6018133f53c92669b to your computer and use it in GitHub Desktop.
DDA Algorithm
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, "DDA Line Drawing"); | |
int x1, y1, x2, y2; | |
x1 = 5, y1 = 5, x2 = 32, y2 = 54; | |
int dx = x2 - x1; | |
int dy = y2 - y1; | |
int steps = max(dx, dy); | |
float x_inc = dx * 1.0 / (steps * 1.0); | |
float y_inc = dy * 1.0 / (steps * 1.0); | |
float x = x1, y = y1; | |
putpixel(abs(x), abs(y), WHITE); | |
for (int i = 0; i < steps; ++i) | |
{ | |
x += x_inc; | |
y += y_inc; | |
putpixel(x, y, WHITE); | |
} | |
delay(5000); | |
line(x1, y1, x2, y2); | |
getch(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment