Computer Graphics and Animation - 03 Drawing Circles
Computer Graphics and Animation - 03 Drawing Circles
ANIMATION
03 Drawing Circles
Drawing A Circle
• ProcedureCircle(xc: integer, yc: integer,
r: integer)
Definitions
x, y : integer
Algorithm
x traversal [0..R]
y = round(sqrt(R*R – x*x))
Set(xc+x,yc+y)
• 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)
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
•
• 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:
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)
• 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