Sunteți pe pagina 1din 30

CSE 423 Computer Graphics

Clipping
• Cohen Sutherland Algorithm (Line)
• Cyrus-Back Algorithm (Line)
• Sutherland-Hodgeman Algorithm (Polygon)
• Cohen Sutherland Algorithm (3d)
Point Clipping
clip
rectangle y = ymax (xmax, ymax)

x = xmin x = xmax

(xmin, ymin) y = ymin

For a point (x,y) to be inside the clip rectangle:


xmin  x  xmax
ymin  y  ymax
Point Clipping
clip
rectangle y = ymax (xmax, ymax)

(x1, y1)
x = xmin x = xmax

(xmin, ymin) y = ymin

For a point (x,y) to be inside the clip rectangle:


xmin  x  xmax
ymin  y  ymax
Line Clipping

clip
rectangle

Cases for clipping lines


Line Clipping

B B

A A

clip
rectangle

Cases for clipping lines


Line Clipping

D' D'

C C
B B

A A

clip
rectangle

Cases for clipping lines


Line Clipping

F
D

D' D'

C C
B B
E

A A

clip
rectangle

Cases for clipping lines


Line Clipping

F
D

D' D'

C C
B H B
E
H' H'
A A
G' G'

clip G
rectangle

Cases for clipping lines


Line Clipping

F
D

D' D'

C C
B H B
E
H' J H'
A A
G' G'

J'
clip G
rectangle I'

Cases for clipping lines


Line Clipping
Clipping Lines by Solving Simultaneous Equations

(x1, y1) (x1, y1)

(x, y) (xb, yb) (xa, ya) (xb, yb) (x, y) (xa, ya)

(x0, y0) (x0, y0)

(xc, yc) (xd, yd) (xc, yc) (xd, yd)

clip clip
rectangle rectangle

x  x0  tline  x1  x0 , y  y0  tline  y1  y0 
x  xa  tedge  xb  xa , y  ya  tedge  yb  ya 
Cohen-Sutherland Algorithm
The Cohen-Sutherland Line-Clipping
Algorithm performs initial tests on a line to
determine whether intersection calculations
can be avoided.
1. First, end-point pairs are checked for Trivial
Acceptance.
2. If the line cannot be trivially accepted, region
checks are done for Trivial Rejection.
3. If the line segment can be neither trivially
accepted or rejected, it is divided into two
segments at a clip edge, so that one segment
can be trivially rejected.
These three steps are performed iteratively
until what remains can be trivially accepted or
rejected.
Cohen-Sutherland Algorithm

1001 1000 1010

bit 0 : y  ymax bit 2 : x  xmax


0001 0000 0010
bit 1 : y  ymin bit 3 : x  xmin

0101 0100 0110


clip
rectangle

Region outcodes
Cohen-Sutherland Algorithm
1. A line segment can be trivially
accepted if the outcodes of both the
endpoints are zero.
2. A line segment can be trivially
rejected if the logical AND of the
outcodes of the endpoints is not zero.
3. A key property of the outcode is that
bits that are set in nonzero outcode
correspond to edges crossed.
Cohen-Sutherland Algorithm

clip C
rectangle

B
A

An Example
Cohen-Sutherland Algorithm

clip C
rectangle

An Example
Cohen-Sutherland Algorithm

clip C
rectangle

An Example
Cohen-Sutherland Algorithm

clip C
rectangle

An Example
Parametric Line-Clipping
(1) This fundamentally different (from Cohen-
Sutherland algorithm) and generally more
efficient algorithm was originally published by
Cyrus and Beck.
(2) Liang and Barsky later independently
developed a more efficient algorithm that is
especially fast in the special cases of upright
2D and 3D clipping regions.They also
introduced more efficient trivial rejection tests
for general clip regions.
The Cyrus-Back Algorithm
Outside of clip region Inside of clip rectangle
Edge Ei Line P0 P1 : Pt   P0  P1  P0 t
PEi

 
Pi t   PEi

P1 N i  Pt   PEi  0

 
N i  Pt   PEi  0
 
N i  Pt   PEi  0
 
 N i  P0  P1  P0 t  PEi  0
 N  P  P  P t  P   0
P0

 
N i  P t   PEi  0 i 0 1 0 Ei

Ni N  P  P 
t 
i 0 Ei

 N i  P0  P1 

t 

N i  P0  PEi , D  P  P
 Ni  D
0 1
The Cyrus-Back Algorithm
Outside of clip region Inside of clip rectangle
Edge Ei
PEi

Pi t   PEi

P1

 
N i  Pt   PEi  0

P0
 
N i  Pt   PEi  0

 
N i  P t   PEi  0

Ni

t exists when

t

N i  P0  PEi  1 N i  0
 Ni  D 2 D  0  P0  P1
3 Ni  D  0
The Cyrus-Back Algorithm
P1
t=1

PE Line 1 P1
Line 2 t=1
P1
t=1
PL
PL PL
PL
P0
PE
t=0 Line 3
P0
t=0

PE

PE
P0
t=0 Clip
rectangle

PE = Potentially Entering PL = Potentially Leaving


N i  D  0  PE N i  D  0  PL
 Angle  90  Angle  90
The Cyrus-Back Algorithm
Precalculate Ni and PEi for each edge
for (each line segment to be clipped) {
if (P1 == P0)
line is degenerated, so clip as a point;
else {
tE = 0; tL = 1;
for (each candidate intersection with a clip edge) {
if (Ni • D != 0) { /* Ignore edges parallel to line */
calculate t;
use sign of Ni • D to categorize as PE or PL;
if (PE) tE = max(tE , t);
if (PL) tL = min(tL , t);
}
}
if (tE > tL) return NULL;
else return P(tE) and P(tL) as true clip intersection;
}
}
Polygon Clipping

Example
Polygon Clipping

Example
Polygon Clipping

Example
Sutherland-Hodgeman Algo.

Clip
Clip
Clip
Against
Against
Against
TheBottom
Clipped
Right
Left
Top Clipping
Clipping
Clipping
Polygon
Boundary
Boundary
Boundary
Initial Condition
4 Cases of Polygon Clipping
Inside Outside
i:first output
p
s
p:second output
s
Polygon
being
clipped

p
s

s
p:output (no output)
i:output
Clip
boundary

Case 2
1
3
4
Algorithm
Input vertex P Close Polygon entry

Does SF
No First Point Yes intersect E?

Yes

Compute
F=P Intersection I
No
Does SP intersect
No
E?

Output
Yes
vertex I

Compute
Intersection Point
I
Exit

Output
vertex I

S=P

Is S on left
Yes
side of E?

Output
vertex S

NO

Exit
3D Clipping
• Both the Cohen-Sutherland and Cyrus-Beck clipping algorithm
readily extend to 3D.
• For Cohen-Sutherland algorithm use two extra-bit in outcode for
incorporating z < zmin and z > zmax regions
Thank You

S-ar putea să vă placă și