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

Clipping Algo

Uploaded by

Shashwat
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)
38 views27 pages

Clipping Algo

Uploaded by

Shashwat
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/ 27

‫الجامعة المستنصرية‬

‫كلية العلوم ‪ /‬قسم علوم الحاسوب‬


‫المرحلة الثالثة ‪ /‬مسائي‬
Part Nine
1. Clipping
2. Point Clipping
3. Line Clipping
4. Cohen – Sutherland algorithm
5. Polygon clipping
6. Simple polygon

Simple Polygon Polygon with Line crossing Polygon with Holes


Clipping

 Many graphics application programs give the user the impression of


looking through a window at a very large picture.

 To display an enlarged portion of a picture, we must not only apply the


appropriate scaling and translation but also identify the visible parts of
the picture for inclusion in the displayed image.

Certain lines may lie partly inside the visible portion of the picture and partly
outside.

 The correct way to select visible information for display is to use clipping.

clipping : a process that divides each element of the picture into


1- Visible portions.
2- Invisible portions.
allowing the invisible portion to be discarded.
Point Clipping

Is to determine if a point (X, Y) is visible or not by a simple pair of inequalities:

Xleft ≤ X ≤ Xright
Ybottom ≤ Y ≤ Ytop

Where :
Xleft , Xright , Ybottom , Ytop are the position of the edge of the screen.

These inequalities provide us with a very simple method of clipping pictures on


a point-by-point basis.
Line Clipping

It would be quite inappropriate to clip pictures by converting all picture


elements into points and using point clipping, the clipping process would take
far too long. We must instead attempt to clip large elements of the picture.
Figure below shows a number of different lines with respect to the screen :

 Notice that those lines which are partly invisible are divided by the screen
boundary into one or more invisible portions but only one visible segment.

 This means that the visible segment of a straight line can be determined
simply by computing its two endpoints.
We divide the line clipping process into two phases:
1- Identify those lines which intersect the window and so need to be clipped
2- Perform the clipping.

All line segments fall into one of the following clipping categories:
1- Visible: both endpoints of the line segment lie within the window.
line (A-----B )

2- Not visible: the line segment definitely lies outside the window.
This will occur if the line segment from (X1, Y1) to (X2, Y2) satisfies any one
of the following four inequalities:
X1 , X2 > X max Y1 , Y2 > Y max
X1 , X2 < X min Y1 , Y2 < Y min
line ( C-----D ) and ( L------K )

3- Clipping candidate: the line is in


neither category 1 nor category 2.
line ( E------F )
Cohen– Sutherland algorithm
This algorithm is to find the category of the line segment. The algorithm
proceeds in two steps:

1. Assign a 4-bit code to each


endpoint of the line segments.
2. Determine The line segment is a
candidate for clipping

Note:
The code is determined according to
which of the following nine regions
the endpoints lie in :

X= 0000 Z=1000
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 )

Note :
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)


Bit 1 ≡ = sign ( Y – Ymax) = sign ( 3 – 5 ) = -2 = 0
Bit 2 ≡ = sign ( Ymin -Y) = sign ( 0 – 3 ) = -3 = 0
Bit 3 ≡ = sign (X - Xmax) = sign ( 2 – 7 ) = -5 = 0
Bit 4 ≡ = sign ( Xmin - X) = sign ( 0 – 2 ) = -2 = 0

2- End point B (1000)


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
How can Determine The line segment is a candidate for clipping

1- Visible: The line segment is visible if both


endpoint codes are 0000
A = 0000
B = 0000

2- Not visible: The line segment is not visible if


the logical AND of the codes is not 0000
C = 1000
D = 1010 AND
-------------------------
1000
3- Clipping candidate: The line segment is a
candidate for clipping if the logical AND
of the endpoint codes is 0000.
E = 0000
F = 0100 AND
------------------------
0000
 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. The part of the line that is clearly outside is discarded.

 To calculate the point of intersection of the line segment with the window
border, the point-slope formula is used. If M is the slope of a line segment
between (X1, Y1) and (X2, Y2), then if X1≠X2:-
M = (Y2-Y1) / (X2-X1)
for any other point (X,Y) on the line :-
M =(Y-Y1) / (X-X1)

If we are testing against a left or right direction the X value is known ( left or
right) edge value. This X value is substituted into the equation:
Y=M * (X-X1) + Y1
If we are testing against a top or bottom, the Y value is known and
substituted into :
X= (1/M) * ( Y-Y1 ) + X1
Example 2:-
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)


(A)
then the line KL is not visible

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.

(B)
Example 3:-
If the clipping window is XL=3; XR=6; YT=7; YB=2 Clip the lines:
AB=(1,4)-(5,8)

The solution :
Code1 of (1 , 4) is 0001
Code2 of ( 5, 8) is 1000
Code1 And Code2 : 0001 And 1000=0000
Then the line is a candidate for clipping

We must calculate the intersection points:


m=(y2-y1)/(x2-x1)
M= (8-4) / (5-1) = 4/4=1

Left intersection : XL= 3 Right intersection : XR=6


YL=1* (3-1)+4= 6 YR=1* (6-1)+4=9
Then ( 3, 6) is the first point of intersection Then ( 6, 9) is rejected

Top intersection : YT=7 Bottom intersection : YB= 2


XT =(1/1) * (7-4 )+1 = 4 XB =(1/1)* (2-4 )+1 = -1
Then (4 ,7) is the second point of intersection Then (-1,2) is rejected
Example 4:
Consider the clipping window
Clip the line ( - 3/2 , 1/6) – ( 1/2 , 3/2)

The solution :
Code1 of (- 3/2 , 1/6) is 0001
Code2 of ( 1/2 , 3/2) is 1000
Code1 And Code2 : 0001 And 1000=0000
Then the line is a candidate for clipping

We must calculate the intersection points: M= 1.34 / 2 = 0.6


Left intersection : XL= -1 Right intersection : XR=1
YL=0.6 (-1-(-3/2))+1/6=1/2 YR=0.6 (1-(-3/2))+1/6=1.8
Then ( -1, 0.5) is the first point of intersection Then ( 1, 1.8) is rejected

Top intersection : YT=1 Bottom intersection : YB= -1


XT =1.6 (1- 1/6 )+(- 3/2) = - 1/4 XB =1.6 (-1 - 1/6 )+(- 3/2) = - 3.25
Then (-1/4 ,1) is the second point of intersection Then (-3.25 ,-1) 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.

For example:
A region that is to be shaded must
have a closed boundary.
Simple polygon

A simple polygon:
defined as a flat shape consisting of straight, non-intersecting line segments
or "sides" that are joined pair-wise to form a closed path.

A simple polygon is characterized by


 No line crossings
 No Holes
Simple Polygon

 If the sides intersect


then the polygon is
not simple

Polygon with Line crossing Polygon with Holes


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

types of polygons with clipping technique

 The window must be convex.


 Polygon to be clipped can be convex or non-convex
In the 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.
 There are four possible cases of testing as illustrated in the below figure.
Where testing is performed against the left edge
Approach
1. Polygon to be clipped is given as v1,v2,……., in
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

Figure: Explain the approach


Sutherland-Hodgman algorithm
The sample polygon has vertices {V1, V2, V3, V4}.

Case 1:- Has the first and second vertices V1 and V2 inside the window, so the
second vertex V2 is sent to the output list.
Case2:- Has the first vertex V2 inside and the second vertex V3 outside the
window. The point of intersection, I1, of the side of the polygon joining
the vertices and the edge 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.

The result of this left clipping is the transformation of


the input list { V1, V2, V3, V4 } to the output list { V2, I1, I2, V1 }.
Example 5:
Find the clipping for the polygon below start from S1
The solution:

1 2 3

4 5 6
Figure ( 4-12) clipping polygon using a Sutherland-Hodgman algorithm
Algorithm the polygon clipping
1- Store the values of the vertices coordinate in the ORG array.

2- Transfer the values in ORG to POLYIN { the initial set of vertices}


For I=1 to Vertices-no
POLYIN (I,1) = ORG (I,1) { X values }
POLYIN (I,2) = ORG (I,2) { Y values }
Next

3- Edge = "L" { start from the left edge of the window }


Programming the polygon clipping:
The declarations:
1- We need to define three arrays to hold the list of polygon vertices; each array
is of two dimensions, for the X and Y values of each vertex.

ORG (n, 2 ) / n is the number of vertices in the polygon


This matrix to store the coordinate of the vertices
POLYON (25,2) / the vertices input list {maximum 25 vertex}
POLYOUT (25,2) / the vertices output list{maximum 25 vertex }
2- We need to define a number of variables :
Vertices-no: integer; counter of the input vertices
Outline: integer; counter of the output vertices
Xmin, Xmax, Ymin, Ymax: Integer; the boundary of the window
V1 (1,2) : array for the first vertex
V2 (1,2) : array for the second vertex
Edge: string; for the edge code

3- We need two functions


V3=Intersection (V1, V2, Edge) : used to determine the intersection point
of the line between two vertices and the clip edge.
Boolean=Inside (V, Edge) : return true if the vertex V is inside the
edge, or return false if V is outside the edge
Example 6:-
Clips the polygon showing in the below figure against each edge of the
clipping window (left, top, bottom, right) using the Sutherland-Hodgeman
algorithm then find the coordinates of the intersection points.

Clipping window [(2,1)-(8,5)] polygon[a(5,6)-b(5,0)-c(9,0)-d(9,3)

The solution :

(8,5)

a(5,6)

d(9,3)

(2,1) b(5,0) c(9,0)


a a

d d
i1
i2 i1 i2
b c

i4

i3
i6
i5
Bottom clipping Right clipping Top clipping
[a, b] In- out i1 [i1-i2] In-out i3 [i3, i4] In-in i4
[b, c] Out-out - [i2, d Out-out - [i4, a ] In-out i5
[c, d ] Out-in I2, d [d, a ] Out-in I4, a [a, i1 ] Out-in I6, i1
[d, a ] In-in a [a, i1 ] In-in i1 [i1, i3 ] In-in i3

Coordinates of i1 = (5,1)
Coordinates of i3 = (8,1)
Coordinates of i6 = (5,5)
i4 , i5 lies on line ( a, d)  a(5,6)
mad = (y2-y1)/(x2-x1) = (3-6)/(9-5)= -0.75
i4(x = xr = 8) y= m*(x-x1)+y1
= -0.75*(8-5)+6 = 3.75
i4 = (8, 3.75)
i5(y = yt = 5) x= 1/m*(y-y1)+x1
= -1.25*(5-6)+5 = 6.25
i5(6.25, 5)

You might also like