0% found this document useful (0 votes)
28 views48 pages

05 Viewing and Clipping

The document discusses coordinate systems, viewing transformations, and 2D clipping in computer graphics. It describes how objects in a world coordinate system are mapped to a screen coordinate system through transformations like panning, zooming, and scaling. 2D clipping is defined as the process of determining which graphic elements lie inside the clipping window and should be displayed by filtering out elements that fall outside the boundary of the window.

Uploaded by

sefefe hunegnaw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views48 pages

05 Viewing and Clipping

The document discusses coordinate systems, viewing transformations, and 2D clipping in computer graphics. It describes how objects in a world coordinate system are mapped to a screen coordinate system through transformations like panning, zooming, and scaling. 2D clipping is defined as the process of determining which graphic elements lie inside the clipping window and should be displayed by filtering out elements that fall outside the boundary of the window.

Uploaded by

sefefe hunegnaw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Computer graphics

2D Viewing and Clipping

1
Coordinate Systems
• World Coordinate System (Object Space)
– Representation of an object measured in in some
physical unit
• Drawing dimensions may be in meters, km, feet, etc.
– Measuring values are floating point
– Limits are flexible
W.T
• Window
– The rectangular region that
defines which part of the real world
W.B
is visible (to be displayed)

W.L W.R
2
Coordinate Systems cont’d

• Screen Coordinate System


(Image Space)
– The space with in the image is
displayed
– Measuring values are integer
– Limits are device dependent
y
• Viewport
– Rectangular region in the screen x
used to display drawing (0,0)

– Defined in screen coordinate


system (1,1)
3
Viewing Transformation
• We denote the boundaries of the world window by four
real values xwmin, ywmin , xwmax, ywmax denoting its left,
bottom, right, top margins respectively
• Similarly, the boundaries of the viewport on the screen are
defined by four integer values xvmin, yvmin, xvmax, yvmax.
• When a graphics display is generated using arbitrary
coordinates on the world window, the important problem
encountered in viewing this display on the actual viewport on the
screen is to have a function which maps every point (x, y) on the
window to a corresponding point (xv, yv) on the viewport.
• The following window to viewport transformation
achieves this relationship.

4
Viewing Transformation cont’d

• Window to viewport transformation is process of mapping


image from a window in world coordinate system to
viewport in screen coordinate system (device coordinate
system )

(xwmax, ywmax)

(x, y) (xvmax, yvmax)

(xv, yv)

(xwmin,ywmin)
(xvmin, yvmin)

5
Viewing Transformation cont’d

Steps 1: choose the window in the world


coordinate system
World: Screen:

Clipping window Viewport


ywmax yvmax

ywmin yvmin

xwmin xwmax xvmin xvmax

Clipping window: Viewport:


What do we want to see? Where do we want to see it?
6
Viewing Transformation cont’d

Window to viewport transformation may involve panning,


zooming, rotation…etc.
Panning moving of the window about the world coordinate
World: Screen:

Clipping window Viewport


ywmax yvmax

ywmin yvmin

xwmin xwmax xvmin xvmax

Clipping window:
Panning… 7
Viewing Transformation cont’d

Panning moving of the window about the world coordinate

World: Screen:

Clipping window Viewport


ywmax yvmax

ywmin yvmin

xwmin xwmax xvmin xvmax

Clipping window:
Panning…
8
Viewing Transformation cont’d

Zooming: changing of the window size (as the widow gets


larger, the image on the viewport gets smaller and vice
versa)

World: Screen:

Viewport
ywmax yvmax

ywmin yvmin

xwmin xwmax xvmin xvmax

Clipping window:
Zooming…
9
Viewing Transformation cont’d

Zooming: changing of the window size (as the widow gets


larger, the image on the viewport gets smaller and vice
versa)

World: Screen:
ywmax Viewport
yvmax

yvmin
ywmin
xwmin xwmax xvmin xvmax

Clipping window:
Zooming… 10
Viewing Transformation cont’d

Step 2: Clipping window, clip object to the size of the


window
Clipping window
ywmax
ywmax  ywmin
ywmin

xwmin xwmax xwmax xwmin

Step 3: Translate to the origin

T xwmin , ywmin 

11
Viewing Transformation cont’d

Step 4: Normalize coordinate, Scale to the size of viewport

ywmax -ywmin yvmax -yvmin

xwmax-xwmin xvmax-xvmin
• The transformation from the
Scale with : window to viewport is said to
 xvmax  xvmin yvmax  yvmin  preserve the aspect ratio, if
S ,  aspect ratio of a world window
 xwmax  xwmin ywmax  ywmin  and viewport are same.
If the two scale factors are unequal, • In such a case, the mapping
from the window to the
then the aspect - ratio changes : distortion !
viewport is distortion-free.
12
Viewing Transformation cont’d

Step 5: translate to the proper position on the screen


coordinate system 1 Viewport
yvmax
yvmax -yvmin
yvmin

xvmax-xvmin xvmin xvmax 1

Translate with :
Txvmin , yvmin 
All together :
 xvmax  xvmin yvmax  yvmin 
T( xvmin , yvmin )S , T( xwmin , ywmin )
 xwmax  xwmin ywmax  ywmin 
13
2D Clipping
A scene is made up of a collection of objects specified in world
coordinates

World Coordinates
14
2D Clipping
When we display a scene only those objects within a particular
window are displayed
Window
wymax

wymin

wxminWorld Coordinates
wxmax
15
2D Clipping
Because drawing things to a display takes time we clip
everything outside the window
Window
wymax

wymin

wxmin wxmax
16
World Coordinates
2D Clipping
1.1 Definition:
– Clipping is the process of determining which elements of
the picture lie inside the window and are visible.

– By default, the “clip window” is the entire canvas


• not necessary to draw outside the canvas
• for some devices, it is damaging (plotters)
• \
– Sometimes it is convenient to restrict the “clip window” to
a smaller portion of the canvas
• partial canvas redraw for menus, dialog boxes, other obscuration

17
2D Clipping
1.2 Shielding:
– Shielding or exterior clipping is the reverse operation of
clipping where window act as the block used to abstract the
view.

– Examples
• A multi view window system
• The design of page layouts in advertising or publishing
applications or for adding labels or design patterns to
picture.
• Combining graphs, maps o schematics
18
2D Clipping

Window
wymax

wymin

wxminWorld Coordinates
wxmax
19
2D Clipping
1.3 Example:
For the image below consider which lines and points should be kept
and which ones should be clipped against the clipping window
P4

Window P2
wymax P6
P3
P1
P7 P5

P9
P8
wymin
P10

20
wxmin wxmax
2D Clipping
1.4 Applications:
– Extract part of a defined scene for viewing.
– Drawing operations such as erase, copy, move etc.
– Displaying multi view windows.
– Creating objects using solid modeling techniques.
– Anti-aliasing line segments or object boundaries.
– Identify visible surfaces in 3D views.

21
2D Clipping
1.5 Types of clipping:
– Three types of clipping techniques are used depending
upon when the clipping operation is performed

a. Analytical clipping
– Clip it before you scan convert it
– used mostly for lines, rectangles, and polygons, where
clipping algorithms are simple and efficient

22
2D Clipping

b. Scissoring
– Clip it during scan conversion
– a brute force technique
• scan convert the primitive, only write pixels if inside the
clipping region
• easy for thick and filled primitives as part of scan line fill
• if primitive is not much larger than clip region, most pixels will
fall inside
• can be more efficient than analytical clipping.

23
2D Clipping

c. Raster Clipping
– Clip it after scan conversion
– render everything onto a temporary canvas and copy
the clipping region
• wasteful, but simple and easy,
• often used for text

24
2D Clipping
Foley and van Dam suggest the following:
– for floating point graphics libraries, try to use analytical
clipping
– for integer graphics libraries
• analytical clipping for lines and polygons
• others, do during scan conversion
– sometimes both analytical and raster clipping
performed

25
2D Clipping
1.6 Levels of clipping:
– Point Clipping
– Line Clipping
– Polygon Clipping
– Area Clipping
– Text Clipping
– Curve Clipping

26
Point Clipping
– Simple and Easy P4 Clipped
Clipped
– a point (x,y) is not clipped if:
Window P2
wxmin ≤ x ≤ wxmax wymax
Clipped
P5
P1
& P 7 Points Within the Window
are Not Clipped
wymin ≤ y ≤ wymax P9 P8
wymin
P10
– otherwise it is clipped
Clipped

wxmin wxmax

27
Line Clipping
– It is Harder than point
clipping
– We first examine the end- ymax
points of each line to see
if they are in the window
or not
• Both endpoints inside, line ymin
trivially accepted
• One in and one out, line is
partially inside
• Both outside, might be
partially inside xmin xmax
• What about trivial cases?

28
Line Clipping
Situation Solution Example

Both end-points inside


Don’t clip
the window

One end-point inside the


Must clip
window, one outside

Both end-points outside


Don’t know!
the window

29
2D Line Clipping Algorithms
1. Analytical Line Clipping
2. Cohen Sutherland Line Clipping
3. Liang Barsky Line Clipping

30
Cohen-Sutherland Line Clipping
– An efficient line clipping
algorithm
– The key advantage of the
algorithm is that it vastly
reduces the number of line
intersections that must be Dr. Ivan E. Sutherland co-
calculated. d e v e lo p e d th e C o h e n -
Sutherland clipping
algorithm. Sutherland is a
graphics giant and includes
amongst his achievements
Cohen is something of a mystery – can the invention of the head
anybody find out who he was? m o u n t e d d i s 31 play.
Cohen-Sutherland Line Clipping
– Two phases Algorithm

Phase I: Identification Phase


All line segments fall into one of the following categories
1. Visible: Both endpoints lies inside
2. Invisible: Line completely lies outside
3. Clipping Candidate: A line neither in category 1 or 2

Phase II: Perform Clipping


Compute intersection for all lines that are candidate for
clipping.
32
Cohen-Sutherland Line Clipping
Phase I: Identification Phase: World space is divided into regions
based on the window boundaries
– Each region has a unique four bit region code
– Region codes indicate the position of the regions with respect to the
window

1001 1000 1010

3 2 1 0 0000
0001 0010
above below right left Window

Region Code Legend 0101 0100 0110


33
Cohen-Sutherland Line Clipping
Every end-point is labelled with the appropriate region code

P11 [1010]
P4 [1000]

Window
wymax P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]

wxmin wxmax
34
Cohen-Sutherland Line Clipping
Visible Lines: Lines completely contained within the window
boundaries have region code [0000] for both end-points so are
not clipped

P11 [1010]
P4 [1000]

Window
wymax P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]

35
wxmin wxmax
Cohen-Sutherland Line Clipping
Invisible Lines: Any line with a common set bit in the region
codes of both end-points can be clipped completely
– The AND operation can efficiently check this
– Non Zero means Invisible
P11 [1010]
P4 [1000]

Window
wymax P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]

36
wxmin wxmax
Cohen-Sutherland Line Clipping
Clipping Candidates: Lines that cannot be identified as
completely inside or outside the window may or may not cross
the window interior. These lines are processed in Phase II.
– If AND operation result in 0 the line is candidate for clipping
P11 [1010]
P4 [1000]

Window
wymax P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]

37
wxmin wxmax
Cohen-Sutherland Line Clipping
Assigning Codes
– Let point (x,y) is be given P [1000] 4
P [1010] 11

code b3b2b1b0: Window


wymax
bit 3 = 1 if wymax - y ≤0
P [0000]6
P [0001]
3
P [0000]
5 P [0010] 12

bit 2 = 1 if y - wymin ≤ 0 P [0001]


7
P [0000]9 P [0010] 8

bit 1 = 1 if wxmax - x ≤0 wymin


P [0100] 10

bit 0 = 1 if x - wxmin ≤ 0
P [0101]
13 P [0110] 14

wxmin wxmax

38
Cohen-Sutherland Clipping
Algorithm
Phase II: Clipping Phase: Lines that are in category 3 are
now processed as follows:
– Compare an end-point outside the window to a
boundary (choose any order in which to consider
boundaries e.g. left, right, bottom, top) and determine
how much can be discarded
– If the remainder of the line is entirely inside or outside
the window, retain it or clip it respectively
– Otherwise, compare the remainder of the line against
the other window boundaries
– Continue until the line is either discarded or a segment
inside the window is found
39
Cohen-Sutherland Line Clipping
• Intersection points with the window boundaries are
calculated using the line-equation parameters
– Consider a line with the end-points (x1, y1) and (x2, y2)
– The y-coordinate of an intersection with a vertical
window boundary can be calculated using:
y = y1 + m (xboundary - x1)
where xboundary can be set to either wxmin or wxmax
– The x-coordinate of an intersection with a horizontal
window boundary can be calculated using:
x = x1 + (yboundary - y1) / m
where yboundary can be set to either wymin or wymax 40
Cohen-Sutherland Line Clipping
• We can use the region codes to determine which window
boundaries should be considered for intersection
– To check if a line crosses a particular boundary we
compare the appropriate bits in the region codes of its
end-points
– If one of these is a 1 and the other is a 0 then the line
crosses the boundary.

41
Cohen-Sutherland Line Clipping
Example1: Consider the line P9 to P10 below
– Start at P10 Window
– From the region codes wymax
of the two end-points we
know the line doesn’t P [0000]
9
cross the left or right wymin P ’ [0000]
10
boundary
– Calculate the intersection P [0100]
10

of the line with the bottom boundary wxmin wxmax


to generate point P10’
– The line P9 to P10’ is completely inside the window so is
retained
42
Cohen-Sutherland Line Clipping
Example 2: Consider the line P3 to P4 below
– Start at P4 P ’ [1001]
4
4P [1000]

– From the region codes Window


wymax
of the two end-points P [0001]
3

we know the line


crosses the left
boundary so calculate wymin
the intersection point to
generate P4’
wxmin wxmax
– The line P3 to P4’ is completely outside the window so is
clipped
43
Cohen-Sutherland Line Clipping
Example 3: Consider the line P7 to P8 below
– Start at P7 Window
wymax
– From the two region
codes of the two P ’ [0000]
7
P [0001] P [0010]
end-points we know 7
P ’ [0000]
8
8
the line crosses the wymin
left boundary so
calculate the
intersection point to wxmin wxmax
generate P7’

44
Cohen-Sutherland Line Clipping
Example 4: Consider the line P7’ to P8
– Start at P8 Window
wymax
– Calculate the
intersection with the P ’ [0000]
7
P [0001] P [0010]
right boundary to 7
P ’ [0000]
8
8
generate P8’ wymin
– P7’ to P8’ is inside
the window so is
retained wxmin wxmax

45
Cohen-Sutherland Line Clipping
Mid-Point Subdivision Method
– Algorithm
1. Initialise the list of lines to all lines
2. Classify lines as in Phase I
i. Assign 4 point bit codes to both end points a3a2a1a0 and b3b2b1b0
ii. If (a3a2a1a0 = b3b2b1b0 = 0 )Line in category 1
iii. If (a3a2a1a0)AND (b3b2b1b0 ) # 0 ) Line in category 2
iv. If (a3a2a1a0)AND (b3b2b1b0 ) = 0 ) Line in category 3
3. Display all lines from the list in category 1 and remove;
4. Delete all lines from the list in category 2 as they are invisible;
5. Divide all lines of category 3 are into two smaller segments at mid-
point (xm,ym) where xm = (x1 +x2)/2 and ym = (y1 +y2)/2
6. Remove the original line from list and enter its two newly created
segments.
7. Repeat step 2-5 until list is null.

46
Cohen-Sutherland Line Clipping

Window
wymax

wymin

wxmin wxmax 47
Cohen-Sutherland Line Clipping
Mid-Point Subdivision Method

– Integer Version
– Fast as Division by 2 can be performed by simple shift
right operation
– For NxN max dimension of line number of
subdivisions required log2 N.
– Thus a 1024x1024 raster display require just 10
subdivisions………

49

You might also like