Skip to content

Instantly share code, notes, and snippets.

@kp96
Last active February 28, 2016 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kp96/52e6018133f53c92669b to your computer and use it in GitHub Desktop.
Save kp96/52e6018133f53c92669b to your computer and use it in GitHub Desktop.
DDA Algorithm
#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