cs 535 computer graphicsryang/teaching/cs535-2012spr/lectures/...cs 535 computer graphics 2d...
TRANSCRIPT
CS 535
Computer Graphics
2D Drawings: Lines, Circle, and Spline
Digital Concepts of Drawing in Raster Arrays
PIXEL is a single array element at (x,y)
- No smaller drawing unit exists
0 1 2
012
P(x,y)
y
Primitive Drawing Operations
From (x1,y1) to (x2,y2)
Equation:
y = mx + b
m = (y2-y1)/(x2-x1)
b = y1 – m*x1
x1 x2
y1
y2
compute -- m, b
for x=x1 to x2, step 1
y = m*x + b
pixel( x, round(y), color)
loop
Drawing Lines
(Digital Differential Analyzer)
[DDA avoids the multiple by slope m]
Equation:
xk+1 = xk + 1
yk+1 = yk + m
if slope |m| > 1
yk+1 = yk + 1
xk+1 = xk + 1/m
compute m (assume |m| <1 )
y = y1, x = x1
pixel(round(x1), round(y1), color)
for x=x1 to x2, step 1
x = x + 1
y = y + m
pixel(x, round(y), color)
End
xk xk+1 xk+2 xk+3
yk
yk+1
yk+2
m>1
M<=1
DDA Algorithm
(Digital Differential Analyzer)
[avoid the float multiple by slope m]
slope m, y – floats
Problems:
1. Necessary to perform float addition
2. Necessary to have float representation
3. Necessary to perform -- round()
Float representations/operations are more expensive than
integer operations. We would like to avoid them if possible.
DDA Algorithm
Uses only integer math to draw a line
Lets assume slope-m is positive and less than 1
we know that: xk+1 = xk + 1
at xk+1 we need to make a decision for next pixel
draw either at:
choice 1 -- (xk + 1, yk )
choice 2 -- (xk + 1, yk + 1)
How do we decide between choice 1 or 2? xk xk+1 xk+2 xk+3
yk
yk+1
yk+2
?
Mid-Point Line Algorithm (Bresenham)
Consider the distance with the actual line (y=mx +b) at the two choices:
d1 = y – yk
= m(xk+1) + b – yk
d2 = (yk+1) – y
= yk + 1 - m(xk+1) -b
xk xk+1
yk
yk+1 d2
d1
Difference between the these two separations is:
d = d1 – d2
if d > 0
move to (yk +1)
else stay at yk
y=mx+b
Mid-Point Line Algorithm (Bresenham)
Still have a “float” operation in calculation of “d”
Lets create a new decision operator by multiplying by x (recall m=y/x )
pk = x (d1 - d2) ~ note pk sign is the same as (d1 - d2)
= 2 y xk - 2 x yk + 2 y + x (2b– 1) [Eq 1]
= 2 y xk - 2 x yk + C
We can use pk as a decision operator – instead of (d1 - d2)
Mid-Point Line Algorithm (Bresenham)
pk+1 = 2 y xk+1 - 2 x yk+1 + C
What is the change from pk to pk +1 ?
pk +1 - pk = 2 y (xk+1 - xk) - 2 x ( yk+1 - yk)
pk +1 = pk + 2 y ( 1 ) - 2 x ( yk+1 - yk) either 1 or 0
so –
if pk < 0 // yk+1 = yk
pk+1 = pk + 2 y
else // yk+1 = yk + 1
pk+1 = pk + 2 y - 2 x
Use [Eq. 1] with x1 and y1 to find:
po = 2 y - x
Mid-Point Line Algorithm (Bresenham)
Compute constants: x, y, 2y, 2y-2x
Calculate: po = 2y - x
plot(x1,y1)
for x=x1 to x2 // let y=y1
if (p < 0)
p = p + 2y
else
y++
p = p + 2y - 2x
end
plot(x,y)
loop
Note -- main loop:
Only integer math.
No float representation,
or operations needed.
Constants: 2dy, 2dx
are integers.
Mid-Point Line Algorithm (Bresenham)
Example:
(20,10) to (30,18)
x = 10, y = 8
(slope 0.8)
p0 = 2y - x = 6
2y = 16 [E]
2y - 2x = -4 [NE]
k Pk (xk+1 ,yk+1 ) k pk (xk+1 ,yk+1 )
0 6 (21,11) 5 6 (26,15)
1 2 (22,12) 6 2 (27,16)
2 -2 (23,12) 7 -2 (28,16)
3 14 (24,13) 8 14 (29,17)
4 10 (25,14) 9 10 (30,18)
full algorithm -- page 90-91 Hearn
adjusts for slope m>1
re-orders x1,x2,y1,y2 as necessary
http://www.cosc.canterbury.ac.nz/people/mukundan/cogr/LineMP.html
Mid-Point Line Algorithm (Bresenham)
Addressing the Framebuffer
1 2 3 . . . N
Xmax
Logical representation
Physical representation in memory -- 1D Array
(0,0)
(x,y)
(Xmax , Ymax )
1D memory as a 2D Framebuffer
Indexing 1D memory
Coordinates (x,y) [or (i,j)] (assume array is unsigned char)
1 byte pixel (grayscale)
frameBuffer[ y * Xmax + x ] = grayscale or index color
4 byte pixel (RGBA) frameBuffer[ (y*Xmax+x)*4 + 0 ] = R
frameBuffer[ (y*Xmax+x)*4 + 1 ] = G
frameBuffer[ (y*Xmax+x)*4 + 2 ] = B
frameBuffer[ (y*Xmax+x)*4 + 3 ] = A
CIRCLE
2D-Drawings
Representing A Circle
Explicit representation:
Implicit representation:
Parametric representation:
Equation for a circle:
Given: center = (xc, yc) and radius r
(x – xc)2 + (y – yc)
2 = r2
y = yc ± r2 – (xc– x)
2
Not an ideal approach:
Non-uniform spacing during evaluation
causes noticeable artifacts
Also, we have to evaluate ^2, and sqrt()
Figure 3-13
xc
yc
r
Drawing Circles
Parametric Polar Representation for circle:
x = xc + r cos
y = yc + r sin
Allows a uniform spacing via the variable
step size is often set to be 1/r.
We can render adjacent points
via lines (to avoid gaps)
xc
yc
step
step
Drawing Circles
Only need to compute
1 octant
We can reduce the number of calculation:
Exploit Symmetry of Circle
Need only to compute (0,45) or (90,45)
Drawing Circles
The Circle Midpoint Algorithm
),( pp yx
Key idea: at each
step, decide which
pixel (E or SE) is
closest to the circle.
Choose that pixel
and draw it.
Goals:
Implicit formulation
Finite differences (incremental)
Integer arithmetic
M
SE
E
The Implicit Circle Equation
If M lies inside the circle, draw pixel E
If M lies outside the circle, draw pixel SE
),( pp yx
M
SE
E
Implicit Formulation
0),( 222 ryxyxF222 )( ryx Notice that
22
222
222 )(
xry
xry
ryx
2),( ryxF means
0),( yxF
0),( yxF
points outside circle
points inside circle
The Midpoint Test
),1(21 pp yxM
The sign of F(M) gives the
answer as to which pixel,
E or SE, to draw
),( pp yx
M
SE
E
Formulating an Algorithm
Let d be a decision variable which makes the midpoint test.
Then the test to decide which pixel to draw is just
if (d < 0)
then
// the midpoint is inside the circle
// the circle passes closer to the upper pixel
draw the E pixel
else
// the midpoint is outside the circle
// the circle passes closer to the lower pixel
draw the SE pixel
Formulating an Algorithm
MidpointCircle (radius, x_center, y_center)
x = 0; y = radius;
CirclePoints (x_center, y_center, x, y);
while (y > x) {
if (d < 0) // the midpoint is inside the circle
// the circle is closer to E pixel
x = x + 1;
CirclePoints (x_center, y_center, x, y);
else // the midpoint is outside the circle
// the circle is closer to SE pixel
x = x + 1; y = y - 1;
CirclePoints (x_center, y_center, x, y);
end
Formulating an Algorithm
CirclePoints (a, b, x, y)
DrawPoint (a + x, b + y);
DrawPoint (a + x, b - y);
DrawPoint (a - x, b + y);
DrawPoint (a - x, b - y);
DrawPoint (a + y, b + x);
DrawPoint (a + y, b - x);
DrawPoint (a - y, b + x);
DrawPoint (a - y, b - x);
end
Using Polar Coordinates
x = xc + rx cos
y = yc + ry sin
Implicit
f(x,y)ellipse = rx2 (x – xc)
2 + ry2 (y - yc)
2 - rx2ry
2
Exploit Symmetry
xc
ry
rx yc
(-x,y) (x,y)
(-x,-y) (x,-y)
Drawing Ellipses
Bresenham approach is typically used for low-level
scan conversion of Lines, Circles & Ellipses.
In principle, any algebraic curve can be expressed
in the Bresenham style.
1. Measure error to actual curve (make decision)
2. Find integer form of error
3. Find incremental update of error criteria
In practice, curve beyond conics are drawn as
a series of short Bresenham straight lines.
Other Algebraic Surfaces
SPLINES
2D-Drawings
2D Freeform Curves or Splines
1
2
3
4
5
6 7
A typical free form curve designed to
pass through or near a sequence of
points.
Splines are parameterized curves that
generalize linear interpolation of straight
lines to higher powers of interpolation
parameter.
Splines
),,,( 1111
zyxx
),,,( 2222
zyxx
21
121
)1(
)()(
txxt
xxtxtx
21 )1( and )0( xxxx
start :
end :
where :
Jagged Lines :
Linear interpolation (1st order Spline)
x 1
x 2
x 3
x 4
x 6
x 7 t=0
t=1 x 5
G0: geometric continuity: the endpoints coincide
22
2
)1()1(
)(
CttBttA
ctbtatx
but 2nd derivative is choppy
matches derivatives
Quadratic interpolation
G1: tangents have the same slope
32
3223 )1()1()1()(
ttt
tDttCttBtAtx
Cubic Spline & Interpolation
C1: first derivative on both curves match at join point
The "speed" is the same before and after
Examples:
• Natural Cubic Spline
• Hermit
• Beizer
• B-spline
Representing Curves
Cubic curve ( to maintain C1 continuity)
Matrix Representation
y
y
y
y
x
x
x
x
d
c
b
a
d
c
b
a
tttyx ]1[][ 23
yyyy
xxxx
dtctbtay
dtctbtax
23
23
Solving for Coefficients
Continuity – C0, C1, etc.
The Gradient of a Cubic Curve
y
y
y
y
x
x
x
x
dt
dy
dtdx
d
c
b
a
d
c
b
a
tt ]0123[][ 12
yyyy
xxxx
dtctbtay
dtctbtax
23
23
023
023
2
2
yyydt
dy
xxxdtdx
ctbta
ctbta
Matrix notion:
The Hermite Specification as a Matrix
Equation
y
y
y
y
x
x
x
x
dt
dy
dt
dy
dt
dx
dt
dx
d
c
b
a
d
c
b
a
y
y
x
x
0123
0100
1111
1000
2
1
2
1
2
1
2
1
Solving the Hermite Coefficients
y
y
y
y
x
x
x
x
dt
dy
dt
dy
dt
dx
dt
dx
d
c
b
a
d
c
b
a
y
y
x
x
2
1
2
1
2
1
2
1
0001
0100
1233
1122
MHermite GHermite
Cubic Hermite Spline Equation:
HermiteHermite
23 ]1ttt[][ GMyx
Another Way to Think About Splines
Cubic Hermite Spline Equation
After reordering multiplications
HermiteHermite
23 ]1ttt[][ GMyx
24132211
2
1
2
1
23
23
23
23
)()()()(
2
32
132
][
ptfptfptfptf
p
p
p
p
tt
ttt
tt
tt
yx
T
Hermite Blending Functions
Beizer Spline
p1
p2
p3
p4
Anchor Point
Anchor Point Control Point
Control Point
)(3
)(3
432
121
ppp
ppp
Converting from Beizer to Hermite
Since
Substituting gives:
4
3
2
1
4
3
2
1
2
1
2
1
3300
0033
1000
0001
2
1
2
1
y
y
y
y
x
x
x
x
y
y
x
x
dt
dy
dt
dy
dt
dx
dt
dx
4
3
2
1
4
3
2
1
Hermite
y
y
y
y
x
x
x
x
TM
d
c
b
a
d
c
b
a
y
y
y
y
x
x
x
x
T
0001
0033
0363
1331
MBeizer
Blending Functions for Beizer Splines
0 t
1 B1 B4
B2 B3
3
4
2
3
2
2
3
1
3)1(
3)1(
)1(
tB
ttB
ttB
tB
Bezier Curve Properties
Blending functions always
•Sum to 1 (partition of unity)
•Are non-negative
•Satisfy 14321 BBBB
Curve always
•Passes inside the convex hull of the control points
•Goes through the two endpoints
•Is tangent to the control polygon at the endpoints
Convex Hull Property
Drawing Spline Curves
for t between 0 and 1 gives all the points on the curve:
0 1/6 1/3 1/2 2/3 5/6 1
xxxx
xxxx
PPPP
PPPPx
481
383
283
187
4
3
21
3212
21
2
2
21
21
1
3
21
21 )1(3)1(3)1()(
)](),([ tytx
Displaying Cubic Curves
•Divide interval [0, 1] into n even pieces
•Evaluate the curve at each value of t within the interval
•Draw line segments connecting the points
What is the cost of displaying curves this way?
An incremental approach possible too.
xxxx PtPttPttPttx 4
3
3
2
2
2
1
3 )1(3)1(3)1()(
Summary
Continuity of free-form curves (G0, G1, C0, C1,
C2…)
Cubic Curves (Splines)
Hermite
Matrix representation
Blending
Beizer