PB Graphics 2018
PB Graphics 2018
Partha Bhowmick
IIT Kharagpur
http://cse.iitkgp.ac.in/~pb
2018
Contents
1 Introduction 1
2 2D Drawing 7
2.1.2 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3 Projection 19
4 Geometrical Transformations 21
4.1 2D transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.4 3D transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
ii CONTENTS
5 Visible-Surface Determination 25
5.1 Back-face culling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
5.2 Painter’s algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
5.3 Z-buffer algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
5.4 Ray tracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
6 Illumination 31
6.1 Illumination models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
6.1.1 Ambient light . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
6.1.2 Diffuse reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
6.1.3 Specular reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
6.1.4 Multiple light sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.1.5 Mapping from intensity to pixel color . . . . . . . . . . . . . . . . . . . . 35
7 Color Models 37
7.1 Human Vision System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
7.1.1 Resolution of Human Eye . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
7.1.2 Pixels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
7.1.3 Sensitivity of Human Eye . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
7.2 Achromatic Light . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
7.2.1 Gamma Correction of Cathode Ray Tubes (CRTs) . . . . . . . . . . . . . 39
7.3 Chromatic Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
7.3.1 Tints, Shades, Tones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
7.3.2 RGB Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
7.3.3 HSL and HSV Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
7.3.4 RGB to HSL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
8 Shading 47
8.1 Interpolation techniques for shading . . . . . . . . . . . . . . . . . . . . . . . . . 47
8.1.1 Flat shading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
8.2 Smooth shading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
8.2.1 Gouraud shading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
8.2.2 Phong shading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
CONTENTS iii
9 Mesh Processing 53
9.1 DCEL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
10 Filling 57
11 Clipping 61
11.3 3D Clipping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Chapter 2
2D Drawing
—Ellsworth Kelly
Observation 1 For each integer x ∈ [x1 , x2 ], there is exactly one integer point (x, y) that belongs
to the DSL. Hence, we can increment x from x1 to x2 at unit step and compute the corresponding
integer value of y.
(x2 , y2 )
(x1 , y1 )
distance 6 1
2
y+1 NE y+1 NE
− m
− m
y
+ E
y
+ E
x x+1 x x+1
Figure 2.2: Two possible cases in Bresenham’s line drawing algorithm. Left: E is selected, as m
lies above l. Right: NE is selected, as m lies below l.
Observation 2 As the slope lies in the interval [0, 1], from each pixel p(x, y), the next pixel
would be either East: E(x + 1, y) or Northeast: NE(x + 1, y + 1). See Figure 2.2.
Observation 3 Let l : ax + by + c = 0 be the real line passing through p1 and p2 . Consider the
decision function f (x, y) = ax + by + c. Let p(u, v) lie on l and p0 (u, v + k) lie above l. Then
f (p) = 0, and so f (p0 ) = f (p) + bk = bk. As k > 0, the sign of the half-plane above l is same as
that of b.
As b < 0, using Observation 3, we can conclude the following about any real point p (see
Figure 2.2).
> 0 ⇐⇒ p lies below l
f (p) < 0 ⇐⇒ p lies above l
= 0 ⇐⇒ p lies on l
Let p(x, y) be the current pixel selected by the algorithm (Figure 2.2). By Observation 2, the
next pixel will be either E or NE depending on whether m(x + 1, y + 12 ) lies above l or not. For
this, compute f (m) := a(x + 1) + b(y + 12 ) + c, and then select NE if f (m) > 0, and E otherwise.
Computation of f See Figure 2.3. To simplify the computation, we increment the value of f (m)
at a midpoint m(x + 1, y + 12 ) to compute that of f (m0 ) at the next midpoint m0 . Two possible
increments:
The 1st midpoint is m1 (x1 +1, y1 + 12 ). We get f (m1 ) = a(x1 +1)+b(y1 + 21 )+c = f (p1 )+a+ 12 b =
a+ 12 b, since f (p1 ) = 0. It’s not an integer but a half-integer, if b is odd. To make all computations
in the integer domain, we just double it. Doubling f does not affect its sign, but it’s an advantage,
as we need to always check the sign of an integer expression (2a + b to start with). We redefine
2.1 Line Drawing 9
m0
y+1 NE y+1 NE
m m
m0
E E
y y
2.1.2 Remarks
Bresenham’s algorithm is widely used to quickly draw line segments in a bitmap (e.g. on a
computer screen), as it uses only integer addition, subtraction and bit shifting, all of which are
very cheap operations. It is one of the earliest algorithms developed in the field of computer
graphics.
While algorithms such as Wu’s algorithm are also frequently used in modern computer graphics
because they support anti-aliasing, the speed and simplicity of Bresenham’s line algorithm makes
it attractive in hardware such as plotters and in modern graphics cards. It can also be found in
many software graphics libraries.
Chapter 2
10 2D Drawing
A digital circle is obtained by digitization of a real circle with integer center and integer radius
(Fig. 2.4). It is a closed 0-connected digital curve such that the smaller between x-distance and
y-distance of each pixel from the corresponding real circle is at most 12 .
8 Oct
ant ant
Oct 1
7
Oct
ant
ant
Oct
x
O
3
Oct
ant
ant
Oct
6
Oct 4
ant ant
5 Oct
Figure 2.4: Left: A real circle (shown in red) of radius 11 and its corresponding digital circle.
Right: A digital circular arc of radius 41.
Let C(c, r) denote the digital circle having center at c ∈ Z2 and integer radius r. When the
center is o := (0, 0), we denote it by C(o, r). The circle drawing algorithm is designed based on
the following observations.
Observation 1 The digital circle C(c, r) is simply given by all the pixels of C(o, r), translated
by the vector →
−
oc. Hence, an algorithm for construction of C(o, r) serves the purpose.
Observation 2 C(o, r) comprises eight symmetric octants (Fig. 2.4). For t = 1, 2, . . . , 8, let
Ct (o, r) denote the t-th octant of C(o, r). In particular, for the 1st octant we have 0 6
x 6 y. If a pixel p(x, y) belongs to C1 (o, r), then the pixels in other seven octants are
given by considering all possible signs of (x, y) along with their swapped values. That is,
(x, y) ∈ C1 (o, r) =⇒ (u, v) : {|u|, |v|} = {x, y} ⊂ C(o, r). For example, in Fig. 2.4,
the pixel (4, 6) belongs to C1 (o, 7), and so the corresponding pixels in other seven octants are
(4, −6), (−4, 6), (−4, −6), (6, 4), (6, −4), (−6, 4), (−6, −4).
A digital circle is thus 8-symmetric in nature. The lines of symmetry are the four halves of two
principal axes and the four halves of ±45◦ lines, about o. The algorithm is designed, therefore,
to generate the pixels of C1 (o, r); then simply reflecting these pixels about the eight lines of
symmetry produce the complete circle.
2.2 Circle Drawing 11
x x+1 x+2 x x+1 x+2
E E
y y
m m
m0
y−1 SE y−1 SE
m0
Figure 2.5: Two possible cases in Bresenham’s circle drawing algorithm. Left: E is selected, as
m lies inside the real circle. Right: SE is selected, as m lies outside.
Observation 3 In 1st octant, y-distance of a pixel in C(o, r) from the real circle is smaller than
x-distance. Hence, for a clockwise traversal in C1 (o, r), the pixel next to (x, y) is either east
(x + 1, y) or southeast (x + 1, y − 1).
Observation 4 By Observation 3, for each integer x ∈ [0, r], there is exactly one integer point
(x, y) with x 6 y that belongs to C1 (o, r). Hence, we can increment x from 0 to r at unit step
and compute the corresponding integer value of y until x > y.
Decision function The idea of decision function is similar to the one used in Bresenham’s line
drawing algorithm. Let f (x, y) = x2 + y 2 − r2 be the decision function for C(o, r). A point
lies inside, on, or outside C(o, r) accordingly as f is negative, zero, or positive at that point
(Fig. 2.5). To decide the next pixel w.r.t. the current pixel p(x, y) ∈ C1 (o, r), we check the
location of the midpoint m(x + 1, y − 21 ), using the following functional value.
1 2
2
f (m) = (x + 1) + y − − r2 (2.3)
2
If f (m) is negative, then E(x + 1, y) is selected, else SE(x + 1, y − 1).
Difference computation To optimize the computation, we use the value of f (m) to obtain f (m0 )
at the next midpoint m0 w.r.t. E or SE, as the case may be. It works as follows.
1 2
2
0
E is selected: f (m ) = (x + 2) + y − − r2 = f (m) + dE , (2.4)
2
3 2
2
0
SE is selected: f (m ) = (x + 2) + y − − r2 = f (m) + dSE , (2.5)
2
where,
dE = 2x + 3 and dSE = 2x − 2y + 5. (2.6)
Chapter 2
12 2D Drawing
Double difference Eq. 2.6 provides the values of dE and dSE using the (x, y) coordinates of p.
For the next pixel (E or SE), these values need to be recomputed. For further optimization, we
increment their previous values as follows.
d2E|E = dE|E − dE = 2(x + 1) + 3 − (2x + 3) = 2.
E is selected (2.7)
d2
SE|E = dSE|E − dSE = 2(x + 1) − 2y + 5 − (2x − 2y + 5) = 2.
d2E|SE = dE|SE − dE = 2(x + 1) + 3 − (2x + 3) = 2.
SE is selected (2.8)
d2
SE|SE = dSE|SE − dSE = 2(x + 1) − 2(y − 1) + 5 − (2x − 2y + 5) = 4.
Initialization The first pixel selected for C1 (o, r) is (0, r). The corresponding midpoint is m =
(1, r − 12 ) at which f (m) = 12 + (r − 12 )2 − r2 = 1 − r + 41 , which is not an integer. However, since
r is an integer and so also the values of dE and dSE , the sign of of f (m) does not get affected if
we disregard its fractional part, 14 . As a result, all computations remain in integer domain, as
shown in Algorithm 2. The respective initial values of dE and dSE , set at (0, r), are 2 · 0 + 3 = 3
and 2 · 0 − 2r + 5 = −2r + 5.
2.3 Curve Drawing 13
• With four knowns such as four control points in B-spline, we can get a non-planar
curve; but with three, we always get it planar!
• For higher degree, the curve may be wiggly or locally non-smooth. For cubic, the
wiggling effect is minimal.
Q(t)
t<0 t>1
06t61
Continuity and Smoothness The parametric tangent vector of the curve Q(t) is given by
d
Q(t) = Q0 (t) = 3t2
2t 1 0 C. (2.12)
dt
While joining two curve segments, say Q1 (t) and Q2 (t), for drawing a larger curve segment,
the end of the first segment should coincide with the start of the second (Fig. 2.7). That is,
at the junction point, we should have Q1 (t = 1) = Q2 (t = 0). This is called G0 continuity (in
both parametric and geometric sense). If the tangents drawn to Q1 (t) and Q2 (t) at the junction
point (also called ‘knot point’) have same directions, then the resultant curve is continuous at
the junction point and said to satisfy the geometric continuity G1 . If the tangent vectors are
identical in both magnitude and direction, i.e., Q01 (t = 1) = Q02 (t = 0), then the resultant
curve satisfies the parametric continuity C 1 . Clearly, satisfying C 1 means satisfying G1 , but not
necessarily the converse.
junction satisfies G0 but not C 1 junction satisfies G0 and G1 junction satisfies G0 and C 1
Figure 2.7: Joining of two parametric cubic curves with different levels of continuity.
Left: only G0 . Middle: G0 and G1 . Right: G0 and C 1 (this is smoothest).
Blending Functions Using a basis matrix M = [mij ]4×4 and a geometry vector G =
h 4 iT
[G1 G2 G3 G4 ]T = gix giy i=1 , the coefficient matrix can be rewritten as
Significance of M : The column vector G is has four elements of geometric constraints. In case
of 2D curves, each of these four elements is a 2-tuple (e.g., G1 = [g1x g1y ]), whereas for 3D
curves, it is a 3-tuple (e.g., G1 = [g1x g1y g1z ]). The geometric constraints are just the input,
such as four control points (for B-splines) or two control points and their tangent vectors (for
Hermite and Bezier curves). M is the basis matrix that decides the proportion in which the
elements of G are mixed—by multiplying with the elements of M serving as ‘weights’—as per
the required nature of the cubic curve.
A cubic Hermite curve segment is defined by four constraints—two endpoints and their tangent
vectors (Fig. 2.8). To set up Eq. 2.14 (Q(t) = T M G), we derive M using four equations from
these four constraints. The geometry vector is taken as G = [P1 P4 R1 R4 ]T , where P1 and P4
are the endpoints, and R1 and R4 are respective tangent vectors. Using Eqns. 2.9, 2.10, and
2.11, the x-component is written as
x(t) = ax t3 + bx t2 + cx t + dx = T Cx = [t3 t2 t 1]M Gx =⇒ x0 (t) = [3t2 2t 1 0]M Gx ,
where, Gx = [g1x g2x g3x g4x ]T . Hence,
P1 = x(0) = [0 0 0 1]M Gx ,
P4 = x(1) = [1 1 1 1]M Gx ,
R1 = x0 (0) = [0 0 1 0]M Gx ,
R4 = x0 (1) = [3 2 1 0]M Gx .
So,
P1 0 0 0 1 0 0 0 1
P4 1 1 1 1 1 1 1 1
Gx =
R1 = 0 0 1
M Gx =⇒ I = M,
1 0 0 1 1
R4 x 3 2 1 0 3 2 1 0
−1
0 0 0 1 2 −2 1 1
1 1 1 1 −3 3 −2 −1
or, M = 0 0 1 1 =
.
0 0 1 0
3 2 1 0 1 0 0 0
Chapter 2
16 2D Drawing
R1 P1 P4
R4
Figure 2.8: Examples of Hermite curves. First three are single curve segments each, the last one
is where two curve segments share an endpoint and have two opposite tangents at that endpoint.
1
6 0) is given by A−1 =
Recall that the inverse of a square matrix A (with determinant |A| = T
|A| C ,
where C is the cofactor matrix of A given from its minors M as follows.
i+j Mij if i + j is even
Cij = (−1) Mij =
−Mij if i + j is odd
Bézier curve replaces the two tangent vectors of Hermite curve by two control points that do
basically fix the tangents when taken with the other two control points. By this, it interpolates
the two end control points and approximates the other two. For four control points P1 , P2 , P3 , P4
in sequence, the respective tangent vectors R1 and R4 at P1 and P4 are determined by the vectors
−−−→ −−−→
P1 P2 and P3 P4 . In particular, the respective tangents P1 and P4 are defined as
Then the matrix MHB defines the relation GH = MHB · GB between the Hermite geometry
vector GH and the Bézier geometry vector GB . It just uses Eq. 2.17 and gives the following
equation.
P1 1 0 0 0 P1
P4 0 0 0 1 P2 = MHB · GB
GH = =
R1 −3 3 (2.19)
0 0 P3
R4 0 0 −3 3 P4
To find the Bézier basis matrix MB , we start with the Hermite form, use Eq. 2.19, and
proceed as follows.
Q(t) = T · MH · GH = T · MH · MHB · GB = T · MH · MHB · GB = T · MB · GB , (2.20)
2.3 Curve Drawing 17
Figure 2.9: Plot of Bernstein polynomial functions up to degree 4 with summation of all four
functions to show characteristic of partition of one. (source: https://en.wikipedia.org/wiki/
Bernstein_polynomial)
where,
−1 3 −3 1
3 −6 3 0
MB = MH · MHB =
−3
. (2.21)
3 0 0
1 0 0 0
Thus, from Eq. 2.20, we get
The four polynomials BB = T ·MB , which serve as the weights in Eq. 2.22, are called Bernstein
polynomials (Fig. 2.9).1
Figure 2.10 shows two Bézier curve segments with a common endpoint P4 , serving as the knot
point. If P4 −P3 = k(P5 −P4 ) with k > 0 (i.e., the three control points are distinct and collinear),
then G1 continuity is ensured. For k = 1, we get C 1 continuity that makes the curve portion
even smoother at the knot point.
At the knot point, we consider the 0-th order and 1st order continuities of x-values of left segment
and right segment. We get
d d
xl (t = 1) = xr (t = 0), xl (t = 1) = xr (t = 0),
dt dt
or, xl (t = 1) = xr (t = 0) = x4 , 3(x4 − x3 ) = 3(x5 − x4 ) =⇒ C1 is ensured .
1
See https://en.wikipedia.org/wiki/Bernstein_polynomial for further details.
Chapter 2
18 2D Drawing
p5
p2
p6
p4
knot
p1 p3 p7
Figure 2.10: Two Bézier curves joined at P4 . Point P3 , P4 , and P5 are collinear here, and hence
G1 is maintained. However, C 1 is not present here, as P4 − P3 6= P5 − P4 .
2.3 Curve Drawing 19
which is obtained from the parametric continuity and the geometric continuity of B-splines.
The geometry vector is constructed from the 4 control points, namely (x0 , y0 ), (x1 , y1 ), (x2 , y2 ),
and (x3 , y3 ), defining a B-spline segment as follows:
x0 y0
x1 y1
G= x2 y2 .
(2.24)
x3 y3
Clipping
—Mark Twain
Region codes The clip window W is specified by four coordinates: xmin , xmax , ymin , ymax . These
coordinates provide the four boundary lines of W , namely L (left), R (right), B (bottom), and
T (top)—in this order by convention, although any other convention would also do. Accordingly,
for each of L, R, B, T, we get a pair of half-planes—one containing W and the other without.
The bit of the half-plane containing W is set to 0, and the other to 1. For example, its rightmost
bit is set to 0 if it lies in the left half-plane of L, else it is set to 1. Thus, for encoding the location
of a point, 4 bits are enough (6 bits in the three-dimensional case). Other three bits are set in
a similar manner depending on its location w.r.t. R, B, T. The resultant codes of nine regions
are referred to as rCodes and shown in Fig. 11.1.
An rCode is computed for each of the two endpoints of the line. It must be recomputed in each
iteration after the clipping occurs. The algorithm includes, excludes, or partially includes the
input line segment based on analysis of three cases (Fig. 11.2) as follows.
1. Both endpoints are in W (bitwise OR = 0000): trivial accept.
2. Both endpoints lie in the same half-plane that does not contain W (bitwise AND 6= 0000):
trivial reject.
3. Neither of the above: nontrivial case (leads to partial or complete rejection at the end).
3.1. Find the outpoint (one of the two endpoints that lies outside W ).
3.2. Compute the intersection of the line with L/R/B/T.
3.3. Replace the outpoint by the intersection point.
3.4. Repeat until a trivial accept or trivial reject occurs.
trivial reject
1000 outpoint replaced
1001 1000
The pseudo-code in C/C++ is given in Fig. 11.3. The coordinates of the two endpoints are
denoted by (x0 , y0 ) and (x1 , y1 ). For finding the intersection point, we use the following:
y1 − y0
slope = ,
x1 − x0
1
x = x0 + (ym − y0 ), where ym is ymin or ymax ,
slope
y = y0 + slope(xm − x0 ), where xm is xmin or xmax .
No need to worry about divide-by-zero because, in each case, the rCode-bit being tested guar-
antees the denominator is non-zero.
11.2 Polygon Clipping 65
Figure 11.4: Steps for clipping a concave polygon ‘W’ with a 5-sided convex polygon using
Sutherland-Hodgman algorithm (source: https://en.wikipedia.org/wiki/Sutherland%E2%
80%93Hodgman_algorithm).
In computer graphics and games development, polygons are clipped based on a window, which
may be rectilinear or convex or, in general, of any arbitrary shape. As a polygon is defined by
a sequence of vertices or edges, the subject or candidate polygon is usually supplied in a list of
vertices in a specific order. The portion(s) of the subject polygon that lies inside the window
is kept, and the rest is clipped. A polygon clipping algorithm is usually designed to handle
different cases such as convex polygon (simplest case), polygon with holes, non-convex polygon,
and self-intersecting polygon.
This algorithm is widely used for clipping polygons3 when the clip polygon (i.e., window) is
convex. It works by extending each line of the convex clip polygon in turn and selecting only
vertices from the subject polygon that are on the visible side (Fig. 11.4).
The algorithm begins with an input list of all vertices in the subject polygon. Next, one side of
the clip polygon is extended infinitely in both directions, and the path of the subject polygon is
traversed. Vertices from the input list are inserted into an output list if they lie on the visible
side of the extended clip polygon line, and new vertices are added to the output list where the
subject polygon path crosses the extended clip polygon line.
This process is repeated iteratively for each side of the clip polygon side, using the output list
from one stage as the input list for the next. Once all sides of the clip polygon have been
processed, the final generated list of vertices defines a new single polygon that is entirely visible.
Note that if the subject polygon was concave at vertices outside the clipping polygon, then the
3
Ivan Sutherland and Gary W. Hodgman: Reentrant Polygon Clipping. Communications of the ACM, 17:32–
42, 1974.
Chapter 11
66 Clipping
List outputList = subjectPolygon;
for (Edge clipEdge in clipPolygon) do
List inputList = outputList;
outputList.clear();
Point S = inputList.last;
for (Point E in inputList) do
if (E inside clipEdge) then
if (S not inside clipEdge) then
outputList.add(ComputeIntersection(S,E,clipEdge));
end if
outputList.add(E);
else if (S inside clipEdge) then
outputList.add(ComputeIntersection(S,E,clipEdge));
end if
S = E;
done
done
new polygon may have coincident (i.e., overlapping) edges—this is acceptable for rendering, but
not for other applications such as computing shadows.
Given a list of edges in a clip polygon, and a list of vertices in a subject polygon, the procedure
for clipping the subject polygon against the clip polygon is shown in Fig. 11.5.
The vertices of the clipped polygon are to be found in outputList when the algorithm termi-
nates. Note that a point is defined as being inside an edge if it lies on the same side of the edge
as the remainder of the polygon. If the vertices of the clip polygon are consistently listed in a
counter-clockwise direction, then this is equivalent to testing whether the point lies to the left
of the line (left means inside, while right means outside), and can be implemented simply by
using a cross product.
ComputeIntersection is a function, omitted here for clarity, which returns the intersection of
a line segment and an infinite edge. Note that it is only called if such an intersection is known
to exist, and hence can simply treat both lines as being infinitely long.
B
Two shapes A−B B−A A∪B A∩B A xor B
A and B
can support holes, but requires additional algorithms to decide which polygons are holes, after
which merging of the polygons can be performed using a variant of the algorithm.
Vatti’s clipping algorithm 6 This algorithm allows clipping of any number of arbitrarily shaped
subject polygons by any number of arbitrarily shaped clip polygons (Fig. 11.7). Unlike the
Sutherland-Hodgman and Weiler-Atherton polygon clipping algorithms, the Vatti algorithm
does not restrict the types of polygons that can be used as subjects or clips. Even complex (self-
intersecting) polygons, and polygons with holes can be processed. The algorithm is generally
applicable only in 2D space.
While clipping usually involves finding the intersections (regions of overlap) of subject and
clip polygons, clipping is conceptualized in Vatti’s algorithm as the interaction of subject and
clip polygons, and realized as boolean set-theoretic operations (Fig. 11.6):
Vatti’s algorithm involves processing both subject and clipping polygon edges in an orderly
fashion, starting with the lowermost edges and working towards the top; this is conceptually
similar to the Bentley-Ottmann algorithm. This sweep line approach divides the problem space
by scanlines, imaginary horizontal lines that pass through every vertex of the participating
polygons. These scanlines outline scanbeams the spaces between adjacent scanlines. These
scanbeams are processed in turn, starting with the lowest scanbeam, with the algorithm adding
points of intersection within these scanbeams into the solution polygons.
11.3 3D Clipping
In 3D graphics, clipping is required in several tasks such as frustum culling, which is a process
of discarding objects that are not visible on the screen (Fig. 11.8).
6
Bala R. Vatti: A generic solution to polygon clipping, Communications of the ACM, 35(7):56–63, 1992.
Chapter 11
68 Clipping
Figure 11.7: Polygon clipping (intersection, union, difference, and xor, based on Vatti’s clipping
algorithm. (source: Clipper—an open source freeware library for clipping and offsetting lines
and polygons. URL: http://www.angusj.com/delphi/clipper.php).