CSCE 441 Lecture 6

From Notes
Jump to navigation Jump to search

« previous | Monday, January 27, 2014 | next »


Clipping Lines

Don't waste time drawing things that aren't on the screen.

Clipping Points is easy: given a point , clipping window is a rectangle from to , we check and . If both conditions meet, we draw the point.

Lines are determined by their endpoints. What if one or both endpoints are outside the window?

Simple Algorithm

If both end-points are inside the clipping window, then we can draw the line. Otherwise, we have to intersect the line with all edges of the clipping rectangle and draw using the intersection(s) as the new endpoint(s).

Checking intersection

  • (e.g. with right boundary): , where
  • (e.g. with bottom boundary):

Boundary cases: intersecting lines that are parallel to boundary line.

This algorithm can be computationally expensive.


A Better Way: Cohen-Sutherland Algorithm

Trivial accepts/rejects

How can we quickly decide whether line segment is entirely inside the window? Test if both endpoints are on the same side (outside) a single boundary (e.g.


Use 4-bit region codes to represent edges:

top bottom left right
1001 1000 1010
0001 0000 0010
0101 0100 0110

Classify using region codes , respectively

  • if c0 & c1 != 0, then trivially reject
  • if c0 | c1 == 0, then trivially accept
  • Otherwise reduce to trivial cases by splitting into two segments
    • compute and clip each boundary as determined by bits


Even Better: Liang-Barsky Algorithm

  • Use parametric form of line for clipping
  • Lines are oriented (moving in-to-out or out-to-in)
  • We don't need to find actual intersection points, but only the parameter values for which the line is within the window.


Initialize interval to .

For each boundary,

  • find parametric intersection with boundary
  • If moving in-to-out,
  • If moving out-to-in,
  • If at any point , we reject the line.

Review: Parametric Lines

Intersecting parametric lines:

Given two lines and

Set and equations equal, replacing parameters to prevent conflict, and then solve for parameters.

Classifying Direction

For boundaries, if , then we classify as in-to-out (even if ).

For boundaries, if , then we classify as in-to-out (even if ).

Summary

Cohen-Sutherland is best used when trivial accept/reject is possible for most lines.

Liang-Barsky: computing values is cheap.

Both can be used together.


Clipping Polygons

Why is this hard?

  1. Added edges: When clipping a triangle within a rectangle, the clipped polygon can have up to 7 new edges!
  2. Drawing a concave polygon can result in multiple polygons on the screen


Sutherland-Hodgman Clipping

  • Consider each edge of the view window individually
  • Clip polygon against view window edge's equation.