April 21, 2023 1
We could solve for y in terms of x
( x0, y0 ) are the origin points
A Simple Circle Drawing Algorithm
The equation for a circle is:
where r is the radius of the circle
222 ryx
22 xry OR
April 21, 2023 2
20020 220 y
20120 221 y
20220 222 y
61920 2219 y
02020 2220 y
Let r = 2022 xry
April 21, 2023 3
x0 = 0
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 efficientThe square (multiply) operationsThe square root operation – try really hard to avoid these!
We need a more efficient, more accurate solution
April 21, 2023 4
Parametric polar representation
x = r . cos (ө)
y = r . sin (ө)
x = xc + r . cos (ө)
y = yc + r . sin (ө)
OR
Arc Length
x1 = r . cos (ө) x2 = r . cos (ө + dө)
y1 = r . sin (ө) y2 = r . sin (ө + dө)
dө: is a fixed angular step size April 21, 2023 5
x2 = x1 . cos (dө) – y1 . sin (dө)
y2 = y1 . cos (dө) + x1 . sin (dө)
By using trigonometry ... get
dtheta = 1/r ;cdetheta = cos (dtheta) ;sdetheta = sin (detheta) ;x = 0 ;y = r ;while (x<=y){Plotpixel (x, y) ; // for eight points xtemp = x ;x = x * cdetheta – y * sdetheta ;y = y * cdetheta + xtemp * sdetheta ;}
April 21, 2023 6
Help for programming
Bresenham’s algorithm for circles
Key idea: compute the initial octant only
Generate firstoctant
1
Reflect first octant about y=x2
Reflect firstquadrantabout x=0
3
Reflect uppersemicircleabout y=0
4
Bresenham’s incremental circle algorithm
Example: circle of radius 8 Bright pixels:
initial pixel (0,8)(1,8)(2,8)(3,7)(4,7)(5,6)(6,5)(7,4)(7,3)(8,2)(8,1)end pixel (8,0)
April 21, 2023 8
April 21, 2023 9
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
April 21, 2023 10
Mid-Point Circle Algorithm
Similarly to the case with lines, there is an incremental algorithm for drawing circles – the mid-point circle algorithmIn the mid-point circle algorithm we use eight-way symmetry so only ever calculate the points for the top right eighth of a circle, and then use symmetry to get the rest of the points
Assume that we have just plotted point (xk, yk)The next point is a choice between (xk+1, yk) and (xk+1, yk-1)We would like to choose the point that is nearest to the actual circleSo how do we make this choice?
April 21, 2023 11
April 21, 2023 12
Note: When x-axis increased by one unit, then the y-axis either increased by one or not
Assuming we have just plotted the pixel at (xk,yk) so we need to choose between (xk+1,yk) ( A), or (xk+1,yk-1) (B)... By comparing distance between points and the real circle line.
dA = (xk + 1)2 + yk2 – r2
dB = (xk + 1)2 + (yk -1)2 – r2
Now check the point with smallest distance, by checking the sum of (dA, and dB)
S = dA + dB
What are the probabilities of S value?
April 21, 2023 13
1. Line between A and B
dA > 0 and dB < 0
If abs (dA) > abs (dB) S >0 choose B
If abs (dA) <= abs (dB) S<= 0 choose A
2. If the circle line pass through or up of A
dA <= 0 and dB < 0
S < 0 choose A
3. If the circle line pass through or under the point B
dA > 0 and dB >= 0
S > 0 choose B
April 21, 2023 14
RULE
If S >0 choose pixel B
Else choose A
April 21, 2023 15
Ellipses
x = a . cos (ө) OR x = xc + a . cos (ө)
y = b . sin(ө) OR y = yc + b . sin(ө)
April 21, 2023 16
To draw ellipse:
1.The ellipse has four points symmetry2.Increment angle with very small value (dө)3.Find (x2, y2) from point (x1, y1)4.Find (x2, y2) according to the following equations
x2 = x1 . cos (dө) – (a/b) . y1 . sin (dө)
y2 = y1 . cos (dө) + (b/a) . x1 . sin (dө)
The start point will be (0, a)
Ending of loop when x = b
April 21, 2023 17
Home work / draw the following figure
April 21, 2023 18
Home work / draw the following figure
April 21, 2023 19