0% found this document useful (0 votes)
30 views

Week 3 Circle Generating Algorithm

This document discusses algorithms for generating circles via digitalization. It begins by explaining Bresenham's circle algorithm which uses eight-way symmetry to incrementally plot points on a circle. It then describes using polynomials and trigonometric functions to define and plot circles but notes they are inefficient. The document concludes by explaining the midpoint circle algorithm, which similarly uses incremental steps and eight-way symmetry to efficiently plot circles digitally.

Uploaded by

Kumar Vaibhav
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Week 3 Circle Generating Algorithm

This document discusses algorithms for generating circles via digitalization. It begins by explaining Bresenham's circle algorithm which uses eight-way symmetry to incrementally plot points on a circle. It then describes using polynomials and trigonometric functions to define and plot circles but notes they are inefficient. The document concludes by explaining the midpoint circle algorithm, which similarly uses incremental steps and eight-way symmetry to efficiently plot circles digitally.

Uploaded by

Kumar Vaibhav
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Circle Generating Algorithm

Bresenham’s Circle Generating


Algorithm
Scan Converting a Circle

A circle is a symmetric figure. Any circle


generating algorithm can take advantage of
the circle’s symmetry to plot eight points for
each value that the algorithm calculates.
Eight way symmetry is used by reflecting
each calculated point around each 45 º axis.
y

-y,x y,x

-x,y x,y

-x,-y x,-y

y,-x
-y,-x
Scan Converting a Circle using Polynomial method:-
This method defines a circle with second order
polynomial equation
y2 = r2 – x2
where x, y = x & y coordinates, r = radius

With this method, each x coordinate in the sector, from


90º to 45º, is found by stepping x from 0 to r / 2 ,
and each y coordinate is found by evaluating r2 – x2
for each step of x.
This is a very inefficient method, however,
because for each point both x & r must be
squared and subtracted from each other;
then the square root of the result must be
y
found.
P(x, r2 – x2 )

y r

x
0 x
• However, unsurprisingly this is not a brilliant
solution!
• Firstly, the resulting circle has large gaps
where the slope approaches the vertical
• Secondly, the calculations are not very
efficient
– The square (multiply) operations
– The square root operation – try really hard to
avoid these!
• We need a more efficient, more accurate
solution
Scan Convert a Circle using trigonometric
method:-
This second method of defining a circle uses
functions : x = r cos θ, y = r sin θ
y
P(r cos θ,r sin θ)

r sin θ
θ
x
r cos θ
θ = current angle, r = circle radius
By this method θ is stepped from θ to π /4
& each value of x & y is calculated.
Computation of values of sin θ & cos θ are
very time consuming
Bresenham’s Circle Algorithm
Working: If the eight-way symmetry of a circle is used
to generate a circle, points will have to be
generated through 45º angle. And, if points are
generated from 90º to 45º, moves will be made
only in the +x and –y directions.

If points are generated from 90º to 45º each new point


closest to the true circle can be found by taking
either of two actions:
(1) Move in the x direction one unit or
(2) Move in the x direction 1 unit & -ve y direction 1
unit.
y

T (xi+1,yi)
yi

(xi+1,yi-1)
yi -1
S

xi xi + 1 x
• Assume that (xi, yi) are the coordinates of the
last scan-converted pixel upon entering step i.
• Let the distance from the origin to pixel T
squared minus the distance to the true circle
squared = D(T) = t2 – r2.
• And let the distance from the origin to pixel S
squared minus the distance to the true circle
squared = D(S) = s2 – r2.
• Coordinates of T are (xi +1, yi)
S are (xi +1, yi – 1)
• Following expressions can be developed:
D(T) = (xi + 1)2 + yi2 – r2
D(S) = (xi + 1)2 + (yi – 1)2 – r2

• This function D provides a relative


measurement of the distance from the center
of a pixel to the true circle. Since D(T) is always
+ve (T is outside the circle) & D(S) will always
be –ve (S is inside the true circle), a decision
variable di can be defined as:
di = D(T) – (-D(S)) = D(T) + D(S)
Therefore,
di = 2(xi + 1)2 + yi2 + (yi – 1)2 – 2r2
• When di<0, we have |D(T)|<|D(S)| and pixel T
is chosen.
• When di>0, we have |D(T)|>|D(S)| and pixel S
is selected.
Decision variable for next step:
di+1 = 2(xi+1 + 1)2 + yi+12 + (y i+1 – 1)2 – 2r2
Hence,
di+1 – di = 2(xi+1 + 1)2 + yi+12 + (yi+1 – 1)2 -
2(xi + 1)2 - yi2 - (yi – 1)2
Since xi+1 = xi + 1, we have
di+1 = di + 4xi + 2(yi+12 - yi2) – 2(yi+1 – yi) + 6
If T is chosen pixel (meaning that di < 0) then
yi+1 = yi and so di+1 = di + 4xi + 6
If S is chosen (meaning that di > 0) then yi+1 =
yi –1 and so di+1 = di + 4(xi-yi) + 10
Hence we have
di+1 = di + 4xi + 6 if di < 0
di + 4(xi-yi) + 10 if di > 0
Finally, we set(0,r) to the starting pixel
coordinates and compute d1 from the original
definition of di:
x = 0, y = r
d1= 2(0 + 1)2 + r2 + (r-1)2 – 2r2
d1= 3 – 2r
Algorithm:

For generating all the pixel coordinates in the


90º to 45º octant that are needed when scan-
converting a circle of radius r.
(i) int x = 0, y = r, d = 3-2r
(ii) while (x<= y)
{
setpixel(x,y)
if (d < 0)
d = d + 4x + 6
else
{
d = d + 4(x - y) + 10
y--
}
x++
}
circlePlotPoints(xCenter, yCenter, x, y);

void circlePlotPoints (int xCenter, int yCenter, int x, int y)


{
setPixel (xCenter + x, yCenter + y);
setPixel (xCenter – x, yCenter + y);
setPixel (xCenter + x, yCenter – y);
setPixel (xCenter – x, yCenter – y);
setPixel (xCenter + y, yCenter + x);
setPixel (xCenter – y, yCenter + x);
setPixel (xCenter + y, yCenter – x);
setPixel (xCenter – y, yCenter – x);
}

18
Consider origin centered circle with radius 8

xi=0, yi=8, ∆i= 2(1-8) = -14


setpixel ∆i ∂ ∂‘ x y
-14 0 8
(0,8)
-11 -13 1 8
(1,8)
-6 -7 2 8
(2,8)
-12 3 3 7
(3,7)
-3 -11 4 7
(4,7)
-3 7 5 6
(5,6)
1 5 6 5
(6,5)
Eight-Way Symmetry
• The first thing we can notice to make our circle
drawing algorithm more efficient is that circles
centred at (0, 0) have eight-way symmetry.
(-x, y) (x, y)

(-y, x) (y, x)

R
2
(-y, -x) (y, -x)

(-x, -y) (x, -y)


Mid Point Circle Drawing
Algorithm
Mid-Point Circle Algorithm
• Similarly to the case with
lines, there is an incremental
algorithm for drawing circles –
the mid-point circle algorithm
• In the mid-point circle
algorithm we use eight-way
symmetry so only ever
The mid-point circle
calculate the points for the algorithm was developed
top right eighth of a circle, by Jack Bresenham

and then use symmetry to get


the rest of the points
Midpoint Circle Algorithm
Mid point algorithm is very similar to
Bresenham’s approach. It is based on the
following function for testing the spatial
relationship between an arbitrary point(x,y)
and a circle of radius centered at the origin:
< 0 (x,y) inside circle
f(x,y) = x2 + y2 – r2 = 0 (x,y) on the circle
> 0 (x,y) outside circle
• Now consider the coordinates of the point
halfway between pixel T & S (xi + 1, yi – ½)
This is called the midpoint and we use it to
define a decision parameter:
pi = f( xi +1, yi – ½)
= (xi + 1)2 + (yi – ½)2 – r2
• If pi is –ve , the midpoint is inside the circle,
then we choose pixel T.
• If pi is +ve, the midpoint is outside the circle,
& we choose S.
• Similarly, the decision parameter for the next
step is:
pi+1 = (xi+1 + 1)2 + (yi+1 – ½)2 – r2
• Since x i+1 = xi + 1
pi+1 - pi = [(xi + 1)+ 1]2 – (xi + 1)2
+ (y i+1 – ½)2 – (yi – ½)2
• Hence
pi+1 = pi + 2(xi + 1) + 1 + (y i+12 – yi2)
– (y i+1 – yi)
If pixel T is chosen (meaning pi < 0), we have
yi+1 = yi.
If pixel S is chosen (meaning pi > 0), we have
yi+1 = yi – 1. Thus,
pi+1= pi + 2(xi + 1) +1 if pi<0
pi + 2(xi + 1) +1 – 2(yi – 1) if pi>0
In terms of (xi, yi), we have
pi+1= pi + 2xi + 3 if pi<0
pi + 2(xi - yi ) + 5 if pi>0
• Finally, we compute the initial value for the
decision parameter using the original
definition of pi and (0,r):
pi = (0 + 1)2 + (r – ½)2 – r2 = 5/4 – r
• One can see that this is not really integer
computation. However, when r is an integer
we can simply set p1= 1 – r.
• The error of being ¼ less than the precise
value does not prevent p1 from getting the
appropriate sign.
• It does not affect the rest of the scan
conversion process, because the decision
variable is only updated with integer
increment in subsequent steps.
Mid Point Circle Algorithm:

The following is a description of this midpoint circle algorithm that


generates the pixel coordinates in the 90˚ to 45 ˚ octant:
int x = 0, y = r, p = 1-r
while (x <=y)
{ setpixel(x,y);
if (p < 0)
p = p + 2x + 3
else {
p = p + 2(x – y) + 5
y- -
}
x++
}
circlePlotPoints(xCenter, yCenter, x, y);

void circlePlotPoints (int xCenter, int yCenter, int x, int y)


{
setPixel (xCenter + x, yCenter + y);
setPixel (xCenter – x, yCenter + y);
setPixel (xCenter + x, yCenter – y);
setPixel (xCenter – x, yCenter – y);
setPixel (xCenter + y, yCenter + x);
setPixel (xCenter – y, yCenter + x);
setPixel (xCenter + y, yCenter – x);
setPixel (xCenter – y, yCenter – x);
}

31

You might also like