0% found this document useful (0 votes)
9 views62 pages

Chapter 6 - Two-Dimensional Viewing

Uploaded by

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

Chapter 6 - Two-Dimensional Viewing

Uploaded by

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

CHAPTER 6:

VIEWING IN 2D
CONTENTS

 Windowing Concepts
 Clipping
 Introduction
 Brute Force
 Cohen-Sutherland Clipping Algorithm

 Area Clipping
 Sutherland-Hodgman Area Clipping Algorithm
WINDOWING I

 A scene is made up of a collection of objects specified in


world coordinates

World Coordinates
WINDOWING II

 When we display a scene only those objects within a


particular window are displayed

Window
wymax

wymin

wxmin wxmax
World Coordinates
WINDOWING III

 Because drawing things to a display takes time, we clip


everything outside the window

Window
wymax

wymin

wxmin wxmax
World Coordinates
WINDOW VS. VIEWPORT

 Window
 World-coordinate area selected for display
 What is to be viewed

 Viewport
 Area on the display device to which a window is mapped
 Where it is to be displayed
VIEWPORT TRANSFORMATION

ywmax Window Normalization


yvmax Viewport

(xw, yw) (xv, yv)


ywmin yvmin
xwmin xwmax xvmin xvmax
Screen Coordinates Image Coordinates

xxvv == xxvmin + (x – x wmin)) ** (x


vmin + (xww – xwmin (xvmax – x vmin)) // (x
vmax– xvmin (xwmax – x wmin))
wmax– xwmin
yyvv == yyvmin + (y – y wmin)) ** (y
vmin + (yww– ywmin (yvmax – y vmin)) // (y
vmax– yvmin (ywmax – y wmin))
wmax– ywmin
WORLD COORDINATES TO VIEWING PORT
COORDINATES
CLIPPING

 Avoid Drawing Parts of Primitives Outside Window

Image

Viewport

Screen
World Coordinates
POINT CLIPPING

 Is Point(x,y) Inside the Clip Window?

P4 Clipped
Clipped

Window P2
wymax Inside
Inside ::
Clipped
P5
P7
P1 (x>=
(x>= xxwmin ) &&
wmin) &&
Points Within the Window
are Not Clipped
P9 (x<=
(x<= xxwmax ) &&
wmax) &&
P8
wymin
(y>=
(y>= yywmin ) &&
wmin) &&
Clipped P10
(y<=
(y<= yywmax
wmax)
)
wxmin wxmax
LINE CLIPPING

 For the image below consider which lines and points


should be kept and which ones should be clipped.

Before Clipping After Clipping


LINE CLIPPING

 Harder - examine the end-points of each line to see if they


are in the window or not

Situation Solution Example

Both end-points inside


Don’t clip
the window

One end-point inside


the window, one Must clip
outside

Both end-points
Don’t know!
outside the window
BRUTE FORCE LINE CLIPPING

Brute force line clipping can be performed as follows:

 For lines with both end-points

within the window Don’t clip

 For lines with one end-point inside

the window and one end-point


Outside calculate the intersection point
(using the equation of the line) and clip from this point out

 For lines with both end-points outside the window test


the line for intersection with all of the window
boundaries, and clip appropriately
INTERSECTION POINTS

 Intersection points (x, y) of a line of endpoints


p1(x1, y1) and p2(x2, y2):

 Vertical Intersection

y = y1 + m(x - x1)
x = xwmin or xwmax
 Horizontal Intersection

x = x1 + (y - y1)/m
y = ywmin or ywmax
BRUTE FORCE LINE CLIPPING (CONT…)

 The brute force algorithm is much too slow


o calculating line intersections is computationally
expensive, because a scene can contain so many lines

 Cohen-Sutherland clipping algorithm: an efficient


line clipping algorithm
oThe key advantage is that it vastly reduces the
number of line intersections that must be calculated
COHEN-SUTHERLAND: WORLD DIVISION

Worldspace 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
above below right left 0001 0010
Window
Region Code Legend
0101 0100 0110
LINE CLIPPING
 Use Simple Tests to Classify Easy Cases First
 Divide the area into regions represented by Bit Codes, and give each
endpoint a bit code.

1001 P7 1000 1010


Bit 4
P1
P4 P8
0001 P3 0010
0000
P2
P6
Bit 3
0101 P5 0100 0110
Bit 1 Bit 2
LINE CLIPPING

 If both endpoints has the bit value ‘1’ in the same bit
position, then the line is completely outside

1001 P7 1000 1010


Bit 4
P1
P4 P8
0001 P3 0000 0010
P2
P6
Bit 3
0101 P5 0100 0110
Bit 1 Bit 2
LINE CLIPPING

 If both endpoints has the bit code 0000, then the line
is completely inside

1001 P7 1000 1010


Bit 4

P4 P8
0001 P3 0000 0010

P6
Bit 3
0101 P5 0100 0110
Bit 1 Bit 2
LINE CLIPPING

 Otherwise,
Compute Intersections with Window
Boundary for Lines
1001 P7 1000 1010
Bit 4

P4 P8
0001 P3 0000 0010

P6
Bit 3
0101 P5 0100 0110
Bit 1 Bit 2
COHEN-SUTHERLAND: OTHER LINES

Lines that cannot be identified as completely inside or


outside the window may or may not cross the window
interior
These lines are 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
COHEN-SUTHERLAND: OTHER LINES
(CONT…)

 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
 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
LINE CLIPPING

1001 P7 1000 1010


Bit 4

P4 P8
0001 P3 0000 0010

P6
Bit 3
0101 P5 0100 0110
Bit 1 Bit 2
LINE CLIPPING

1001 P7 1000 1010


Bit 4

P4 P8
0001 P3 0000 0010

P6
Bit 3
0101 P’5 0100 0110
Bit 1 Bit 2
LINE CLIPPING

1001 P7 1000 1010


Bit 4

P4 P8
0001 P3 0000 0010

P6
Bit 3
0101 P’5 0100 0110
Bit 1 Bit 2
LINE CLIPPING

1001 1000
P’7 1010
Bit 4

P4 P’8
0001 P3 0000 0010

P6
Bit 3
0101 P’5 0100 0110
Bit 1 Bit 2
LINE CLIPPING

1001 1000
P’7 1010
Bit 4

P4 P’8
0001 P3 0000 0010

P6
Bit 3
0101 P’5 0100 0110
Bit 1 Bit 2
COHEN-SUTHERLAND EXAMPLES

 Consider the line P9 to P10 below:

 Start at P10 Window


wymax
 From the region codes of the two
end-points we know the line
doesn’t cross the left or right P9 [0000]
boundary wymin
P10’ [0000]
 Calculate the intersection of the
line with the bottom boundary to P10 [0100]

generate point P10’ wxmin wxmax


 The line P9 to P10’ is completely
inside the window so is retained
COHEN-SUTHERLAND EXAMPLES

 Consider the line P7 to P8

 Start at P7
Window
wymax
 From the two region
codes of the two end- P7’ [0000]
points we know the line P7 [0001]
P8’ [0000] P [0010]
crosses the left boundary wymin
8

so calculate the
intersection point to
generate P7’
wxmin wxmax
COHEN-SUTHERLAND EXAMPLES

 Consider the line P7’ to P8

 Start at P8
Window
wymax
 Calculate the intersection
with the right boundary to P7’ [0000]
P8 [0010]
generate P8’ P7 [0001]
P8’ [0000]
 P7’ to P8’ is inside
the wymin
window so is retained

wxmin wxmax
AREA CLIPPING

Similarly to lines, areas must be


clipped to a window boundary

Consideration must be taken as


to which portions of the area
must be clipped
SUTHERLAND-HODGMAN AREA
CLIPPING ALGORITHM

A technique for clipping areas developed by Sutherland


& Hodgman

 Put simply the polygon is clipped by comparing it against


each boundary in turn
SUTHERLAND-HODGMAN AREA
CLIPPING ALGORITHM (CONT…)

 To clip an area against an individual boundary:

 Consider each vertex in turn against the boundary


 Vertices inside the boundary are saved for clipping against the
next boundary
 Vertices outside the boundary are clipped
 If we proceed from a point inside the boundary to one outside,
the intersection of the line with the boundary is saved
 If we cross from the outside to the inside intersection point and
the vertex are saved
CASE 1: BOTH V1 & V2 OUT

OUT IN

Out OUT V1

Action: nothing
Polygon
being
clipped

V2
Clip boundary
CASE 2: BOTH V1 & V2 IN

OUT IN
V2
IN IN

Action: save V2

V1
CASE 3: V1 IN & V2 OUT

OUT IN
V1
IN OUT V2
V1’

Action: save V1’


CASE 4: V1 OUT & V2 IN

OUT IN

Out IN

Action: save V1’ V2

V2
V1’
V1
HODGEMAN-SUTHERLAND

v1 v2
v4

v3
v5 v6

v8 v7
HODGEMAN-SUTHERLAND

v1 v2
v4

v3
v5 v6

v8 v7
HODGEMAN-SUTHERLAND: TOP

1 2
4 3
v3
v5 v6

v8 v7
HODGEMAN-SUTHERLAND:TOP

(v1 v 2 v3 v 4 v5 v6 v7 v8) 

(1  2 v3  3  4 v5 v6 v7 v8)
HODGEMAN-SUTHERLAND: BOTTOM

1 2
4 3
v3
v5 v6

v8 v7
HODGEMAN-SUTHERLAND: BOTTOM

1 2
4 3
v3
v5 v6

5 6
HODGEMAN-SUTHERLAND: BOTTOM

(1  2 v3  3  4 v5 v6 v7 v8) 

( 51  2 v3  3  4 v5 v6  6)
HODGEMAN-SUTHERLAND: RIGHT

1 2
4 3
v3
v5 v6

5 6
HODGEMAN-SUTHERLAND: RIGHT

This case is trivial: do nothing

( 51  2 v3  3  4 v5 v6  6) 

( 51  2 v3  3  4 v5 v6  6)
HODGEMAN-SUTHERLAND: LEFT

1 2
4 3
v3
v5 v6

5 6
HODGEMAN-SUTHERLAND: LEFT

8 2
9 3
v3
10
v6
7 6
HODGEMAN-SUTHERLAND: LEFT

( 51  2 v3  3  4 v5 v6  6) 

( 7  8  2 v3  3  910 v6  5)
HODGEMAN-SUTHERLAND: DONE

Extra
Lines
EXTENDED H-S ALGORITHM

 Tag the different new vertices


 Call  the one generated on transition from IN
to OUT
 Call  the one generated on transition from

OUT to IN
 Starting
from OUT, connect  and  as soon
as  is generated
EXAMPLE OF H-S ALGORITHM

OUT IN


HODGEMAN-SUTHERLAND: DONE
SUTHERLAND-HODGMAN EXAMPLE

P
S

S
P I

P S
Save Point I No Points Saved
Save Point P

S
I P

Save Points I & P


WEILER-ATHERTON POLYGON CLIPPING

 Another approach to polygon clipping


 No extra clipping outside window
 Works for arbitrary shapes
 Avoids degenerate polygons
WEILER-ATHERTON CLIPPING

• Start at first (inside) vertex


• Traverse polygon until hitting a window boundary

Output intersection pointi
• Turn right
• Follow window boundary until next intersection
• Output second intersection
• Turn right, again, and follow subject polygon until
closed
• Continue on subject polygon from first intersection
point.
• Repeat processing until complete
WEILER-ATHERTON CLIPPING

13

14
4 2
3
5 1

12 6

9 Clip
7 Polygon (ep)
Subject Polygon 8
10
(sp)
11
WEILER-ATHERTON CLIPPING
OTHER AREA CLIPPING CONCERNS

 Clipping concave areas can be a little more tricky as often


superfluous lines must be removed .

Clipping curves requires more work, e.g., for circles we must


find the two intersection points on the window boundary.
TEXT CLIPPING

 All-or-None String Clipping


TEXT CLIPPING

 All-or-None Character Clipping


End of Chapter

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy