1 cs 430/536 computer graphics i circle drawing and clipping week 3, lecture 6 david breen, william...
TRANSCRIPT
1
CS 430/536Computer Graphics I
Circle Drawing and ClippingWeek 3, Lecture 6
David Breen, William Regli and Maxim Peysakhov
Geometric and Intelligent Computing Laboratory
Department of Computer Science
Drexel Universityhttp://gicl.cs.drexel.edu
2
Outline
• Scan conversion of circles• Clipping circles• Scan conversion of ellipses
3
Scan Conversion of Circles
• Generalization of the line algorithm
• Assumptions:– circle at (0,0)– Fill 1/8 of the circle,
then use symmetry
Using the 8-way symmetry of a circle:
1994 Foley/VanDam/Finer/Huges/Phillips ICG
Not using the 8-way symmetry of a circle
4
Scan Conversion of Circles
• Implicit representation of the circle function:
• Note: < 0 for points inside the circle, and > 0 for points outside the circle
5
Scan Conversion of Circles
• Assume we finished pixel• What pixel to draw next?
(going clockwise)• Note: the slope of the circular
arc is between 0 and –1– Hence, choice is between:
E and SE
• Idea: If the circle passes above the midpoint M, then we go to E next, otherwise we go to SE
1994 Foley/VanDam/Finer/Huges/Phillips ICG
6
Scan Conversion of Circles
• We need a decision variable D:
• If then M is below the arc, hence the E pixel is closer to the line.
• If then M is above the arc, hence the SE pixel is closer to the line.
1994 Foley/VanDam/Finer/Huges/Phillips ICG
7
Case I: When E is next• What increment for computing a new D?• Next midpoint is:
• Hence, increment by:Pics/Math courtesy of Dave Mount @ UMD-CP
8
Case II: When SE is next• What increment for computing a new D?• Next midpoint is:
• Hence, increment by:Pics/Math courtesy of Dave Mount @ UMD-CP
9
Scan Conversion of Circles
• How to compute the initial value of D:
• We start with x = 0 and y = R, so the first midpoint is at x = 1, y = R-1/2:
10
Scan Conversion of Circles
• Converting this to an integer algorithm:– Need only know if D is positive or negative– D & R are integers– Note D is incremented by an integer value– Therefore D + 1/4 is positive only when D
is positive; it is safe to drop the 1/4
• Hence: set the initial D to 1 – R (subtracting 1/4)
11
Circle Scan Conversion Algorithm
• Given radius R and center (0, 0)– First point (0, R)
• Initial decision parameter D = 1- R• While x y
– If (D < 0)• x++; D += 2x + 3;
– else• x++; y--; D += 2(x - y) + 5
– WritePoints(x,y)
12
WritePoints(x,y)
• Writes pixels to the seven other octants
13
Clipping Circles
• Accept/Reject test– Does bounding box of
the circle intersect with clipping box?
• If yes, condition pixel write on clipping box inside/outside test
14
Properties of Ellipses
• “Elongated circle”• For all points on
ellipse, sum of distances to foci is constant
d1+ d2 = const
• If F1=(x1, y1) and F2=(x2, y2) then
y
x
d1
d2
P=(x,y)F1
F2
€
(x − x1)2 + (y − y1)
2 + (x − x2)2 + (y − y2)2 = const
15
Properties of Ellipses
• Equation simplified if ellipse axis parallel to coordinate axis
• Parametric form
y
x
ry
rx
xc
yc
€
x − xc
rx
⎛
⎝ ⎜
⎞
⎠ ⎟
2
+y − yc
ry
⎛
⎝ ⎜ ⎜
⎞
⎠ ⎟ ⎟
2
=1
€
x = xc + rx cosθ
y = yc + ry sinθ
16
Symmetry Considerations
• 4-way symmetry• Unit steps in x until reach
region boundary• Switch to unit steps in y
(x,y)
ry
rx
(x,-y)
(-x,y)
(-x,-y)
Region1
Region 2
Slope = -1
• Step in x while
• Switch to steps in y when
€
f (x,y) = ry2x 2 + rx
2y 2 − rx2ry
2
dy
dx= −
ry2x
rx2y
dy
dx= −1
ry2x = rx
2y
€
ry2x < rx
2y
€
ry2x ≥ rx
2y
17
Midpoint Algorithm (initializing)
• Similar to circles• The initial value for region 1
• The initial value for region 2
• We have initial values, now we need the increments
1994 Foley/VanDam/Finer/Huges/Phillips ICG
(x,y)
ryRegion
1Region 2
Slope = -1
€
Dinit1 = f (1,ry − 12)
= ry2 − rx
2ry + 14 rx
2
€
Dinit 2 = f (x p + 12 , y p −1)
= ry2(x p + 1
2)2 + rx2(y p −1)2 − rx
2ry2
18
Making a Decision
• Computing the decision variable
• If then M is below the arc, hence the E pixel is closer to the line.
• If then M is above the arc, hence the SE pixel is closer to the line.
1994 Foley/VanDam/Finer/Huges/Phillips ICG
€
D = f (x p +1,y p − 12)
= ry2(x p +1)2 + rx
2(y p − 12)2 − rx
2ry2
19
Computing the Increment
1994 Foley/VanDam/Finer/Huges/Phillips ICG
€
Dnew = f (x p + 2, y p − 12)
= ry2(x p + 2)2 + rx
2(y p − 12)2 − rx
2ry2
= Dold + ry2(2x p + 3)
Dnew = f (x p + 2, y p − 32)
= ry2(x p + 2)2 + rx
2(y p − 32)2 − rx
2ry2
= Dold + ry2(2x p + 3) + rx
2(−2y p + 2)
increment =ry
2(2x p + 3) Dold < 0
ry2(2x p + 3) + rx
2(−2y p + 2) Dold ≥ 0
⎧ ⎨ ⎩
E case
SE case
21
Computing the Increment in 2nd Region
• Decision variable in 2nd region
• If then M is left of the arc, hence the SE pixel is closer to the line.
• If then M is right of the arc, hence the S pixel is closer to the line.
1994 Foley/VanDam/Finer/Huges/Phillips ICG
€
D = f (x p + 12 ,y p −1)
= ry2(x p + 1
2)2 + rx2(y p −1)2 − rx
2ry2
22
Computing the Increment in 2nd Region
1994 Foley/VanDam/Finer/Huges/Phillips ICG
€
Dnew = f (x p + 32 ,y p − 2)
= ry2(x p + 3
2)2 + rx2(y p − 2)2 − rx
2ry2
= Dold + rx2(−2y p + 3) + ry
2(2x p + 2)
Dnew = f (x p + 12 , y p − 2)
= ry2(x p + 1
2)2 + rx2(y p − 2)2 − rx
2ry2
= Dold + rx2(−2y p + 3)
increment =rx
2(−2y p + 3) + ry2(2x p + 2) Dold < 0
rx2(−2y p + 3) Dold ≥ 0
⎧ ⎨ ⎩
SE case
S case
23
Midpoint Algorithm for Ellipses
Set first point to (0,ry)
Set the Decision variable to
Loop (x = x+1)
If D < 0 then pick E and
If D ≥ 0 then pick SE and
Until
2212ykyDDrxr+=++
2221122ykyxkDDrxrry++=++−
22222211222,222ykykyxkxkxrxrxrryryr++=+=−
2222ykxkrxry≥
Set first point to the last computedSet the Decision variable to
Loop (y = y - 1)If D < 0 then pick SE and
If D ≥ 0 then pick S and
Until y < 0Use symmetry to complete the
ellipse
Region 1 Region 2
€
Dinit1 = ry2 − rx
2ry + 14 rx
2
€
D + = ry2(2x p + 3)
€
D + = ry2(2x p + 3) + rx
2(−2y p + 2)
€
2ry2xk ≥ 2rx
2yk
€
Dinit 2 = ry2(x p + 1
2)2 + rx2(y p −1)2 − rx
2ry
€
D + = ry2(2x p + 2) + rx
2(−2y p + 3)
€
D + = rx2(−2y p + 3)