Anda di halaman 1dari 6

Punjabi university computer science department 01/02/2017

Name :-Abdeta Hussien


Roll No:-16071060
Assignment 2
Write the Program that draw circle using the following algorithm.
Direct approach algorithm
Polar domain algorithm
bresenham algorithm
Midpoint algorithm
#include<iostream>
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
using namespace std;
int ROUND(double x)
{
return((int)(x+0.5));
}
void put_circle_pixel(int h,int k,int x,int y,int colour)
{
putpixel(h+x,k+y,colour);
putpixel(h+x,k-y,colour);
putpixel(h-x,k-y,colour);
putpixel(h-x,k+y,colour);
putpixel(h+y,k+x,colour);
putpixel(h+y,k-x,colour);
putpixel(h-y,k-x,colour);

1
Punjabi university computer science department 01/02/2017

putpixel(h-y,k+x,colour);
}
int main()
{
int gdriver = DETECT, gmode, errorcode;
float h,k,x,y,r,deltatheta,theta,p;
int a,colour=6;
char c;
initgraph(&gdriver, &gmode, "C:\\TC\\BGI");
errorcode = graphresult();
if (errorcode != grOk)
{
cout<<"Graphics error: %s\n", grapherrormsg(errorcode);
cout<<"Press any key to halt:";
getch();
exit(1);
}
do{
cout<<"choose the circle algorithm\n";
cout<<"1. Direct Approach\n";
cout<<"2. polar domain\n";
cout<<"3. bresenham circle\n";
cout<<"4. mid-point circle\n";
cout<<"Enter your algorithm choice(1-4)";
cin>>a;
cout<<"Enter the value of h = ";
cin>>h;
cout<<"Enter the value of k = ";

2
Punjabi university computer science department 01/02/2017

cin>>k;
cout<<"Enter the value of r = ";
cin>>r;

switch(a)

case 1:
cout<<"The algrorithm is direct algorithm";
x=0;
y=r;
put_circle_pixel(h,k,x,y,colour);
while(x<=y)
{
x++;
y=sqrt((r*r)-(x*x));
put_circle_pixel(h,k,ROUND(x),ROUND(y),colour);
}
break;
case 2:
cout<<"The algorithm is polar domain";
deltatheta= 1/r;
theta = deltatheta;
x=r;
y=0;
put_circle_pixel(h,k,x,y, colour);
while(theta<=M_PI_4)

3
Punjabi university computer science department 01/02/2017

{
x=r*cos(theta);
y=r*sin(theta);
put_circle_pixel(h,k,ROUND(x),ROUND(y),colour);
theta=theta+deltatheta;
}
break;
case 3:
cout<<"The algorithm is bresenham circle algorithms";
x=0;
y=r;
p=3-2*r;
put_circle_pixel(h,k,x,y,colour);
while(x<y)
{
if (p<0)
p=p+4*x+6;
else
{
p=p+4*(x-y)+10;
y=y-1;
}
x=x+1;
put_circle_pixel(h,k,ROUND(x),ROUND(y),colour);
}
break;
case 4:
cout<<"The algorithm is mid-point circle algorithms";

4
Punjabi university computer science department 01/02/2017

x=0;
y=r;
p=1-r;
put_circle_pixel(h,k,x,y,colour);
while(x<=y)
{
if (p<0)
p=p+2*x+3;
else
{
p=p+2*(x-y)+5;
y=y-1;
}
x=x+1;
put_circle_pixel(h,k,ROUND(x),ROUND(y),colour);
}
break;
default:
{
cout<<"wrong input";
}
}
getch();
cleardevice();
cout<<"Do you want to continue\n";
cout<<"If yes press 'y' or 'Y' otherwise press any other key\n";
cin>>a;
cin>>c;

5
Punjabi university computer science department 01/02/2017

}
while(c=='y' || c=='Y');
getch();
closegraph();
return 0;
}

Anda mungkin juga menyukai