1
Lesson 6: Viewing &
Clipping
Shima Muhammad Qafor
UHD
College Of Science & Technology
Computer Science Department
Computer Graphics - Theory
Outline
2
Coordinate systems
World coordinate system.
Normalized coordinate system.
Device coordinate system.
Viewing
Clipping
Point clipping.
Line clipping.
Polygon clipping.
Coordinate Systems (1)
3
World coordinates (WCS): Any convenient Cartesian coordinate
system.
we select a “window” of this world for viewing.
Viewing coordinates: World coordinate positions are first
converted to viewing coordinates corresponding to the view we
want of a scene, based on the position and orientation.
Viewing coordinates are how we wish to present the view on the
output device.
Normalized coordinates are introduced to make viewing
process independent of any output device.
Each coordinate value is in the range from −1 to 1 or in the range
from 0 to 1, depending on the system
Clipping is usually and more efficiently done in these coordinates.
Device coordinates (DCS): The coordinate systems for display
devices are generally called device coordinates.
The Camera and the Scene
4
What does a camera do?
Takes in a 3D scene
Places (i.e., projects) the scene onto a 2D medium such as a roll
of film or a digital pixel array.
Coordinate Systems (2)
5
Windowing & Clipping
6
A section of a two-dimensional scene that is selected for display is
called a “clipping window”
The clipping window selects what we want to see.
Viewport indicates where and in what size it is to be viewed on the output
device.
The process for not showing the part of the drawing which one is not
interested is called “clipping”.
What do you want to see? Where do you want to see it?
Viewing Transformation
7
The mapping of a WCS to DCS is called viewing transformation
Normalization transformation
Workstation transformation.
Normalization transformation Workstation transformation
2D Clipping
8
The procedure that eliminates those portions of a picture
that are either inside or outside of a specified region of
space is referred to as a clipping algorithm or simply
clipping.
Usually, clipping region is a rectangle in standard position, with the
rectangle edges parallel to the coordinate axis; although we could use any
shape for a clipping application.
before after
Clipping Applications
9
The most common applications of clipping:
Clipping is applied to extract a designated portion of a
scene (either two-dimensional or three-dimensional) for
display on an output device.
Clipping methods are also used:
to antialias object boundaries, to construct objects using solid-
modeling methods.
to manage a multi-window environment, and
to allow parts of a picture to be moved, copied, or erased in
drawing and painting programs.
Primitive Clipping
10
Point Clipping
Line Clipping
Polygon Clipping
Point Clipping
11
If the edges of clipping window are:
xwmin , xwmax , ywmin , ywmax
a point (x, y) is not clipped if:
xwmin x xwmax
ywmin y ywmax
otherwise it is clipped
Line Clipping
12
Examine the end-points of each line to see if they
are inside the window or not:
Situation Solution Example
Both end-points inside the window Don’t clip
One end-point inside the window,
Must clip
one outside
Both end-points outside the
Don’t know!
window
Line Clipping Algorithm:
Cohen–Sutherland Line Clipping (1)
13
Divide the plane into nine regions.
Assign a unique 4-bit binary number to each region (outcode).
Outcode region: each bit position is used to indicate whether the
point is inside or outside one of the clipping-window boundaries.
4 3 2 1 1001 1000 1010
y ymax
top bottom right left
0000
0001 0010
Window
Region Code Legend y ymin
0101 0100 0110
x x min x xmax
Line Clipping Algorithm:
Cohen–Sutherland Line Clipping (2)
14
The logical AND operation of the endpoints determines if the line is completely
outside of the window:
if the logical AND of the endpoint codes is not zero, the line can be rejected
(completely outside).
if the logical AND operation is 0000, the line could not be reject
The logical OR operation of the endpoints determines if the line is completely
inside of the window:
if the logical OR of the endpoint codes is zero, the line can be accepted
(completely inside).
if the logical OR operation would not be 0000 , the line can not be accepted
Line Clipping Algorithm:
Cohen–Sutherland Line Clipping (3)
15
J
B D
O1 = outcode (x1, y1) I
F
A C
O2 = outcode (x2, y2) E
AB O1 = O 2 = 0 Inside the window
CD O1 ≠ 0, O2 = 0 Partially inside the window. Find intersection point and repeat
(Or vice versa)
EF O1 & O2 ≠ 0 Outside the window
IJ O1 or O2 ≠ 0 Partially inside the window. Find intersection point and repeat
Example 1: Cohen–Sutherland Line Clipping (1)
16
0101
1010
Example 1: Cohen–Sutherland Line Clipping (2)
17
0101
0010
Example 1: Cohen–Sutherland Line Clipping (3)
18
0001
0010
Example 1: Cohen–Sutherland Line Clipping (4)
19
0001
0000
Example 1: Cohen–Sutherland Line Clipping (5)
20
0000
0000
Example 2: Cohen–Sutherland Line Clipping (1)
21
Use the Cohen-Sutherland Clipping Algorithm to clip two of
the following lines against the clipping window’s boundaries:
Example 2: Cohen–Sutherland Line Clipping (2)
22
Solution:
Line AB: outcode(A) & outcode(B) = 0001 & 0100 = 0000 Intersection point = A`
Line A`B: outcode(A’) & outcode(B) = 0000 & 0100 = 0000 Intersection point = B`
Line A`B`: outcode(A`) = outcode(B`) = 0000 A`B` is inside the clipping window
Line CD: outcode(C) & outcode(D) 1000 & 0100 = 0000 Intersection point = C`
Line C`D: outcode(C’) & outcode(D) = 0000 & 0100 = 0000 Intersection point = D`
Line C`D`: outcode(C`) = outcode(D`) = 0000 C`D` is inside the clipping window
Line EF: outcode(E) & outcode(F) 0010 & 0100 = 0000 Intersection point = E`
Line E`F: outcode(E’) & outcode(F) = 0110 & 0100 = 0100 E`F is outside the clipping window
Cohen–Sutherland Line Clipping –
Calculating Line Intersection
23
Consider a line with the end-points (x1, y1) and (x2, y2).
m is the slope of the line, and can be calculated as:
m = (y2 – y1) / (x2 – x1)
Intersection points with the clipping boundaries are
calculated using the line-equation parameters:
The intersection with vertical boundary (x = xwmin or x =
xwmax), the y coordinate can be calculated as:
y = y1 + m(x – x1)
The intersection with a horizontal boundary (y = ywmin or y =
ywmax.), the x coordinate can be calculated as
x = x1 + (y – y1) / m
Example 3:
24
The line is partially inside the window.
Find m
m = 1/3
The value of m is within the range from 0 to 1, the line segment intersects
with the boundary xwmax
Solve y by substitute m in y = y1 + m ( x – x1)
x = xwmax = 5
y = 2 + 1/3 (5 – 4) = 2.33
Intersection point at xwmax is (5, 2.33)
Homework (1)
25
1. Use Sutherland and Cohen out code Algorithm to clip
two lines P1 (40, 50), P2 (75, 45) and P3(70, 20),
P4(100,10) against a window A(50, 10), B(80, 10),
C(80, 40) and D(50, 40).
2. Clip the line P1(-3/2, 1/6) and P2(1/2, 3/2) against
window A(-1, -1), B(-1, 1), C(1, 1) and D(1, -1) using
Sutherland and Cohen Subdivision Algorithm
Homework (2)
26
Polygon Clipping
27
Polygons are just composed of lines. Why do we need to
treat them differently?
Need to keep track of what is inside
A line clipper would often produce a disjoint set of lines
how to form a closed boundary around the clipped fill area?
Polygon
Lines
Sutherland-Hodgeman Polygon Clipping
28
Clip a polygon by processing the polygon boundary as
a whole against each window edge.
Clip the entire polygon with one edge
Clipthe output polygon against the next edge
Repeat for all edges
References
29
Hearn Baker Carithers, "Computer Graphics with OpenGL", 4th
Ed., Ch. 2, P. 30, and Ch. 6 P. 227.
Edward Angel and Dave Shreiner, “Interactive computer
graphics: A Top-Down Approach With Shader-Based OpenGL”,
6th Ed., 2012, Ch.2, P. 73, Ch. 4 P. 195.
30