Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and...

Preview:

Citation preview

Bresenham’s Algorithm

Line Drawing

• Reference: Edward Angel’s book:– 6th Ed. Sections 6.8 and 6.9

• Assuming:– Clipped (to fall within the window)– 2D screen coordinates– Each pixel covers a square region

• where is its center?• Rounding (X, Y) implies center at (0.0, 0.0).• Truncating implies center at (0.5, 0.5).

DDA Algorithm• DDA stands for digital

differential analyzer.• The differential

equation for a line is: m = dy / dx

• Avoids the (relatively) more expensive multiplication.

y = mx + h

Stepping in X direction

Bresenham’s Algorithm

• Improve upon DDA algorithm to use integer arithmetic only.

• Applicable to circle drawing too. We discuss only the line drawing here.

Candidate Pixels

1 m 0

last pixel

candidates

Note that line could havepassed through any

part of this pixel

Decision Variables• Variables a and b

record the distances to the pixel centers of (i+1, j) and (i+1, j+1)

• If a > b, then y=j• If a < b, then y= j+1

Integer Only

• If (x1, y1) and (x2, y2) are integer points, then x * a and x * b may be stored as integers as well.– Because a and b increase or decrease by

m = y / x

Using the Symmetry

• The above works when x 0, y 0, and x y.

• If x < y, then step in Y direction instead.

• It is easy to extend it to

handle x < 0 or y < 0.

Curve Surfaces

Examples of Curve Surfaces

• Spheres

• The body of a car

• Almost everything in nature

Representations

• Simple (or “explicit”) functions.

• Implicit functions.

• Parametric functions.

Explicit Functions• For example: z = f(x, y)

– Independent variables: x and y– Dependent variable: z

• Easy to render:– For the above, loop over x and y.

• But too limited:– For example, how do you describe a sphere

centered at the origin? – z = (r2-x2-y2)1/2 gives us the upper hemisphere only.

x

y

z

Implicit Functions

• 0 = f (x, y, z)– All variables are independent variables.

• Plane: ax+by+cz = 0

• Sphere: x2+y2+z2-r2 = 0

• More powerful than explicit functions, but harder to render.

Parametric Functions• x = fx(u, v)• y = fy(u, v)• z = fz(u, v)• Cubic curve: p(u) = c0+c1u+c2u2+c3u3 • Sphere:

– x = r cos(u)cos(v)– y = r sin(u)cos(v)– z = r sin(v)

• To render it, loop over u and v.

p(u)

p(umin)

p(umax)

x

y

z p(u,0)

p(1,v)p(0,v)

p(u,1)

But, how do we design or specify a surface?

Control Points

• Like bending a piece of wood, we control its shape at some control points.

• Some control points lie on the curve and some don’t. Those lie on the curves are called knots.

Interpolation• Let p(u) = c0+c1u+c2u2+c3u3

• Given 4 control points p0, p1, p2, p3, we may make p(u) pass through all of them at u=0, 1/3, 2/3, 1.

• See Section 10.4 for the derivation of c = [c0, c1, c2, c3]T

p0

p1

p2

p3

Matrix-Vector Form

ucu k

kk

3

0

)(p

c

c

c

c

3

2

1

0

c

u

u

u

3

2

1

udefine

uccu TTu )(pthen

Hermite Form

p(0) p(1)

p’(0) p’(1)

Use two interpolating conditions andtwo derivative conditions per segment

Ensures continuity and first derivativecontinuity between segments

Bezier Curve• Instead of interpolating all 4 control

points (p0, p1, p2, p3), p1 and p2 controls the tangents at p0 and p3.

• The curve lies in the convex hull of the four control points.

Approximating Derivatives

p0

p1p2

p3

p1 located at u=1/3 p2 located at u=2/3

3/1

pp)0('p 01

3/1

pp)1('p 23

slope p’(0) slope p’(1)

u

Equations

p(0) = p0 = c0

p(1) = p3 = c0+c1+c2+c3

p’(0) = 3(p1- p0) = c0

p’(1) = 3(p3- p2) = c1+2c2+3c3

Interpolating conditions are the same

Approximating derivative conditions

Solve four linear equations for c=MBp

Bezier Matrix

1331

0363

0033

0001

MB

p(u) = uTMBp = b(u)Tp

blending functions

Blending Functions

b(u)

3(1 u)

3u 2(1 u)

3 2u (1 u)3u

Note that all zeros are at 0 and 1 which forcesthe functions to be smooth over (0,1)

Blending Polynomial

Q: What are the blending polynomials for interpolation? (A: See Fig 10.11)

Piecewise Curve Segments• For curves with more than 4 control points,

we may either:– Increase the degree of polynomials, or– Join piecewise segments.

• Do pieces meet smoothly at the join points?

Cn vs Gn Continuity

• Cn means continuity at n-th derivative.

• Gn doesn’t require the exact match of n-th derivatives at the joint, just being proportional.

• The tangents point in the same direction, but they may have different magnitudes.

B-Spline

• If we don’t require the curve to pass through any control point, we may have more control at the join points.

• To define the curve between pi and pi+1,

use also pi-1 and pi+2

NURBS

• Non-uniform Rational B-Spline.

• In NURBS, we may use the weights to change the importance of a control point.

• We won’t discuss it in depth here. For details, see Sections 10.8.

For A More Formal Discussion

• The above discussion is aimed at stimulating your interest.

• For a more formal discussion, especially if you’re interested in researches in these areas, see Angel’s book chapter 10.

• [Bonus] Tensor-product surfaces are mentioned in 10.4.2

But, the graphics hardware knows triangles only…

Tessellation

• Curve surfaces can be approximated by (a lot of) polygons for the purpose of rendering.

• The tessellation may be static (done before rendering) or dynamic (during rendering).

Subdivision

• For example, the “de Casteljau” algorithm for rendering Bezier Splines