Thursday 13 October 2011


EX .NO:1                LINE DRAWING USING DDA ALGORITHM
DATE:

AIM:
To write a C program to draw a line using DDA algorithm.
ALGORITHM:
Step 1: Input the line endpoints and store the left endpoint in (x1, y1) and right
endpoint in (x2, y2)
Step 2: Calculate the values of ∆x and ∆y using ∆x = xb – xa, ∆y = yb – ya
Step 3: if the values of ∆x > ∆y assign values of steps as ∆x otherwise the values
of steps as ∆y
Step 4: Calculate the values of X increment and Y increment and assign
the value x= xa and y = ya
Step 5: for k=1 to steps do
X = X + X increment
Y= Y + Y increment
Putpixel(ceil(x), ceil(y),15)

PROGRAM:
#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "graphics.h"
main()
{
intgd=DETECT,gm;
intxa,xb,ya,yb;
intdx,dy,steps,k,xinc,yinc,x,y;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("Enter the two left end pixel points(xa,ya):\n");
scanf("%d%d",&xa,&ya);
printf("Enter the two Right end pixel points(xb,yb):\n");
scanf("%d%d",&xb,&yb);
dx=xb-xa;
dy=yb-ya;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=dx/steps;
yinc=dy/steps;
x=xa;
y=ya;
putpixel(x,y,6);
for(k=1;k<=steps;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,6);


}
getch();
return(0);
}
OUTPUT:
Enter the two left end pixel points(xa,ya):\n")
scanf("%d%d",&xa,&ya);
printf("Enter the two Right end pixel points(xb,yb):\n");
scanf("%d%d",&xb,&yb);
Enter The Two Left Endpoints(xa,ya)   :       100 200
 Enter The Two Right Endpoints(xb,yb) :      200   200

RESULT:
Thus the program of line drawing using DDA algorithm is executed and output is obtained .

0 comments:

Post a Comment