0% found this document useful (0 votes)
49 views37 pages

Clipping

Uploaded by

daniellatumaku9
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)
49 views37 pages

Clipping

Uploaded by

daniellatumaku9
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/ 37

Clipping

204481 Foundation of Computer Graphics November 11, 2024 1


Clipping

 Clipping means
 Identifying portions of a scene that are inside (or outside) a
specified region
 Examples
 Multiple viewports on a device

 Deciding how much of a games world the player can see

204481 Foundation of Computer Graphics November 11, 2024 2


Clipping

 Clipping means
 Identifying portions of a scene that are inside (or outside) a
specified region
 Examples
 Multiple viewports on a device

 Deciding how much of a games world the player can see

Player can’t see


this far yet

204481 Foundation of Computer Graphics November 11, 2024 3


 The primary use of clipping in computer graphics is
to remove objects, lines or line segments that are
outside the viewing volume

204481 Foundation of Computer Graphics November 11, 2024 4


Requirements for clipping

 Is (x, y) inside or outside a given region

 For 2D graphics the region defining what is to be


clipped is called
 The clip window

Clip window

204481 Foundation of Computer Graphics November 11, 2024 5


Interior and exterior clipping

 interior clipping
 what is to be saved is inside the clip window
 exterior clipping
 what is to be saved is outside clip window

Interior clipping
- keep point P2
P2
(x2, y2)

204481 Foundation of Computer Graphics November 11, 2024 6


Interior and exterior clipping

Exterior clipping Clip window


- keep point P1
P1
(x1, y1)

 We shall assume interior clipping for now



But you must be aware of exterior clipping too

204481 Foundation of Computer Graphics November 11, 2024 7


Overview of types of clipping

 All-or-none clipping
 If any part of object outside clip window
whole object is rejected

 Point clipping
 Only keep points inside clip window
 Line clipping
 Only keep segment of line inside clip window
 Polygon clipping
 Only keep sub-polygons inside clip window

204481 Foundation of Computer Graphics November 11, 2024 8


Before and after POINT clipping

 Before P2

P3 P1
P4

 After

P3 P1

204481 Foundation of Computer Graphics November 11, 2024 9


Before and after LINE clipping

 Before P2

 After P1

P2’

P1’

204481 Foundation of Computer Graphics November 11, 2024 10


Before and after POLYGON clipping

 Before

 After

204481 Foundation of Computer Graphics November 11, 2024 11


Cohen-Sutherland 2D clipping

 4 regions are defined – outside the clip window


 TOP
 BOTTOM
 LEFT
 RIGHT P2

P1

204481 Foundation of Computer Graphics November 11, 2024 12


The 4-bit codes

 A 4-bit code is assigned to each point


 This code is sometimes called a Cohen-
Sutherland region code
 LEFT bit 1 = binary 0001
 RIGHT bit 2 = binary 0010
 BOTTOM bit 3 = binary 0100
 TOP bit 4 = binary 1000

 So a point coded 0001 is LEFT of the clip window, etc.

204481 Foundation of Computer Graphics November 11, 2024 13


Cohen-Sutherland region codes

 The point (65, 50) is above and to the right of the


clip window,
 Therefore gets the code: 1010
(65, 50)

(10, 40)
(60, 40)

(10, 10) (60, 10)

204481 Foundation of Computer Graphics November 11, 2024 14


Cohen-Sutherland region codes

 1000 indicates the TOP region


 0010 indicates the RIGHT region
 the 4 bit Cohen-Sutherland code
is formed by a logical OR of all region codes

 1000 TOP
 OR 0010 RIGHT
 ------------------
 = 1010

204481 Foundation of Computer Graphics November 11, 2024 15


How the codes are useful
 Why bother encoding points with 4-bit Cohen-Sutherland
region codes?
 Can make some quick decisions
 If code is zero (0000) point is inside window
 If code is non-zero point is outside window

 For the two endpoints of a line


 If codes for both endpoints are zero,
whole line is inside window
 If (logical) AND of codes for endpoints is non-zero,
the endpoints must have a region in common …
So the whole line must be outside clip window

204481 Foundation of Computer Graphics November 11, 2024 16


Some lines and their endpoint codes
TOP BOTTOM RIGHT LEFT

 Line1 codes are 1001 and 0000


 Line2 codes are 0000 and 0000 << inside
 Line3 codes are 1000 and 0100
 Line4 codes are 1010 and 0110 << outside
Line1

Line4
Line2

Line3
204481 Foundation of Computer Graphics November 11, 2024 17
Clipping from region codes

 To clip a line based on region codes:


 Choose an endpoint that is OUTSIDE clip window (I.e.
non-zero code)
 Check first bit (TOP) – if set, clip intersection from point
to TOP of clip window
 Check second bit (BOTTOM) and so on

204481 Foundation of Computer Graphics November 11, 2024 18


Line Clipping Algorithms

 Goal: avoid drawing primitives that are outside the


viewing window.
 The most common case: clipping line segments
against a rectangular window

204481 Foundation of Computer Graphics November 11, 2024 19


Line Clipping Algorithms

 Three cases:
 Segment is entirely inside the window - Accept
 Segment is entirely outside the window - Reject
 Segment intersects the boundary - Clip

204481 Foundation of Computer Graphics November 11, 2024 21


Point Classification

 How can we tell if a point is inside a rectangular


window?

ymax

ymin
xmin xmax

204481 Foundation of Computer Graphics November 11, 2024 22


Line Segment Clipping

 If both endpoints are inside the window, the entire


segment is inside: trivial accept
 If one endpoint is inside, and the other is outside,
line segment must be split.
 What happens when both endpoints are outside?

204481 Foundation of Computer Graphics November 11, 2024 23


Cohen-Sutherland Algorithm

 Assign a 4-digit binary outcode to each of the 9


regions defined by the window:

1001 1000 1010


ymax

0001 0000 0010


ymin
0101 0100 0110
xmin xmax
204481 Foundation of Computer Graphics November 11, 2024 24
Cohen-Sutherland Algorithm

1001 1000 1010 bit condition


ymax
1 y > ymax
0001 0000 0010
2 y < ymin
ymin
3 x > xmax
0101 0100 0110
4 x < xmin
xmin xmax

204481 Foundation of Computer Graphics November 11, 2024 25


Clipping a line

 Choose point P2 (code is 1000)


P2

P1
 Clip from point to TOP of clip window

P1
204481 Foundation of Computer Graphics November 11, 2024 26
Clipping a line

 Choose point P1 (code is 0110)

 Bit 1 (TOP) not set P1


 Bit 2 (BOTTOM) set – so clip bottom

204481 Foundation of Computer Graphics November 11, 2024 27


Clipping a line

 Bit 3 (RIGHT) set – so clip right

 Bit 4 (LEFT) not set – so clipping finished

204481 Foundation of Computer Graphics November 11, 2024 28


Text clipping
 strategies for dealing with text objects
 Consider following example:

 Assume interior
clipping

ABC

Clip window

204481 Foundation of Computer Graphics November 11, 2024 29


Student Exercise

 Draw three possible results of interior


clipping of the string “ABC”

 Try to name what type


of clipping you have
done in each case
ABC

Clip window
204481 Foundation of Computer Graphics November 11, 2024 30
Student Exercise

 All-or-none string clipping


 No text is retained after clipping
since part of text is outside clip window
 Whole String “ABC” is clipped as a single object

Before clipping After clipping

ABC

Clip window Clip window


204481 Foundation of Computer Graphics November 11, 2024 31
Student Exercise

 All-or-none character clipping


 Only “B” and “C” are retained after clipping
since part of “A” is outside clip window
 Each character is clipped as a separate object
Before clipping After clipping

ABC BC

Clip window Clip window

204481 Foundation of Computer Graphics November 11, 2024 32


Student Exercise

 Character component clipping


 All of “B” and “C” are retained, and those parts
of “A” inside clip window are also retained
 Each component of each character is clipped as a separate
object
Before clipping After clipping

ABC ABC

Clip window Clip window

204481 Foundation of Computer Graphics November 11, 2024 33


Summary – clipping in general

 Clipping means
 Identifying portions of a scene that are inside (or
outside) a specified region
 Clipping is defined using a clip window
 Interior clipping keep things inside clip window /
Exterior cl. outside

204481 Foundation of Computer Graphics November 11, 2024 34


 Types of clipping
 All-or-none (AON) clipping

if any part of object outside clip area then
reject whole object
 Point / line / polygon / text clipping (AON
string/char, component)

204481 Foundation of Computer Graphics November 11, 2024 35


 3D clipping involves defining a clip volume
 A finite volume of space defined by front and
back clipping planes, the view plane window and
projection method
(more details in lecture on 3D)

204481 Foundation of Computer Graphics November 11, 2024 36


Summary

 A 4-bit encoding for regions related to clip window


 TOP
 BOTTOM
 LEFT
 RIGHT
 Code can be used to easily
 To determine if a point in inside clip window
 If endpoints of a line mean whole line
is fully inside or fully outside clip window
 Can implement in C++ using char
 8-bit, unsigned, data type

204481 Foundation of Computer Graphics November 11, 2024 37

You might also like