circle generating algorithms

14
Circle generating algorithms A circle is defined as the set of points that are all at a given distance r from a centre position (x 0 , y 0 ). This distance relationship is expressed by the Pythagorean theorem in Cartesian coordinates as (x- x 0 ) 2 +(y- y 0 ) 2 =r 2 We could solve for y in terms of x: •Direct •Polar coordinate based •Bresenham’s

Upload: deepak-singh

Post on 08-Mar-2015

149 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Circle Generating Algorithms

Circle generating algorithms

A circle is defined as the set of points that are all at a given distance r from a centre position (x0, y0). This distance relationship is expressed by the Pythagorean theorem in Cartesian coordinates as

(x- x0)2+(y- y0 )2=r2

We could solve for y in terms of x:

•Direct•Polar coordinate based•Bresenham’s

Page 2: Circle Generating Algorithms

Direct circle algorithm

• Cartesian coordinates

• Circle equation:( x - xc )2 + ( y - yc )2 = r2

• Step along x axis from xc - r to xc + r and calculate

y = yc ± r2 - ( x - xc )2

Page 3: Circle Generating Algorithms

Polar coordinates

• Polar coordinate equation

x = xc + r cos

y = yc + r sin

• step through values of from 0 to 2π

Page 4: Circle Generating Algorithms

Optimisation and speed-up

• Symmetry of a circle can be used

• Calculations of point coordinates only for a first one-eighth of a circle

(x,y)

(y,x)

(x,-y)

(y,-x)

(-x,y)

(-y,x)

(-y,-x)

(-x,-y)

Page 5: Circle Generating Algorithms

Midpoint Circle algorithm• Used to determine the points needed for drawing a circle. The algorithm is a variant of Bresenham's line algorithm, and is thus sometimes known as Bresenham's circle algorithm, although not actually invented by Bresenham. The algorithm starts accordingly with the circle equation x2 + y2 = r2. So, the center of the circle is located at (0,0). We consider first only the first octant and draw a curve which starts at point (r,0) and proceeds upwards and to the left, reaching the angle of 45°. Any point (x,y) on the boundary of the circle with radius r satisfies the equation fcircle(x,y)=0, that is

                            < 0, if (x,y) is inside the circle boundary

fcircle (x,y)             = 0, if (x,y) is on the circle boundary

                          > 0, if (x,y) is outsider the circle boundary

the relative position of any point can be determined by checking the sign of the circle function.

Page 6: Circle Generating Algorithms

Sampling Position :(xk , yk )

Next Determine,(xk+1, yk) or (xk+1, yk-1)

The circle function tests in the above equation are performed for the midpoints between pixels near the circle path at each sampling step. The figure shows the midpoint between two pixels. Assuming we have just plotted the pixel at (xk,yk). We next need to determine whether the pixel at position (xk+1, yk) or the one at position (xk+1, yk-1) is closer to the circle. The decision parameter is calculated as

    pk = fcircle (xk+1, yk – ½ )        = (xk+1)2 + (yk- ½)2 – r2

if pk<0, this midpoint is inside the circle and the pixel on scan line yk is closer to the circle boundary. Otherwise, the mid-position is outside or on the circle boundary, and we select the pixel on scan-line yk-1.

Successive decision parameters are obtained using incremental calculations.     pk+1 = fcircle (xk+1 + 1, yk+1 – ½ )            = [ (xk+1) + 1 ]2 + [yk+1 – ½ ]2 – r2

    or pk+1 = pk + 2(xk+1) + (y2k+1- y2

k) – (yk+1 – yk) + 1

where yk+1 is either yk or yk-1, depending on the sign of pk.

Page 7: Circle Generating Algorithms

Two Terms ( 0,r ) and (Increments for obtaining pk+1 are either 2xk+1 + 1 (if pk is negative) or 2xk+1 + 1 – 2yk+1. Evaluation of the terms 2xk+1 and 2yk+1 can also be done incrementally as

            2xk+1 = 2xk + 2           2yk+1 = 2yk – 2

Start position (0,r0, these two terms have the values 0 and 2r, respectively. Each successive value is obtained by adding 2 to the previous value of 2x and subtracting 2 from the previous value of 2y.

The initial decision parameter is obtained by evaluating the circle function at the start position (x0,y0) = (0,r):

p0 = fcircle(1, r- ½)

= 1 + (r- ½)2 – r2

(or)

p0 = 5/4 – r

if the radius r is specified as an integer, we can simply round p0 to

p0 = 1 – r (for r an integer)

since all increments are integers.

Page 8: Circle Generating Algorithms

Midpoint Circle Algorithm

1. Input radius r and circle centre (xc,yc), and obtain the first point on the circumference of a circle centered on the origin as (x0,y0) = (0, r).

2. Calculate the initial value of the decision parameter as p0 = 5/4 – r.

3. At each xk position, starting at k=0, perform the following test: if pk<0, the next point along the circle centered on (0,0) is (xk+1, yk) and p                  pk+1 = pk + 2xk+1 + 1otherwise, the next point along the circle is (xk+1, yk-1) and                  pk+1 = pk + 2xk+1 + 1 – 2yk+1 Where 2xk+1 = 2xk + 2; and 2yk+1 = 2yk - 2

4.      Determine symmetry points in the other seven octants.

5.      Move each calculated pixel position (x,y) onto the circular path centered on (xc,yc) and plot the coordinate values.  

x = x + xc,  y = y + yc6.      Repeat steps 3 through 5 until x >= y.

Page 9: Circle Generating Algorithms

/*MID POINT CIRCLE ALGORITHM IMPLEMENTED IN C*/#include<conio.h>#include<stdio.h>#include<graphics.h>#include<math.h>#include<dos.h>int midx,midy,i,gdriver = DETECT, gmode,j,x,y,radius,p,xc,yc,x1,y1;main(){initgraph(&gdriver, &gmode, "");setfillstyle(7,8);bar(0,35,getmaxx(),getmaxy());settextstyle( 2,0,7);border();setcolor(15);gotoxy(60,14);outtextxy(80,200,"ENTER THE VALUES OF X-CENTER ");scanf("%d",&xc);gotoxy(60,17);outtextxy(80,250,"ENTER THE VALUES OF Y-CENTER ");scanf("%d",&xc,&yc);gotoxy(60,20);outtextxy(80,300,"ENTER THE RADIUS OF THE CIRCLE ");scanf("%d",&radius);setcolor(BLUE);for(i=0;i<70;i++){setcolor(4);settextstyle(1,0,3);outtextxy(50+3*i,10,"MIDPOINT CIRCLE ALGORITHM");setcolor((rand()%10)+1);circle(20+3*i,19,12);circle(410+3*i,19,12);delay(7);setcolor(0);settextstyle(1,0,3);outtextxy(50+3*i,10,"MIDPOINT CIRCLE ALGORITHM");circle(20+3*i,19,12);circle(410+3*i,19,12);settextstyle(1,0,1);setfillstyle(7,8);bar(300,450,610,477);setcolor(rand()%100);outtextxy(250,410,"programmed by-Vishal Sagar Bhardwaj");delay(1);} setfillstyle(7,1); bar(0,0,getmaxx(),getmaxy()); border();

setcolor(WHITE);line(getmaxx()/2,0,getmaxx()/2,getmaxy());line(0,getmaxy()/2,getmaxx(),getmaxy()/2);midx = getmaxx() / 2;midy = getmaxy() / 2;setcolor(4);x=0;y=radius;plotpoints();p=1-radius;while(x<y){if(p<0){x=x+1;p=p+(2*x)+1;}else{x=x+1;y=y-1;p=p+2*(x-y)+1;plotpoints();}}getch();closegraph();return 0;}plotpoints(){putpixel(xc+x+midx,midy-yc+y,15);putpixel(xc-x+midx,midy-yc+y,15);putpixel(xc+x+midx,midy-yc-y,15);putpixel(midx+xc-x,midy-yc-y,15);putpixel(midx+xc+y,midy-yc+x,15);putpixel(midx+xc-y,midy-yc+x,15);putpixel(midx+xc+y,midy-yc-x,15);putpixel(midx+xc-y,midy-yc-x,15);}border(){setcolor(5);rectangle(0,0,getmaxx(),getmaxy());rectangle(1,1,getmaxx()-1,getmaxy()-1);rectangle(2,2,getmaxx()-2,getmaxx()-2);}

Page 10: Circle Generating Algorithms

Bresenham’s circle algorithm

1. Input radius r

2. Plot a point at (0, r)

3. Calculate the initial value of the decision parameter as p0 = 5/4 – r ≈ 1 – r

Page 11: Circle Generating Algorithms

4. At each position xk, starting at k = 0, perform the following test:

if pk < 0

plot point at (xk +1, yk)

compute new pk+1 = pk + 2xk+1 + 1else

plot point at (xk + 1, yk – 1)

compute new pk+1 = pk + 2xk+1 + 1 – 2yk+1

where xk+1 = xk + 1 and yk+1 = yk - 1

Page 12: Circle Generating Algorithms

5. Determine symmetry points in the other seven octants and plot points

6. Repeat steps 4 and 5 until x y

Page 13: Circle Generating Algorithms

#includeclass Circle{private:int gd,gm,x,y,r;float d;public:void get();void bres();Circle();~Circle();}Circle::Circle(){gd=DETECT;initgraph(&gd,&gm,"K:\\tc\\bgi");}void Circle::get(){outtextxy(10,30,"Enter the radius of circle");cin>>r;}Circle::~Circle(){closegraph();}void Circle::bres(){cleardevice();outtextxy(100,10,"Bresenham's circle drawing algorithm");get();x=0;y=r;d=3-(2*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(d<=0)d=d+(4*x)+6;

Page 14: Circle Generating Algorithms

• else{d=d+(4*(x-y))+10;y=y-1;}x++;delay(10);

} while(x<Y);}void main(){clrscr();Circle c;c.bres();getch();}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(d<=0)d=d+(4*x)+6;else{d=d+(4*(x-y))+10;y=y-1;}x++;delay(10);

} while(x<Y);}void main(){clrscr();Circle c;c.bres();getch();}