Computer Graphics

Tutorial No. 1 : About DDA Line drawing Algorithm

Source Code in C Langauge

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>

int main() { int xstart,ystart,xend,yend,dx,dy,steps,i,xtemp,ytemp; float x,y,xinc,yinc; int gdriver = DETECT, gmode; clrscr(); printf("Enter the X value of Start Point : "); scanf("%d",&xstart); printf("Enter the Y value of Start Point : "); scanf("%d",&ystart); printf("Enter the X value of End Point : "); scanf("%d",&xend); printf("Enter the Y value of End Point : "); scanf("%d",¥d); dx = xend-xstart; dy = yend-ystart; if(dx>dy) { steps = dx; } else { steps = dy; } xinc = dx/(float)steps; yinc = dy/(float)steps; printf("Value of Start Point is (%d,%d) ",xstart,ystart); printf("\nValue of End Point is (%d,%d) ",xend,yend); printf("\nValue of Step is %d ",steps); printf("\nValue of X incement is %.2f ",xinc); printf("\nValue of Y incement is %.2f ",yinc); getch(); x=xstart; y=ystart; clrscr(); initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); printf("\nValue of Point 1 is (%d,%d) ",xstart,ystart); for(i=1;i<=steps;i++) { x=x+xinc; y=y+yinc; xtemp = x; ytemp = y; if((x-xtemp)>0.5) { xtemp++; } if((y-ytemp)>0.5) { ytemp++; } putpixel(xtemp,ytemp,4); printf("\nValue of Point %d is (%d,%d) ",(i+1),xtemp,ytemp); } getch(); closegraph(); return 0; }