Professional Documents
Culture Documents
Ex 1
Ex 1
Ex 1
INPUT:
Enter The Two Left Endpoints(xa,ya) :
234
100
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.
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.
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.