0% found this document useful (0 votes)
38 views18 pages

Clipping

Uploaded by

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

Clipping

Uploaded by

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

Line Clipping

• It is performed by using the line clipping algorithm.

• The line clipping algorithms are:


1. Cohen Sutherland Line Clipping Algorithm
2. Midpoint Subdivision Line Clipping Algorithm
3. Liang-Barsky Line Clipping Algorithm
Cohen Sutherland Line Clipping Algorithm:
In the algorithm, first of all, it is detected whether line lies inside the screen or it is outside
the screen.
All lines come under any one of the following categories:
1. Visible
2. Not Visible
3. Clipping Case

Visible: If a line lies within the window, i.e., both endpoints of the line lies within the
window. A line is visible and will be displayed as it is.

Not Visible: If a line lies outside the window it will be invisible and rejected. Such lines
will not display. If any one of the following inequalities is satisfied then the line is
considered invisible.

(xmin, ymin) and (xmax, ymax ) are coordinates of the window.


Clipping Case:
If the line is neither visible case nor invisible case. It is considered to be clipped case. First of
all, the category of a line is found based on nine regions given below.
• All nine regions are assigned codes.
• Each code is of 4 bits.
• If both endpoints of the line have end bits zero, then the line is considered to be visible.
1001 1000 1010

0001 0000 0010

0101 0100 0110

Advantage of Cohen Sutherland Line Clipping:


1. It calculates end-points very quickly and rejects and accepts lines quickly.
2. 2. It can clip pictures much large than screen size.
How can determine the code for each endpoint?
Starting from the leftmost bit, each bit of the code is set to true(1) or false(0)
according to the schema:

 Bit 1 ≡ endpoint is above of the window = sign ( Y – Ymax )

 Bit 2 ≡ endpoint is below of the window = sign ( Ymin –Y )

 Bit 3 ≡ endpoint is right of the window = sign ( X – Xmax )

 Bit 4 ≡ endpoint is left of the window = sign ( Xmin – X )

sign (a) = 1 if a is positive sign (a) = 0 otherwise.


Example 1: To fined the code for any end point

1- End point A (0000)


5,6
Bit 1 ≡ = sign ( Y – Ymax) = sign ( 3 – 5 ) = -2 = 0
Bit 2 ≡ = sign ( Ymin -Y) = sign ( 0 – 3 ) = -3 = 0 5,5

Bit 3 ≡ = sign (X - Xmax) = sign ( 2 – 7 ) = -5 = 0


Bit 4 ≡ = sign ( Xmin - X) = sign ( 0 – 2 ) = -2 = 0 2,3

2- End point B (1000)


0,0
Bit 1 ≡ = sign ( 6 – 5) = 1 = 1
Bit 2 ≡ = sign ( 0 - 6) = -6 = 0
Bit 3 ≡ = sign (5 – 7 ) = -2 = 0
Bit 4 ≡ = sign ( 0 – 5 )= -5 = 0
Algorithm of Cohen Sutherland Line Clipping:

Step1:Calculate positions of both endpoints of the line.

Step2:Perform OR operation on both of these end-points.

Step3:If the OR operation gives 0000 then line is considered to be visible [Case of Trivial
Acceptance] else Perform AND operation on both endpoints.

If And ≠ 0000  then the line is invisible [Case of Trivial Rejection]


else And = 0000  Line is considered the clipped case.

Step4:If a line is clipped case, find an intersection with boundaries of the window
 Intersection points with a clipping boundary can be calculated using the slope-intercept
form of the line equation.
 For a line with endpoint coordinates (x1, y1) and (x2, y2), the y coordinate of the
intersection point with a vertical boundary can be obtained with the calculation
y = y1 + m(x-x1)
B x2,y2
[y-y1/x-x1 = y2-y1/x2-x1
x, y D
y-y1 = (x-x1)m
y = y1 + m(x-x1)] E
A C
x1,y1
where the x value is set either to or

and slope of the line is given by


ABC & ADE
m=(y2-y1 )(x2-x1)
m= y2-y1 / x2 - x1----- 1
Similarly, if we are looking for the intersection with a horizontal boundary, the m= y-y1 / x – x1 -------- 2
x coordinate can be calculated as

x = x1 + (y-y1)/ m

[x-x1= y-y1/m
X = x1 + (y-y1)/m]

with y set either to or .


Pseudo Code:

Step 1 : Assign a region code for two endpoints of given line.


Step 2 : If both endpoints have a region code 0000
then given line is completely inside.
Step 3 : Else, perform the logical AND operation for both region codes.
Step 3.1 : If the result is not 0000, then given line is completely
outside.
Step 3.2 : Else line is partially inside.
Step 3.2.1 : Choose an endpoint of the line
that is outside the given rectangle.
Step 3.2.2 : Find the intersection point of the
rectangular boundary (based on region code).
Step 3.2.3 : Replace endpoint with the intersection point
and update the region code.
Step 3.2.4 : Repeat step 2 until we find a clipped line either
trivially accepted or trivially rejected.
Step 4 : Repeat step 1 for other lines

The point of intersection of the line segment with an extended window edge, each
of the four directions is tested in the order: left, right, top, bottom.
EXAMPLE
If the clipping window is XL = -4; XR = 4; YT = 4; YB = -4
Clip the lines: KL=(4,5)-(6,5) and AB=(1,1)-(1,3)

The solution :
For the line KL:
• code1 of (4,5) is 1000
• code2 of (6,5) is 1010
• code1 And code2 : 1000 And 1010 = 1000 ( not zero) then the line
KL is not visible, Trivial rejection

For the line AB:


• code1 of (1,1) is 0000
• code2 of (1,3) is 0000
• code1 And code2 : 0000 And 0000 = 0000 ( zero) then the line AB
is visible, Trivial acceptance.
EXAMPLE 3
5,8
6,7
If the clipping window is XL=3; XR=6; YT=7; YB=2
Clip the lines: 1,4
AB=(1,4)-(5,8)
The solution :
3,2
• Code1 of (1 , 4) is 0001
• Code2 of ( 5, 8) is 1000 Right intersection : XR=6 YR=1* (6-1)+4=9
• Code1 And Code2 : 0001 And 1000=0000 Then ( 6, 9) is rejected
• Then the line is a candidate for clipping
Top intersection : YT=7 XT =(1/1) * (7-4 )
• We must calculate the intersection points: +1 = 4
• m=(y2-y1)/(x2-x1) = (8-4) / (5-1) = 4/4=1 Then (4 ,7) is the second point of
intersection
• Left intersection : XL= 3 , YL=1* (3-1)+4= 6
• Then ( 3, 6) is the first point of intersection. Bottom intersection : YB= 2 XB =(1/1)* (2-
4 )+1 = -1 Then (-1,2) is rejected
Polygon Clipping
Polygon : is traditionally a plane figure that is bounded by a finite chain of straight line
segments closing in a loop to form a closed chain or circuit. These segments are called its
edges or sides, and the points where two edges meet are the polygon's vertices ( vertex) or
corners.
• Line clipping is acceptable when the output can be a set of disconnected lines segments.
• There are, however, the situation in which a polygon clipped against a window should
result in one or more polygons.

Sutherland-Hodgman
• The Sutherland-Hodgman polygon clipping algorithm clips a polygon against each edge
of the window, one at a time.
• Specifically for each window edge, it inputs a list of vertices and outputs a new list of
vertices which is submitted to the algorithm for clipping against the next window edge.
• The first window edge has as input the original set of vertices.
• After clipping against the last edge, the output list consists of the vertices describing the
clipped polygon
We can correctly clip a polygon by processing the polygon boundary as a whole against
each window edge.

 This could be accomplished by processing all polygon vertices against each clip
rectangle boundary in turn.

 Beginning with the initial set of polygon vertices, we could first clip the polygon
against the left rectangle boundary to produce a new sequence of vertices.

 The new set of vertices could then successively passed to a right boundary clipper, a
bottom boundary clipper, and a top boundary clipper.

 At each step, a new sequence of output vertices is generated and passed to the next
window boundary clipper.
Algorithm

For each window edge, we tack the input list of vertices for that edge, tests a pair of
consecutive vertices against a window edge to produce the output list of vertices. .

Where testing is performed against the left edge

Approach

1. Polygon to be clipped is given as v1,v2,…….vn


2. Polygon edge is a pair[vi,vi+1]
3. Process all polygon edges in succession against a window edge, Polygon (v1, v2,…...vn)
Polygon(w1,w2,…….wm)
4. Repeat on resulting polygon with next window edge

 There are four possible cases of testing as illustrated in the below figure where testing is
performed against the left edge
4 CASES

» Case 1 : Output vi+1


» Case 2 : Output intersection point
» Case 3 : No output
» Case 4 : Output intersection point and Vi+1
The sample polygon has vertices {V1, V2, V3, V4}. Case 2

Case 1:- Has the first and second vertices V1 and V2 v3 i1 v2


inside the window, so the second vertex V2 is sent Case1
Case 3 
to the output list.
v4 i2 v1
Case2:- Has the first vertex V2 inside and the
second vertex V3 outside the window. The point of
Case 4
intersection, i1, is added to the output list.
Case3:-Has both vertices, V3 and V4 outside the
window and no point is output.
Case4:- Has the first vertex V4 outside and the
second vertex V1 inside the window, the point of
intersection I2 and the second vertex V1 are added
to the output.
INPUT
v5
v1,v2,v3,v4,v
5,v6 V5’
V4’
Edges v6 v5’’
1. v1,v2
2. v2,v3 v4
3. v3,v4
4. v4,v5 v3
5. v5,v6
6. v6,v1
V2’
v1
Left Clipper Edges Edges
7. v2’ v2’v3 v3v4 v2
8. v3 v3v4 v4v5 Edges
9. v4 v4v5 Right v5v5’ Bottom v3v4 Top
10. v5 v5v5’ Clipper v5’v2’ Clipper v4v5 Clipper Edges
11. v5’ v5’v2’ 1. v3 v2’v3 1. v3 v5v5’ 1. v4 v4v4’
2. v4 2. v4 v5’v2’ 2. v4’ v4’v5’’
3. v5 3. v5 v2’v3 3. v5’’ v5’’v2’
4. v5’ 4. v5’ 4. v2’ v2’v3
5. v2’ 5. v2’ 5. v3 v3v4
INPUT
v5
v1,v2,v3,v4,v
5,v6 V5’
v6’ V4’
Edges v6 v5’’
1. v1,v2
2. v2,v3 v4
3. v3,v4
4. v4,v5 v3
5. v5,v6
6. v6,v1
V2’
v1
Left Clipper Edges Edges
7. v2’ v2’v3 v3v4 v2
8. v3 v3v4 v4v4’
9. v4 v4v5 Top v4’v5’’ Bottom Top
10. v5 v5v5’ Clipper v5’’v2’ Clipper Clipper
11. v5’ v5’v2’ 1. v3 v2’v3 1. v4 1. v4
2. v4 2. v4’ 2. v4’
3. v4’ 3. v5’’ 3. v5’’
4. v5’’ 4. v2’ 4. v2’
5. v2’ 5. v3 5. v3

You might also like