35
Bresenham’s Algorithm

Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

Embed Size (px)

Citation preview

Page 1: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

Bresenham’s Algorithm

Page 2: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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).

Page 3: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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

Page 4: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

Stepping in X direction

Page 5: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D
Page 6: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

Bresenham’s Algorithm

• Improve upon DDA algorithm to use integer arithmetic only.

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

Page 7: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

Candidate Pixels

1 m 0

last pixel

candidates

Note that line could havepassed through any

part of this pixel

Page 8: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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

Page 9: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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

Page 10: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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.

Page 11: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

Curve Surfaces

Page 12: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

Examples of Curve Surfaces

• Spheres

• The body of a car

• Almost everything in nature

Page 13: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

Representations

• Simple (or “explicit”) functions.

• Implicit functions.

• Parametric functions.

Page 14: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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

Page 15: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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.

Page 16: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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)

Page 17: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

But, how do we design or specify a surface?

Page 18: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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.

Page 19: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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

Page 20: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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

Page 21: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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

Page 22: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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.

Page 23: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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

Page 24: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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

Page 25: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

Bezier Matrix

1331

0363

0033

0001

MB

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

blending functions

Page 26: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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)

Page 27: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

Blending Polynomial

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

Page 28: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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?

Page 29: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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.

Page 30: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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

Page 31: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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.

Page 32: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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

Page 33: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

But, the graphics hardware knows triangles only…

Page 34: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

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).

Page 35: Bresenham’s Algorithm. Line Drawing Reference: Edward Angel’s book: –6 th Ed. Sections 6.8 and 6.9 Assuming: –Clipped (to fall within the window) –2D

Subdivision

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