Showing posts with label line algorithm using c. Show all posts
Showing posts with label line algorithm using c. Show all posts

Saturday, January 16, 2010

DDA LINE Drawing Algorithm



download this file : dda.c


program:
#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
#include"graphics.h"
#include"dos.h"
#include"math.h"
void main()
{
int i,gd,gm,delay=1;
float dx,dy,x1,x2,y1,y2,step,maxx,maxy,roundx,roundy;
float x,y,xi,yi;
char buf1[50],buf2[50],buf3[50],buf4[10],buf5[10];
clrscr();
gd=DETECT;
printf("\n Enter start co-ordinates (x1,y1) :");
scanf("%f%f",&x1,&y1);
printf("\n Enter end co-ordinates  (x2,y2) :");
scanf("%f%f",&x2,&y2);
clrscr();
x1+=100;
x2+=100;
y1+=100;
y2+=100;
initgraph(&gd,&gm,"");
dx=x2-x1;
dy=y2-y1;
maxx=getmaxx();
maxy=getmaxy();
line(100,0,100,maxy);
line(0,100,maxx,100);
outtextxy(80,90,"0");
outtextxy(80,200,"100");
outtextxy(80,300,"200");
outtextxy(80,400,"300");
outtextxy(80,500,"400");
outtextxy(180,90,"100");
outtextxy(280,90,"200");
outtextxy(380,90,"300");
outtextxy(480,90,"400");
outtextxy(580,90,"500");
sprintf(buf1," DDA LINE ALGORITHM ");
outtextxy(300,10,buf1);
sprintf(buf2,"x1=%3.0f,y1=%3.0f,x2=%3.0f,y2=%3.0f,",x1-100,y1-100,x2-100,y2-100);
outtextxy(200,20,buf2);
if(abs(dx)>abs(dy))
step=dx;
else
step=dy;
xi = dx / step;
yi = dy / step;
sprintf(buf3,"dx=%3.0f,dy=%3.0f,step=%3.0f,xi=%3.2f,yi=%3.2f",dx,dy,step,xi,yi);
outtextxy(200,30,buf3);
x=x1;
y=y1;
putpixel(x,y,delay);
circle(x,y,2);
sprintf(buf4,"(%3.0f,%3.0f)",x1-100,y1-100);
outtextxy(x1+10,y1,buf4);
for(i=1;i<=step;i++)
{
  if(delay==15)
  {
sleep(1);
delay=1;
  }
  delay++;
  x=x+xi;
  y=y+yi;
  roundx=ceil(x);
  roundy=ceil(y);
  putpixel(roundx,roundy,delay);
}
circle(roundx,roundy,2);
sprintf(buf5,"(%3.0f,%3.0f)",roundx-100,roundy-100);
outtextxy(roundx+10,roundy,buf5);

getch();
restorecrtmode();
}

download this file : dda.c


ouput:
 Enter start co-ordinates (x1,y1) : 100    125
 Enter end co-ordinates  (x2,y2) : 250    350