0% found this document useful (0 votes)
11 views

PB Graphics 2018

This document discusses computer graphics and covers topics such as 2D drawing algorithms like Bresenham's line and circle algorithms, curve drawing using parametric, Hermite, and Bézier curves, projections, geometric transformations, visible surface determination, illumination models, color models, shading techniques, mesh processing, filling, and clipping.

Uploaded by

ManeeshMahajan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

PB Graphics 2018

This document discusses computer graphics and covers topics such as 2D drawing algorithms like Bresenham's line and circle algorithms, curve drawing using parametric, Hermite, and Bézier curves, projections, geometric transformations, visible surface determination, illumination models, color models, shading techniques, mesh processing, filling, and clipping.

Uploaded by

ManeeshMahajan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Computer Graphics

Selected Lecture Notes

Partha Bhowmick
IIT Kharagpur
http://cse.iitkgp.ac.in/~pb

2018
Contents
1 Introduction 1

1.1 Basic Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.2 Definitions and Terminologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2 2D Drawing 7

2.1 Line Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

2.1.1 Bresenham’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

2.1.2 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2.2 Circle Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

2.2.1 Bresenham’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

2.3 Curve Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

2.3.1 Parametric Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

2.3.2 Hermite Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

2.3.3 Bézier Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

2.3.4 B-spline (Uniform Nonrational) . . . . . . . . . . . . . . . . . . . . . . . . 19

3 Projection 19

3.1 Types of Projection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

3.1.1 Parallel Projection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

3.1.2 Perspective Projection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

3.2 Computation of Projection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

4 Geometrical Transformations 21

4.1 2D transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

4.2 Homogeneous coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

4.3 Composition of transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

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

10.1 Flood Fill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

10.2 Polygon Filling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

11 Clipping 61

11.1 Line Clipping using Region Codes . . . . . . . . . . . . . . . . . . . . . . . . . . 61

11.2 Polygon Clipping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

11.2.1 Sutherland-Hodgman Algorithm . . . . . . . . . . . . . . . . . . . . . . . 65

11.2.2 Other Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

11.3 3D Clipping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Chapter 2

2D Drawing

When I see a white piece of paper, I feel I’ve


got to draw. And drawing, for me, is the be-
ginning of everything.

—Ellsworth Kelly

2.1 Line Drawing


A digital line segment (DLS) is obtained by digitization of a real straight line segment. Given
two pixels p1 (x1 , y1 ) and p2 (x2 , y2 ), with 0 6 x1 < x2 , 0 6 y1 < y2 , and x2 − x1 > y2 − y1 , the
DLS connecting p1 and p2 is a 0-connected digital curve satisfying the following properties:
1. Each pixel has x1 6 x 6 x2 .
2. The smaller between x-distance and y-distance of each pixel from the underlying real line
is at most 21 .
Fig. 2.1 shows an example.

2.1.1 Bresenham’s Algorithm

For designing the algorithm, we need the following observations.

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

Figure 2.1: An example of DLS with x2 − x1 = 8, y2 − y1 = 3.


Chapter 2
8 2D Drawing

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.

Selection of pixels Let dx = x2 − x1 and dy = y2 − y1 . Then, with s = dy /dx , we get the


equation of l as y = sx + t, or, dy x − dx y + ndx = 0, or, ax + by + c = 0, where,

a = dy , b = −dx , c = tdx . (2.1)

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:

i) if E is selected based on f (m), then m0 = (x + 2, y + 12 ), or, f (m0 ) = f (m) + dE , where


dE = a.
ii) if NE is selected based on f (m), then m0 = (x + 2, y + 32 ), or, f (m0 ) = f (m) + dN E , where
dN E = a + b.

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

x x+1 x+2 x x+1 x+2

Figure 2.3: Computation of decision function f in Bresenham’s algorithm. Left: E is selected,


and so f (m0 ) = f (m) + dE . Right: NE is selected, and so f (m0 ) = f (m) + dN E .

Algorithm 1: Bresenham’s line drawing algorithm.


1 int dx ← x2 − x1 , dy ← y2 − y1
2 int f ← 2dy − dx , dE ← 2dy , dN E ← 2(dy − dx )
3 int x ← x1 , y ← y1
4 select(x, y)
5 while x < x2 do
6 if f 6 0 then
7 x ← x + 1, f ← f + dE
8 else
9 x ← x + 1, y ← y + 1, f ← f + dN E
10 select(x, y)

the increments of f accordingly, as follows.


dE = 2a = 2(y2 − y1 ),
(2.2)
dN E = 2(a + b) = 2(y2 − y1 − x2 + x1 ).
The steps are shown in Algorithm 1.

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

2.2 Circle 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.

2.2.1 Bresenham’s Algorithm

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

Algorithm 2: Bresenham’s circle drawing algorithm.


1 int x ← 0, y ← r, f ← 1 − r, de ← 3, dse ← −2r + 5
2 select(u, v) : {|u|, |v|} = {x, y}
3 while x < y do
4 if f < 0 then //select E
5 f ← f + de
6 de ← de + 2, dse ← dse + 2
7 x←x+1
8 else
9 f ← f + dse
10 de ← de + 2, dse ← dse + 4
11 x ← x + 1, y ← y + 1
12 select(u, v) : {|u|, |v|} = {x, y}

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

2.3 Curve Drawing


In computer graphics, parametric cubic curves are predominantly used for drawing smooth and
continuous 2D/3D curves and surfaces. The advantages are as follows.
1. Explicit form (y = f (x), z = g(x)): Not easy to draw because of the following reasons.
• It is not possible to get multiple values of y or z for a single value of x, as needed in
nonlinear curves like circles, ellipses, spirals, etc.
• Rotational invariance is not possible.
• Vertical tangents are difficult to be handled.
2. Implicit form (f (x, y, z) = 0): Problems are as follows.
• May have multiple solutions but all may not be required, e.g., when drawing an
arbitrarily oriented semicircle.
• Lack of assurance to tangent continuity at the junction point of two implicit curves.
3. Parametric form (x = x(t), y = y(t), z = z(t)): Resolves the issues with the implicit and
the explicit forms, and offers readiness and flexibility for curve drawing. Some of the
important ones are as follows.
• Parametric curves are designed with parametric tangent vectors (always having finite
values) instead of geometric slopes (which may be infinite).
• A long curve is flexibly represented by a sequence of piecewise polynomial curves,
which can be joined smoothly with each other at the junction points.
4. Cubic parametric curves strike a balance between polynomial degree and computational
cost. It requires four coefficients to define a cubic curve, which are obtained from four
given knowns. These might be two endpoints and their tangents (Bezier curve) or might
be four control points (B-spline). The reasons for choosing cubic polynomial are as follows.

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

2.3.1 Parametric Equations

A curve segment Q(t) := [x(t), y(t)] in 2D can be represented as follows.


x(t) = ax t3 + bx t2 + cx t + dx , (2.9)
3 2
y(t) = ay t + by t + cy t + dy , (2.10)
where 0 6 t 6 1.
We set 0 6 t 6 1 to get a segment of the cubic curve, as shown in Fig. 2.6. For 3D curves, we
just have another cubic function z(t) defined by four other coefficients. For simplicity, in this
section we discuss about 2D parametric cubic curves, and all the equations can be extended for
3D parametric cubic curves as well.
Using parameter vector (T ) and coefficient matrix (C), we express Eqn. 2.9 succinctly as
follows.  
ax ay
 bx by 
T = [t3 t2 t 1], C =   cx cy  =⇒ Q(t) = T C.
 (2.11)
dx dy
Chapter 2
14 2D Drawing

Q(t)

t<0 t>1
06t61

Figure 2.6: A parametric cubic curve and its segment for 0 6 t 6 1.

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.

Q1 (t) Q2 (t) Q1 (t) Q2 (t) Q1 (t) Q2 (t)

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

[C]4×2 = [M ]4×4 [G]4×2 . (2.13)

Hence, from Eqn. 2.11,


Q(t) = T M G, (2.14)
from whose expansion we get
  
m11 m12 m13 m14 G1
  m21 m22 m23 m24   G2
Q(t) = [x(t) y(t)] = t3 t2
 
t 1  m31
 . (2.15)
m32 m33 m34   G3 
m41 m42 m43 m44 G4
2.3 Curve Drawing 15

From Eqn. 2.15, therefore, we get


(t3 m11 + t2 m21 + tm31 + m41 )g1x +
(t3 m12 + t2 m22 + tm32 + m42 )g2x +
x(t) = = weighted sum of elements gix of G.
(t3 m13 + t2 m23 + tm33 + m43 )g3x +
(t3 m14 + t2 m24 + tm34 + m44 )g4x
(t3 m11 + t2 m21 + tm31 + m41 )g1y +
(t3 m12 + t2 m22 + tm32 + m42 )g2y +
y(t) = = weighted sum of elements giy of G.
(t3 m13 + t2 m23 + tm33 + m43 )g3y +
(t3 m14 + t2 m24 + tm34 + m44 )g4y
Each weight is a cubic polynomial of t and is called the blending function. Thus, a blending
matrix is defined by B = T M , from which we get
Q(t) = BG. (2.16)

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.

2.3.2 Hermite Curves

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

2.3.3 Bézier Curves

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

R1 = Q0 (0) = 3(P2 − P1 ), R4 = Q0 (1) = 3(P4 − P3 ). (2.17)

Accordingly, the Bézier geometry vector becomes


 
P1
 P2 
GB = 
 P3  .
 (2.18)
P4

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

Q(t) = T · MB · GB = (1 − t)3 P1 + 3t(1 − t)2 P2 + 3t2 (1 − t)P3 + t3 P4 . (2.22)

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

2.3.4 B-spline (Uniform Nonrational)

The basis matrix is given by


 
−1 3 −3 1
1  3 −6 3 0 
M=  , (2.23)
6 −3
 0 3 0 
1 4 1 0

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

Hence, from Eqn. 2.14, we get


1
ax = (−x0 + 3x1 − 3x2 + x3 ) (2.25)
6
1
bx = (x0 − 2x1 + x2 ) (2.26)
2
1
cx = (−x0 + x2 ) (2.27)
2
1
dx = (x0 + 4x1 + x2 ) (2.28)
6
Chapter 11

Clipping

You can’t depend on your eyes when


your imagination is out of focus.

—Mark Twain

Clipping is somewhat like focusing—it is a method to selectively enable or disable rendering


operations within a defined region of interest (called clip window in 2D). A rendering algorithm
only draws pixels in the intersection between the clip region and the scene model.
By clipping, lines and surfaces outside the view volume
(aka. frustum) are removed1 . The inset figure shows a
view frustum, with near- and far-clip planes; only the
shaded volume is rendered. A clip window or clip regions Top
is commonly specified to improve render performance. It
allows the renderer to save time and energy by skipping
calculations related to pixels that the user cannot see. Pix-
els that will be drawn are said to be within the clip win- Right
dow. Pixels that will not be drawn are outside the clip Near
window. More informally, pixels that will not be drawn
are said to be “clipped”.

11.1 Line Clipping using Region Codes


Cohen-Sutherland algorithm is used for line clipping in 2D when the clip window W is rectilinear,
i.e., an axis-parallel rectangle. If it is a rectangle but not axis-parallel, then the coordinate
system can be rotated accordingly so that W becomes axis-parallel. The algorithm divides
the two-dimensional space into 9 regions, encodes them uniquely into 4-bit region codes, and
then performs clipping on each input line. The algorithm was developed in 1967 during flight-
simulator work by Danny Cohen and Ivan Sutherland.2
1
Gary Bertoline and Wiebe, Eric (2002): Fundamentals of Graphics Communication (3rd ed.), McGraw-Hill,
p. G-3.
2
Principles of Interactive Computer Graphics, p. 124, 252, by Bob Sproull and William M. Newman, 1973,
McGraw-Hill Education, International edition.
Chapter 11
62 Clipping
0001 L R 0010
1000 1001 1000 1010
T

0001 0000 0010


W
B
0100 0101 0100 0110

Figure 11.1: 4-bit region codes (in TBRL format).

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

non-trivial case 0000


0000 0000
outpoint
trivial accept
replaced
0010
trivial accept 0000

Figure 11.2: 3 cases in Cohen-Sutherland algorithm.


11.1 Line Clipping using Region Codes 63

typedef int rCode;


const int INSIDE = 0; // 0000
const int LEFT = 1; // 0001
const int RIGHT = 2; // 0010
const int BOTTOM = 4; // 0100
const int TOP = 8; // 1000

rCode Compute_rCode(double x, double y){


rCode code = INSIDE; // initialized as being inside W
if (x < xmin) code |= LEFT;
else code |= RIGHT;
if (y < ymin) code |= BOTTOM;
else code |= TOP;
return code;
}

void CohenSutherlandLineClipAndDraw(double x0, double y0, double x1, double y1){


rCode rCode0 = Compute_rCode(x0, y0);
rCode rCode1 = Compute_rCode(x1, y1);
bool accept;
while (true) {
if (!(rCode0 | rCode1)) {
accept = true; break; }
else if (rCode0 & rCode1) {
accept = false; break;}
else {
double x, y;

// Find the outpoint -- it has non-zero rCode.


rCode rCodeOut = rCode0 ? rCode0 : rCode1;

// Find the intersection point.


if (rCodeOut & TOP) { // point is above W
x = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0);
y = ymax; }
else if (rCodeOut & BOTTOM) { // point is below W
x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
y = ymin; }
else if (rCodeOut & RIGHT) { // point is to the right of W
y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
x = xmax; }
else if (rCodeOut & LEFT) { // point is to the left of W
y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
x = xmin; }

// Move outpoint to intersection point for next pass.


if (rCodeOut == rCode0) {
x0 = x, y0 = y;
rCode0 = Compute_rCode(x0, y0); }
else {
x1 = x, y1 = y;
rCode1 = Compute_rCode(x1, y1); }
}
}
if (accept) {
DrawLineSegment(x0, y0, x1, y1); }
}

Figure 11.3: Pseudo-code of Cohen-Sutherland line clipping algorithm.


Chapter 11
64 Clipping

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

11.2 Polygon Clipping

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.

11.2.1 Sutherland-Hodgman Algorithm

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

Figure 11.5: Pseudo-code of Sutherland-Hodgman polygon clipping algorithm.

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.

The Weiler-Atherton algorithm overcomes the limitations of Sutherland-Hodgman algorithm by


returning a set of divided polygons, but is more complex and computationally more expensive;
so, Sutherland-Hodgman is used for many rendering applications. Sutherland-Hodgman can
also be extended into 3D space by clipping the polygon paths based on the boundaries of planes
defined by the viewing space.

11.2.2 Other Algorithms

Weiler-Atherton Algorithm 4 It allows clipping of a subject polygon by an arbitrarily shaped


clipping polygon/area/region. Although generally applied in 2D, it can be used in 3D through
visible surface determination and with improved efficiency through Z-ordering.5 The algorithm
4
Weiler, Kevin and Atherton, Peter: Hidden Surface Removal using Polygon Area Sorting, Computer Graphics,
11(2):214–222, 1977. URL: https://www.cs.drexel.edu/~david/Classes/CS430/HWs/p214-weiler.pdf.
5
Foley, James, Andries van Dam, Steven Feiner, and John Hughes: Computer Graphics: Principle and Practice.
Addison-Wesley Publishing Company. Reading, Massachusetts: 1987. pages 689–693.
11.3 3D Clipping 67

B
Two shapes A−B B−A A∪B A∩B A xor B
A and B

Figure 11.6: Object clipping as realized as boolean set-theoretic operations.

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

• difference—clipping polygons remove overlapping regions from the subject.


• union—clipping returns the regions covered by either subject or clip polygons.
• xor —clipping returns the regions covered by either subject or clip polygons except where
they are covered by both subject and clip polygons.

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

Figure 11.8: Frustum culling as 3D clipping.

©Piyush K Bhunre and Partha Bhowmick

Figure 11.9: 3D clipping on a triangulated object named mother&child containing 10 thousand


triangles with the rectilinear clip window translating along a principal axis in discrete steps.

You might also like