DDA Line Drawing C Program
Digital differential analyzer (DDA) is a floating-point operation based computer line drawing algorithm.
Follow The Instructions To Successfully Run The Program In Dev-Cpp/CodeBlock: Whenever you #include <graphics.h> in a program, you must instruct the linker to link in certain libraries. The command to do so from Dev-C++ is Alt-P. Choose the Parameters tab from the pop-up window and type the following into the Linker area: -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32 Note: for loops in the program is written using -std=c99 or -std=gnu99 syntex.
Code
#include <stdio.h> #include <math.h> #include <graphics.h> void dda(int x1, int y1, int x2, int y2) { initwindow(500, 500, "DDA"); int step, xInc, yInc, x, y, dx, dy; dx = x2 - x1; dy = y2 - y1; step = (abs(dx) > abs(dy))? dx : dy; xInc = dx / step; yInc = dy / step; x = x1; y = y1; putpixel(round(x), round(y), 1); for (int i = 0; i < step; i++) { x += xInc; y += yInc; putpixel(round(x), round(y), 1); } } int main() { int x1,y1, x2, y2; printf("Enter The Points:\n"); printf("(x1,y1): ? "); scanf("%d%d",&x1,&y1); printf("(x2,y2): ? "); scanf("%d%d",&x2,&y2); dda(x1,y1,x2,y2); while(!kbhit()); return 0; }