Implementation of Line, Circle and ellipse Attributes - Code

Latest

programs

Friday, June 10, 2011

Implementation of Line, Circle and ellipse Attributes


CIRCLE DRAWING



AIM:
               To write a program to draw a circle using Bresenham’s circle drawing Algorithm.

FUNCTIONS USED:
Circle()

      The function circle() is used to draw a circle using(x,y) as centre point.
 Syntax:
         circle (x,y,radius)
initgraph().

       This function takes thee arguments and they are
         i).the video driver to be used (gd).
         ii).the graphics mode (gm).
         iii).the path name.
Syntax:
       Initgraph(gd,gm,path)
Putpixel ()
        The function putpixel() is used to place a pixel at particular coordinate

Syntax:

Putpixel(x,y,color)















ALGORITHM:

Step 1: Start

Step 2: Get the center point as (xc,yc),get the radius as r.

Step 3: Assign y=r,x=0

Step 4: Calculate p=3-2r

Step 5: If p<0,p=p+4x+6 else p=p+10+4(x-y) and y=y-1

Step 6: Increment x by 1

Step 7: Do steps (9) to (16)

Step 8: Repeat steps (5) to (9) until x<=y

Step 9: Put a pixel on (xc+x,yc+y,15);

Step 10: Put a pixel on (xc+x,yc-y,15);

Step 11: Put a pixel on (xc-x,yc+y,15);

Step 12: Put a pixel on (xc-x, yc-y, 15);

Step 13: Put a pixel on (xc+y,yc+x,15);

Step14: Put a pixel on (xc+y, yc-x, 15);

Step 15: Put a pixel on (xc-y, yc+x, 15);

Step 16: Put a pixel on (xc-y, yc-x, 15);

Step 17: Stop











PROGRAM:

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void my_circle(int,int,int);
void plot(int,int,int,int);
void main()
{
int gd=DETECT,gm;
int xc,yc,r;
initgraph(&gd,&gm,"");
cout<<"enter X-Axis----->";
cin>>xc;
cout<<"enter  Y-Axis----->";
cin>>yc;
cout<<"enter radius----->";
cin>>r;
my_circle(xc,yc,r);
getch();
closegraph();
}
void my_circle(int xc,int yc,int r)
{
int x,y,p;
p=3-2*r;
x=0;
y=r;
while(x<=y)
{
if(p<0)
p=p+(4*x+6);
else
{
p=p+10+4*(x-y);
y=y-1;
}
plot(xc,yc,x,y);
x=x+1;
}
}
void plot(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,15);
putpixel(xc+x,yc-y,15);
putpixel(xc-x,yc+y,15);
putpixel(xc-x,yc-y,15);
putpixel(xc+y,yc+x,15);
putpixel(xc+y,yc-x,15);
putpixel(xc-y,yc+x,15);
putpixel(xc-y,yc-x,15);
}




INPUT:

Enter X-Axis----->:100
Enter Y-Axis----->:200
Enter radius----->: 40

OUTPUT:


 











:
    

RESULT:
                         Thus the program is executed and verified.

 

No comments:

Post a Comment