Lesson 4
Fundamental
algorithms
Trinh Thanh Trung School of ICT, HUST
Content
1. Clipping algorithms
2. Polygon drawing
3. Anti-aliasing
1.
Clipping algorithms
Clipping
■ A fundamental task in graphics is to keep those
parts of an object that lie outside a selected view
from being drawn
■ Clipping is the removal of all objects or part of
objects in a modelled scene that are outside the
real-world window.
Clipping
■ For each point to be rendered on the screen
xmin x xmax
ymin y ymax
xmin xmax
ymax
ymin
Line clipping
■ Lines are defined by their endpoints, so it should
be possible just to examine these (in a similar way
to points) and determine whether or not to clip
without considering every pixel on the line
■ We often have windows that are either very
large, i.e. nearly the whole scene fits inside, or very
small, i.e. most of the scene lies inside the window
■ Hence, most lines may be either trivially
accepted or rejected
Cohen-Sutherland Outcode
Line clipping
Cohen-Sutherland Outcode
■ Each point on all lines are first assigned an
“outcode” defining their position relative to the
clipping rectangle
■ If p.x <= xmin: P.code or 0001
■ If p.y <= ymin: P.code or 0100
■ If p.x >= xmax: P.code or 0010
■ If p.y >= ymax: P.code or 1000
Cohen-Sutherland Outcode
■ If P1.code OR P2.code == 0000
□ Accept line
■ If P1.code AND P2.code != 0000
□ Remove line
■ In case the line crosses the screen, the end
points will be redefined by the intersections of the
line and the boundary of the display.
Cohen-Sutherland
■ The Cohen-Sutherland line-clipping algorithm is
particularly fast for “trivial” cases, i.e. lines
completely inside or outside the window.
■ Non-trivial lines, i.e. ones that cross a boundary
of the window, are clipped by computing the
coordinates of the new boundary endpoint of the
line where it crosses the edge of the window
Cyrus-beck & Liang-barsky
Line clipping
Cyrus-beck & Liang-barsky
■ The Cohen-Sutherland algorithm requires the
window to be a rectangle, with edges aligned with
the co-ordinate axes
■ It is sometimes necessary to clip to any convex
polygonal window, e.g. triangular, hexagonal, or
rotated.
■ Cyrus-beck and Liang-Barsky line clippers better
optimise the intersection calculations for clipping to
window boundary
■ Nicholl-Lee-Nicholl reducing redundant
boundary clipping by identifying edge and corner
regions
Cyrus-beck & Liang-barsky
■ x = x1 + (x2 - x1)u = x1 + uDx
■ y = y1 + (y2 - y1)u = y1 + uDy
■ xmin x1 + Dx.u xmax x [xm, xM]
■ ymin y1 + Dy.u ymax y [ym, yM]
■ Pk u qk k = 1, 2, 3, 4
q1 = x1 − xm P1 = −Dx
q2 = xM − x1 P2 = Dx
q3 = y1 − ym P3 = −Dy
q4 = yM − y1 P4 = Dy
Sutherland-Hodgman
Polygon clipping
Sutherland-Hodgman clipping
■ Sutherland-Hodgman basic routine
□ Go around polygon one vertex at a time
□ Current vertex has position p
□ Previous vertex had position s, and it has been
added to the output if appropriate
Sutherland-Hodgman clipping
■ Edge from s to p takes one of four cases:
inside outside inside outside inside outside inside outside
s p p s
p s p s
p output i output no output i output
p output
Sutherland-Hodgman clipping
1. s inside plane and p inside plane
o Add p to output
o Note: s has already been added
2. s inside plane and p outside plane
o Find intersection point i
o Add i to output
3. s outside plane and p outside plane
o Add nothing
4. s outside plane and p inside plane
o Find intersection point i
o Add i to output, followed by p
2.
Polygon drawing
Polygon scan conversion
■ Polygon scan conversion is a classic general purpose
algorithm.
■ For each scan-line we determine the polygon edges
that intersect it, compute spans representing the interior
portions of the polygons along this scan-line and fill the
associated pixels.
■ This represents the heart of a scan-line rendering
algorithm used in many commercial products including
Renderman and 3D Studio MAX.
Polygon scan conversion
■ Use mid-point algorithm
to specify the boundaries
between polygons
■ Basic algorithm:
□ Specify the intersections
between scanline and
polygon boundaries
□ Sort x value incrementally
□ Fill the pixels between
each pair of x