CS 535
Computer Graphics
2D Drawings: Lines, Circle, and Spline
Primitive Drawing Operations
Digital Concepts of Drawing in Raster Arrays
PIXEL is a single array element at (x,y)
- No smaller drawing unit exists
P(x,y)
y
2
1
0
0 1 2
Drawing Lines
From (x1,y1) to (x2,y2)
y2
y1
Equation:
y = mx + b
x1
x2
m = (y2-y1)/(x2-x1)
b = y1 – m*x1
compute -- m, b
for x=x1 to x2, step 1
y = m*x + b
pixel( x, round(y), color)
loop
compute m (assume |m| <1 )
DDA Algorithm
(Digital Differential Analyzer)
[DDA avoids the multiple by slope m]
Equation:
xk+1 = xk + 1
yk+1 = yk + m
y = y1, x = x1
pixel(round(x1), round(y1), col
for x=x1 to x2, step 1
x = x + 1
y = y + m
pixel(x, round(y), color)
End
m>1
yk+2
if slope |m| > 1
yk+1 = yk + 1
xk+1 = xk + 1/m
yk+1
yk
M<=1
xk
xk+1 xk+2 xk+3
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.
Mid-Point Line Algorithm (Bresenham)
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)
yk+2
yk+1
?
yk
xk
xk+1 xk+2 xk+3
How do we decide between choice 1 or 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
y=mx+b
yk+1
yk
Difference between the these two separations is: xk
d = d1 – d2
if d > 0
move to (yk +1)
else stay at yk
d2
d1
xk+1
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
pk+1 = pk + 2 y
// yk+1 = yk
else
// yk+1 = yk + 1
pk+1 = pk + 2 y - 2 x
Use [Eq. 1] with x1 and y1 to find:
p o = 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 + 2 y
else
y++
p = p + 2 y - 2 x
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)
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
(23,12)
7
x = 10, y = 8
-2
-2
(28,16)
3
14
(24,13)
8
14
(29,17)
(slope 0.8)
4
10
(25,14)
9
10
(30,18)
Example:
(20,10) to (30,18)
p0 = 2y - x = 6
2y = 16 [E]
2y - 2x = -4 [NE]
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
Addressing the Framebuffer
Logical representation
(Xmax , Ymax )
(x,y)
(0,0)
Physical representation in memory -- 1D Array
1
2
Xmax
3
...
N
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
frameBuffer[ (y*Xmax+x)*4
frameBuffer[ (y*Xmax+x)*4
frameBuffer[ (y*Xmax+x)*4
+0]=R
+1]=G
+2]=B
+3]=A
2D-Drawings
CIRCLE
Representing A Circle
Explicit representation:
Implicit representation:
Parametric representation:
Drawing Circles
Equation for a circle:
Given: center = (xc, yc) and radius r
(x – xc
)2
y = yc ±
+ (y – yc
)2
=
r2
r2 – (xc– x)2
r
yc
xc
Not an ideal approach:
Non-uniform spacing during evaluation
causes noticeable artifacts
Also, we have to evaluate ^2, and sqrt()
Figure 3-13
Drawing Circles
Parametric Polar Representation for circle:
x = xc + r cos
y = yc + r sin
step
yc
Allows a uniform spacing via the variable
xc
step size is often set to be 1/r.
We can render adjacent points
via lines (to avoid gaps)
step
Drawing Circles
We can reduce the number of calculation:
Exploit Symmetry of Circle
Need only to compute (0,45)
or (90,45)
Only need to compute
1 octant
The Circle Midpoint Algorithm
(xp , yp )
E
M
SE
Goals:
Implicit formulation
Finite differences (incremental)
Integer arithmetic
Key idea: at each
step, decide which
pixel (E or SE) is
closest to the circle.
Choose that pixel
and draw it.
The Implicit Circle Equation
If M lies inside the circle, draw pixel E
If M lies outside the circle, draw pixel SE
(xp , yp )
E
M
SE
Implicit Formulation
F ( x, y) x y r 0
2
Notice that
2
2
( x 2 y 2 ) r 2 means F ( x, y) r 2
(x y ) r
2
2
y r x
2
2
2
2
y r 2 x2
F ( x, y) 0
points outside circle
F ( x, y) 0
points inside circle
The Midpoint Test
M ( x p 1, y p 12 )
(xp , yp )
E
M
SE
The sign of F(M) gives the
answer as to which pixel,
E or SE, to draw
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
DrawPoint
DrawPoint
DrawPoint
DrawPoint
DrawPoint
DrawPoint
DrawPoint
end
(a
(a
(a
(a
(a
(a
(a
(a
+
+
+
+
-
x,
x,
x,
x,
y,
y,
y,
y,
b
b
b
b
b
b
b
b
+
+
+
+
-
y);
y);
y);
y);
x);
x);
x);
x);
Drawing Ellipses
ry
yc
rx
Using Polar Coordinates
x = xc + rx cos
y = yc + ry sin
xc
Implicit
f(x,y)ellipse = rx2 (x – xc)2 + ry2 (y - yc)2 - rx2ry2
(-x,y)
(x,y)
Exploit Symmetry
(-x,-y)
(x,-y)
Other Algebraic Surfaces
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.
2D-Drawings
SPLINES
2D Freeform Curves or Splines
4
2
1
3
5
6
7
Splines
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.
Linear interpolation (1st order Spline)
start :
end :
x1 ( x1, y1, z1,)
x2 ( x2 , y2 , z2 ,)
x(t ) x1 t ( x2 x1)
(1 t ) x1 tx2
x(0) x1
where :
x(1) x2
and
x4
t=1
x2
Jagged Lines :
t=0
x1
x5
x7
x3
x6
G0: geometric continuity: the endpoints coincide
Quadratic interpolation
x(t ) a bt ct
2
A(1 t ) 2 Bt (1 t ) Ct 2
matches derivatives
but 2nd derivative is choppy
G1: tangents have the same slope
Cubic Spline & Interpolation
3
3
2
2
x (t ) A(1 t ) Bt (t 1) Ct (t 1) Dt
t t2 t3
C1: first derivative on both curves match at join point
Examples:
• Natural Cubic Spline
• Hermit
• Beizer
• B-spline
The "speed" is the same before and after
Representing Curves
Cubic curve ( to maintain C1 continuity)
x axt bxt cxt d x
3
2
y a yt byt c yt d y
3
Matrix Representation
[x
y ] [t 3 t 2
2
ax a y
b
b
x y
t 1]
cx c y
d x d y
Solving for Coefficients
Continuity – C0, C1, etc.
The Gradient of a Cubic Curve
2
3
a
t
2
xtc
x a t bxt b
d 0x
xt c
x
3 2
x x
dx
dt
2
3
a
t
2
y a t byt byt cyct yd0y
dy
dt
3 2
y y
Matrix notion:
[
dx
dt
dy
dt
] [3 t
2
2t
1
ax a y
b
bx y
1 0]
cx c y
d x d y
The Hermite Specification as a Matrix
Equation
x1 y1 0 0
x y
1
1
2
2
dx1 dy1
dt dt 0 0
dx2 dy2
dt dt 3 2
0 1 ax a y
b
1 1 bx y
1 0 c x c y
1 0 d x d y
Solving the Hermite Coefficients
1 x1 y1 ax a y
2 2 1
3 3 2 1 x y b b
dx2 dy2 x y
0
0
1
0 dt1 dt1 cx c y
dx2 dy2
0
0
0 dt dt d x d y
1
MHermite
GHermite
Cubic Hermite Spline Equation:
[x
y] [ t
3
t
2
t 1]M Hermite GHermite
Another Way to Think About Splines
Cubic Hermite Spline Equation
[x
y] [ t 3
t2
t
1]M Hermite GHermite
After reordering multiplications
2t 3t 1
3
2
2t 3t
y] 3
t 2t 2 t
3 2
t t
T
p1
p
2
[x
p1
p2
f1 (t ) p1 f 2 (t ) p2 f 3 (t ) p1 f 4 (t ) p2
3
2
Hermite Blending Functions
Beizer Spline
p3
Control Point
p2
Control Point
Anchor Point
p4
Anchor Point
p1
p1 3( p2 p1 )
p2 3( p3 p4 )
Converting from Beizer to Hermite
T
Since
x1 y1 1
x y 0
2 2
dxdt1 dydt1 3
dx2 dy2
dt dt 0
0 0
0 0
3 0
0 3
Substituting gives:
ax a y
x1 y1
b
x y
b
x yM
2 2
T
Hermite
cx c y
x3 y3
d
d
x
y
x y
4 4
0 x1
1 x2
0 x3
3 x4
y1
y2
y3
y4
1 3 3
3 6 3
3 3
0
0
0
1
MBeizer
1
0
0
0
Blending Functions for Beizer Splines
B1
(1 t 3 )
2
B2
(1 t ) 3t
2
B3
(1 t )3t
B4
t 3
1
B1
B4
B2
0
B3
t
Bezier Curve Properties
Blending functions always
•Sum to 1 (partition of unity)
•Are non-negative
•Satisfy 1 B2 B3 B4 B1
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
[ x(t ), y(t )] for t between 0 and 1 gives all the points on the curve:
x( 12 ) (1 12 ) P1x 3 12 (1 12 ) 2 P2 x 3 12 (1 12 ) P3 x 12 P4 x
3
2
3
78 P1x 83 P2 x 83 P3 x 18 P4 x
0
1/6
1/3
1/2
2/3
5/6
1
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?
x(t ) (1 t 3 ) P1x 3t (1 t )2 P2 x 3t 2 (1 t ) P3 x t 3 P4 x
An incremental approach possible too.
Summary
Continuity of free-form curves (G0, G1, C0, C1,
C2…)
Cubic Curves (Splines)
Hermite
Matrix representation
Blending
Beizer
© Copyright 2026 Paperzz