COMPUTER GRAPHICS AND
ANIMATION
03 Drawing Circles
Drawing A Circle
• ProcedureCircle(xc: integer, yc: integer,
r: integer)
Draws a circle with the center at and radius .
Drawing A Circle
The ultimate noob approach:
Definitions
x, y : integer
Algorithm
x traversal [0..R]
y = round(sqrt(R*R – x*x))
Set(xc+x,yc+y)
Does it work well?
Drawing A Circle
• Consider
a circle with the center at .
• 8-way symmetry drawing in one octant
is enough.
Drawing A Circle
Another noob approach:
Definitions
, x, y : integer
Algorithm
for 0 to 45
x = round(cos() * R)
y = round(sin() * R)
Set(xc+x,yc+y)
Does it work well?
Drawing a Circle
• Find another way to traverse from to .
•
• Notice that
at ,
• We could change the algorithm:
Definitions
x, y : integer
Algorithm
x 0
While x ≤ y
y = f(x) {f(x) = round(sqrt(R*R – x*x))}
Set(xc+x,yc+y)
x x + 1
Does it work well?
Midpoint Circle Algorithm
•
• For any given point
is on the circle
is inside the circle
is outside the circle
Midpoint Circle Algorithm
Rough sketch:
x 0; y R
d some value {initial d}
Iterate
Set(xc+x,yc+y)
Stop x ≥ y
If d > 0 then {choose DR}
y y – 1; d d + dDR
Else {choose R}
d d + dR
x x + 1
Midpoint Circle Algorithm
•Evaluate
• choose
• choose
Midpoint Circle Algorithm
•Determine
the next midpoint for .
• is choose
• is choose
Midpoint Circle Algorithm
•• If
is
Midpoint Circle Algorithm
•• If
is
Midpoint Circle Algorithm
• Start
position
• Next midpoint
Midpoint Circle Algorithm
• To
avoid using decimal numbers, we need
to “normalize” .
• Thus, we have:
Midpoint Circle Algorithm
Dictionary
x, y, d : integer
Algorithm
x 0; y r; d 5 – 4 * r
Iterate
Set(xc + x, yc + y)
Stop x ≥ y
If d < 0 then {choose R}
d d + 8 * x + 12
Else {d ≥ 0, choose DR}
d d + 8 * (x – y) + 20
y y – 1
x x + 1
Midpoint Circle Algorithm
• Let’s get back to the previous equation:
• If we subtract the previous equation by ,
we get
where
Midpoint Circle Algorithm
• We get a new decision variable .
choose
choose
• However, recall that , , are integers, so
(and all subsequent ) will also be integers.
We can simplify thus:
choose
choose
Midpoint Circle Algorithm
• We
do not need to “normalize” , , and .
Thus we have:
Midpoint Circle Algorithm
Dictionary
x, y, d : integer
Algorithm
x 0; y r; d 1 - r
Iterate
Set(xc + x, yc + y)
Stop x ≥ y
If d < 0 then {choose R}
d d + 2 * x + 3
Else {d ≥ 0, choose DR}
d d + 2 * (x – y) + 5
y y – 1
x x + 1
Second Order Difference
• We
can improve the performance of the
algorithm by using the incremental
computation technique more extensively.
• Instead of recalculating and in every
iteration, we can simply increment and by
some value.
Second Order Difference
• If
we choose in the current iteration, the
point of evaluation moves from to .
•
•
Second Order Difference
• If
we choose in the current iteration, the
point of evaluation moves from to .
•
•
Second Order Difference
•
Start position
• Initial
• Initial
• Initial
Second Order Difference
x, y, d, dR, dDR : integer
Algorithm
x 0; y r;
d 1 - r; dR 3; dDR -2 * R + 5
Iterate
Set(xc + x, yc + y)
Stop x ≥ y
If d < 0 then {choose R}
d d + dR
dR dR + 2; dDR dDR + 2
Else {d ≥ 0, choose DR}
d d + dDR
dR dR + 2; dDR dDR + 4
y y – 1
x x + 1
Drawing An Ellipse
yc : integer,
• Procedure DrawEllipse(xc : integer,
a : integer, b : integer)
Draws an ellipse with the center at , horizontal radius
of , and vertical radius of .
Drawing An Ellipse
• An ellipse is defined as the following:
• Unlike a circle, you cannot divide an
ellipse into 8 octants. You have to divide
an ellipse into 4 quadrants.
Midpoint Ellipse Algorithm
• Using
the midpoint algorithm, we start
from and stop at .
• From to a certain point , we either go or
per pixel. From to , we either go or per
pixel.
Midpoint Ellipse Algorithm
•• is a point where the curve has a gradient of .
• Grad
• Gradient =
the and components are of equal magnitude
stop when at the next midpoint
At the beginning ,
change region when
Midpoint Ellipse Algorithm
•
• For any point
is on the ellipse
is inside the ellipse
is outside the ellipse
Midpoint Ellipse Algorithm
Rough sketch:
x 0; y b
d some value {initial d for region 1}
{Region 1}
Iterate
Set(xc+x,yc+y)
Stop b2(x + 1) ≥ a2(y – 0.5)
If d > 0 then {choose DR}
y y – 1; d d + dDR
Else {choose R}
d d + dR
x x + 1
{Region 2}
d some value {initial d for region 2}
Iterate
Set(xc+x,yc+y)
Stop y = 0
If d < 0 then {choose DR}
x x + 1; d d + dDR
Else {choose D}
d d + dD
y y - 1
Midpoint Ellipse Algorithm
•Region
1
• Evaluate
• choose
• choose
Midpoint Ellipse Algorithm
• We
need to determine the next midpoint
for .
• is choose
• is choose
Midpoint Ellipse Algorithm
•• If
is
Midpoint Ellipse Algorithm
•• If
is
Midpoint Ellipse Algorithm
• Start
position
• Next midpoint =
Midpoint Ellipse Algorithm
• To
avoid using real numbers, needs to be
"normalized"
• Thus, we have:
Midpoint Ellipse Algorithm
• Rough sketch for Region 1:
x 0; y b
d 4b2 – 4a2b + a2
Iterate
Set(xc+x,yc+y)
Stop 2b2(x + 1) ≥ a2(2y – 1)
If d > 0 then {choose DR}
y y – 1
d d + b2(8x+12)+a2(8-8y)
Else {choose R}
d d + b2(8x+12)
x x + 1
Midpoint Ellipse Algorithm
•Region
2
• Evaluate
• choose
• choose
Midpoint Ellipse Algorithm
• We
need to determine the next midpoint
for .
• is choose
• is choose
Midpoint Ellipse Algorithm
•• If
is
Midpoint Ellipse Algorithm
•• If
is
Midpoint Ellipse Algorithm
• From
(boundary between region 1 and
region 2), we can get for the next
midpoint:
= coordinate of
= last coordinate in region 1
Midpoint Ellipse Algorithm
• To
avoid using real numbers, needs to be
"normalized"
• Thus, we have:
Midpoint Ellipse Algorithm
• Rough sketch for region 2
d b2(2x+1)2 + 4a2(y-1)2 – 4a2b2
While y>0 do
If d < 0 then {choose DR}
x x + 1
d d + b2(8x+8)+a2(12-8y)
Else {choose D}
d d + a2(12-8y)
y y – 1
Set(xc+x,yc+y)
Programming Tips
•• You
might like to assign expressions that occur
often into variables for faster computation.
• Example: in the previous slide, the expressions
and occur often in calculations. It would be better
to assign those values into variables:
a_square, b_square : integer
a_square a * a
b_square b * b
• Other expressions that might be better declared as
variables: , , , , ,
Programming Tips
• It’s faster to compute simple exponentials
(such as squares and cubes) using the
multiplication operator than using the
exponent operator.
y x * x FASTER
y x ^ 2 SLOWER
Review Questions
• Explain why the “ultimate noob approach”
on slide 3 does not work.
• Explain why the “another noob approach”
on slide 5 does not work.
• Explain why we can do the midpoint
algorithm for drawing a circle just by
traversing one octant of a circle.
Review Questions
• Try
deriving the circle midpoint algorithm
yourself. If you can derive the following,
you should be able to do it.
– First pixel to Set
– Stop condition
– Initial (decision variable), , and
• In the midpoint circle algorithm, why can
we simplify the initial into ?
Review Questions
• Try
deriving the midpoint circle algorithm
using second order differences.
• Use the circle midpoint algorithm to draw a
circle centered at with a radius of . Plot the
pixels manually; do not use a computer.
• Explain why we can’t do the midpoint
algorithm for drawing an ellipse by just
traversing one octant of an ellipse. Why do
we have to traverse one quarter of an ellipse?
Review Questions
• Try
deriving the midpoint algorithm for
drawing a ellipse yourself. If you can derive
the following, you should be able to do it.
– First phase
• First pixel to Set
• Stop condition
• Initial (decision variable), , and
– Second phase
• Stop condition
• Initial , , and
Exercise
• Use
the midpoint circle algorithm to draw a
circle centered at with a radius of . Draw
the Set points on a grid.
Exercise
• Modify the midpoint circle algorithm to
draw a circle with a thickness of N pixels.
• Modify the midpoint circle algorithm to
draw a dotted circle.
• Modify the midpoint circle algorithm to
draw an arc (add two other parameters: a
start angle and an end angle).
Exercise
• The midpoint circle algorithms in this slide are
not optimal. For example, at the first iteration
when a full circle is drawn, the commands
Set(xc + x, xc + y) and Set (xc – x,
xc + y) causes the pixel at (xc, xy + r) to be
Set twice. Optimize the midpoint circle
algorithm so each pixel is only Set once.
• Do the same for the midpoint ellipse algorithm.
Exercise
• Try doing the second order difference on
the midpoint ellipse algorithm. This can
improve the performance of the algorithm
A LOT.
Exercise
Write the code to draw a parabola with the following equation: y = x 2/100 on
a screen with the bottom left corner at (0,0) and the top right corner at
(100,100). Use the midpoint algorithm.
• Start at (0,0).
• Find the evaluation function.
• Just like drawing an ellipse, there are two phases. At the first phase,
you traverse along the x-axis; at the second phase, you traverse along
the y-axis. Determine when to switch from first phase to second phase.
• At the first phase, you check whether the next point is R or UR. Find R
and UR for the first phase. Also find the initial d for the first phase.
• At the second phase, you check whether the next point is U or UR. Find
U and UR for the first phase. Also find the initial d for the second
phase.
• Stop when you reach the top-right edge (x=100 or y=100)
• Write the algorithm.
• You can optimize this using second order difference, but you don’t have
to.
Exercise
• Write the code for the procedure DrawMunchedBox
which draws a box that is “munched” at all corners as
specified below.
Procedure DrawMunchedBox(In x1 : integer, In y1 : integer,
In x2 : integer, In y2 : integer, In R : integer)
{Draws a “munched” box with the bottom left corner at
(x1,y1)and the top right corner at (x2,y2) with quarter-
circles(radius R) at each corner}
{I.S. : x1 < x2; y1 < y2; the width and height of the box
is much larger than R, so it is possible to draw the
corners as quarter-circles}
{F.S. : the rounded box is drawn}
Exercise
• The
only primitive that you may use is
Set(x,y) to draw a point at . You have to
draw the lines and (quarter) circles by
using Set.
• Below is an example of a “munched” box.