Sunteți pe pagina 1din 9

Ex. No.

1a) : Digital Differential Analyzer (DDA) Line Drawing Method


Date :
Aim :
To write a C Program for Line Drawing using Digital Differential Analyzer
(DDA) Method.
Algorithm:
Step 1: Input
Input the two end point pixel positions say(x1,y1) and (x2,y2)
Step 2: Calculate the deflection voltage for both Horizontal and Vertical axes and
assign it to dx and dy respectively,
dx=x2-x1 ; dy=y2-y1
Step 3: Calculate the steps, number of intermediate pixel
Steps=Max(dx,dy)
Step 4: Calculate and plot the points
Plot the starting points
Repeat the following process for steps times
Calculate the offset xinc=dx/steps and yinc=dy/steps
Increment the x and y
X=x+increment , y=y+increment
Plot (x,y)
PROGRAM:
#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "graphics.h"
main()
{
int gd=DETECT,gm;
int xa,xb,ya,yb;
int dx,dy,steps,k,xinc,yinc,x,y;
initgraph(&gd,&gm,"E:\\tcc\\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);
}

INPUT:
Enter The Two Left Endpoints(xa,ya) :

234

100

Enter The Two Right Endpoints(xb,yb) :

578

250

OUTPUT

RESULT:
Thus the C program to implement the DDA algorithm for line drawing was
written, executed and the output was verified successfully.

Ex. No. 1b) :


Date :

BRESENHAMS LINE DRAWING ALGORITHM

Aim :
To write a C Program for Line Drawing using Bresenhams Line Drawing
Algorithm.
Algorithm :
Step 1: Input
Input the two end points say(x1,y1) and (x2,y2) while (x2,y2)=(x1,y1)
Plot (x0,y0)
Step 2: Calculate the deflection voltage for both Horizontal and Vertical axes and
assign it to x and y respectively,
x=x2-x1 ; y=y2-y1
Step 3: Calculate the initial decision parameter P0=2 y- x
Step 4: Calculate from k=0 repeat for x times
if (Pk<0)
{
Plot(Xk+1,Yk)
Pk+1=Pk+2 y;
}
else
{
Plot(Xk+1,Yk+1)
Pk+1=Pk+2 y-2 x;
}
PROGRAM:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
void main()
{
float x,y,x1,y1,x2,y2,dx,dy,e;
inti,gd,gm;

clrscr();
printf("Enter the (x1,y1) coordinate:");
scanf("%f%f",&x1,&y1);
printf("Enter the (x2,y2) coordinate:");
scanf("%f%f",&x2,&y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI\\");
dx=abs(x2-x1);
dy=abs(y2-y1);
x=x1;
y=y1;
e=2*dy-dx;
i=1;
do
{
putpixel(x,y,15);
while(e>=0)
{
y=y+1;
e=e-2*dx;
}
x=x+1;
e=e+2*dy;
i=i+1;
}
while(i<=dx);
getch();
closegraph();
}

INPUT:
Enter the (x1, y1) coordinates : 200 100
Enter the (x2, y2) coordinates : 500 100

OUTPUT:

RESULT:
Thus the C program to implement the Bresenhams algorithm for line drawing was
written, executed and the output was verified successfully.

Ex. No. 1c) :

MID POINT CIRCLE DRAWING ALGORITHM

Date :
Aim :
To write a C Program for Circle Drawing using Mid Point Circle Drawing
Algorithm.
Algorithm :
Step 1: Input
Input the radius r , circle center(Xc , Yc)
1st Point = (Xo,Yo)=(0,r)
Step 2: Calculate the initial value of decision parameter
Po=(5/4)-r (or) Po=1-r
Step 3: At each Xk, from k=0, check Pk
If(Pk<0)
next point is (Xk+1 , Yk)
Pk+1=Pk+2(Xk+1)+1
else
next point is (Xk+1 , Yk-1)
Pk+1=Pk+2(Xk+1)+1-2(Yk+1)
2Xk+1=2Xk+2
2Yk+1=2Yk-2
Step 4: Determine symmetry points in other 7 octants
Step 5: Plot X=X+Xc,Y=Y+Yc
Step 6: Repeat steps 3,4,5 until XY
PROGRAM:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
void main()
{

float p;
intgd,gm,x,y,i,r;
clrscr();
printf("Enter the radius of a circle:");
scanf("%d",&r);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI\\");
x=0;
y=r;
p=1.25-r;
do
{
putpixel(200+x,200+y,15);
putpixel(200+y,200+x,15);
putpixel(200+y,200-x,15);
putpixel(200+x,200-y,15);
putpixel(200-x,200-y,15);
putpixel(200-y,200-x,15);
putpixel(200-y,200+x,15);
putpixel(200-x,200+y,15);
if(p<0)
{
x=x+1;
y=y;
p=p+2*x+1;
}
else
{
x=x+1;
y=y-1;
p=p+2*(x-y)+1;
}
}
while(x<y);
getch();
closegraph();
}

INPUT:
Enter the radius of circle : 150
OUTPUT:

RESULT:
Thus the C program to implement the Mid point algorithm for Circle drawing was
written, executed and the output was verified successfully.

S-ar putea să vă placă și