You are on page 1of 19

Chapter Four

Clipping
Part one
Computer Graphics Chapter Four

Clipping
Clipping
Many graphics application programs give the user the impression of looking
through a window at a very large picture. The program makes use of the scaling
and translation techniques to generate a variety of different views of a single
representation of a plan.

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, a
process which divides each element of the picture into its visible and invisible
portions, allowing the invisible portion to be discarded.

1- Point Clipping
Is to determine if a point (X, Y) is visible or not by a simple pair of inequalities:
X left ≤ X ≤ X right
Y bottom ≤ Y ≤ Y top
Where X left, X right, Y bottom, Y top 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.
86 (2020-2021 ( ‫قسم علوم الحاسبات‬ ‫ فرح قيس عبد هللا‬.‫ د‬-: ‫مدرسة المادة‬
Computer Graphics Chapter Four

2- 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 (4-6) shows a number of different lines with respect to the screen :

Figure (4-6)
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
86 (2020-2021 ( ‫قسم علوم الحاسبات‬ ‫ فرح قيس عبد هللا‬.‫ د‬-: ‫مدرسة المادة‬
Computer Graphics Chapter Four

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
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
3- Clipping candidate: the line is in neither category 1 nor category 2.

Figure (4-7) some of the line segments

Line segment AB is in category 1 (visible).


Line segments CD and EF are in category 2 ( invisible)
Line segments GH, IJ, KL are in category 3 (clipping candidate)

07 (2020-2021 ( ‫قسم علوم الحاسبات‬ ‫ فرح قيس عبد هللا‬.‫ د‬-: ‫مدرسة المادة‬
Computer Graphics Chapter Four

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. The code is
determined according to which of the following nine regions the endpoints
lie in :

Figure (4-8)

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 the window = sign ( Y – Ymax)
Bit 2 ≡ endpoint is below the window = sign ( Ymin -Y)
Bit 3 ≡ endpoint is to the right of the window = sign (X - Xmax)

07 (2020-2021 ( ‫قسم علوم الحاسبات‬ ‫ فرح قيس عبد هللا‬.‫ د‬-: ‫مدرسة المادة‬
Computer Graphics Chapter Four

Bit 4 ≡ endpoint is to the left of the window = sign ( Xmin - X)


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

2- The line segment is visible if both endpoint codes are 0000


The line segment is not visible if the logical AND of the codes is not 0000
The line segment is a candidate for clipping if the logical AND of the endpoint
codes is 0000.

We now decide whether the line candidate for clipping either intersects the
window and so is to be clipped or don't intersect the window and so is not
displayed.
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
07 (2020-2021 ( ‫قسم علوم الحاسبات‬ ‫ فرح قيس عبد هللا‬.‫ د‬-: ‫مدرسة المادة‬
‫‪Computer Graphics‬‬ ‫‪Chapter Four‬‬

‫‪07‬‬ ‫قسم علوم الحاسبات ( ‪(2020-2021‬‬ ‫مدرسة المادة ‪ -:‬د‪ .‬فرح قيس عبد هللا‬
Computer Graphics Chapter Four

Notes:
1- If M=∞ we don’t calculate the intersection points with the left edge and right
edge because the line is parallel to them, we only calculate the intersection
points with the top edge and bottom edge.
2- If M=0 we don’t calculate the intersection points with the top edge and
bottom edge because the line is parallel to them, we only calculate the
intersection points with the left and right edge.

07 (2020-2021 ( ‫قسم علوم الحاسبات‬ ‫ فرح قيس عبد هللا‬.‫ د‬-: ‫مدرسة المادة‬
Computer Graphics Chapter Four

Example 1:- 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)

Solution 1 :
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

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.

Example 2: Consider the clipping window

Clip the line ( - 3/2 , 1/6) – ( 1/2 , 3/2)

Solution 2 :

Code1 of (- 3/2 , 1/6) is 0001


Code2 of ( 1/2 , 3/2) is 1000

07 (2020-2021 ( ‫قسم علوم الحاسبات‬ ‫ فرح قيس عبد هللا‬.‫ د‬-: ‫مدرسة المادة‬
Computer Graphics Chapter Four

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


YL=0.6 (-1-(-3/2))+1/6=1/2
Then ( -1, 0.5) is the first point of intersection

Right intersection : XR=1


YR=0.6 (1-(-3/2))+1/6=1.8
Then ( 1, 1.8) is rejected

Top intersection : YT=1


XT =1.6 (1- 1/6 )+(- 3/2) = - 1/4
Then (-1/4 ,1) is the second point of intersection

Bottom intersection : YB=-1


XB =1.6 (-1 - 1/6 )+(- 3/2) = - 3.25
Then (-3.25 ,-1) is rejected

08 (2020-2021 ( ‫قسم علوم الحاسبات‬ ‫ فرح قيس عبد هللا‬.‫ د‬-: ‫مدرسة المادة‬
Computer Graphics Chapter Four

3- Polygon clipping
In geometry, a 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.
We interest in this section on the simple polygon.
Simple polygon: planer set of ordered points. In geometry, a simple polygon is
defined as a flat shape consisting of straight, non-intersecting line segments or
"sides" that are joined pair-wise to form a closed path. If the sides intersect
then the polygon is not simple.
A simple polygon is characterized by
 No line crossings
 No Holes

Simple Polygon Polygon with Holes Polygon with Line crossing

Figure (4-9) some types of polygon

00 (2020-2021 ( ‫قسم علوم الحاسبات‬ ‫ فرح قيس عبد هللا‬.‫ د‬-: ‫مدرسة المادة‬
Computer Graphics Chapter Four

Sutherland-Hodgman
- The window must be convex
- Polygon to be clipped can be convex or non-convex
-

Figure (4-10) types of polygons with clipping technique

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

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
figure - 3 - Where testing is performed against the left edge
06 (2020-2021 ( ‫قسم علوم الحاسبات‬ ‫ فرح قيس عبد هللا‬.‫ د‬-: ‫مدرسة المادة‬
Computer Graphics Chapter Four

Approach
 Polygon to be clipped is given as v1,v2,……., in
 Polygon edge is a pair[vi,vi+1]
 Process all polygon edges in succession against a window edge
Polygon (v1, v2,…...vn) Polygon(w1,w2,…….wm)
 Repeat on resulting polygon with next window edge

Figure (4-11) 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.

06 (2020-2021 ( ‫قسم علوم الحاسبات‬ ‫ فرح قيس عبد هللا‬.‫ د‬-: ‫مدرسة المادة‬
Computer Graphics Chapter Four

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
Find the clipping for the polygon below start from S1

Solution

67 (2020-2021 ( ‫قسم علوم الحاسبات‬ ‫ فرح قيس عبد هللا‬.‫ د‬-: ‫مدرسة المادة‬
Computer Graphics Chapter Four

Figure ( 4-12) clipping polygon using a Sutherland-Hodgman algorithm

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 :
67 (2020-2021 ( ‫قسم علوم الحاسبات‬ ‫ فرح قيس عبد هللا‬.‫ د‬-: ‫مدرسة المادة‬
Computer Graphics Chapter Four

Vertices-no: integer; counter of the input vertices


Outline: integer; a 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

The algorithm
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 }

67 (2020-2021 ( ‫قسم علوم الحاسبات‬ ‫ فرح قيس عبد هللا‬.‫ د‬-: ‫مدرسة المادة‬
‫‪Computer Graphics‬‬ ‫‪Chapter Four‬‬

‫‪67‬‬ ‫قسم علوم الحاسبات ( ‪(2020-2021‬‬ ‫مدرسة المادة ‪ -:‬د‪ .‬فرح قيس عبد هللا‬
Computer Graphics Chapter Four

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

67 (2020-2021 ( ‫قسم علوم الحاسبات‬ ‫ فرح قيس عبد هللا‬.‫ د‬-: ‫مدرسة المادة‬
Computer Graphics Chapter Four

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) m(a d)=(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)

HW . suppose window where left up(1,8) and right down (5,2) and need clipping
polygon where vertices { (3,3) ,(6,5) , (4,7), (-3,7),(2,5) ,(2,0),(3,3)} what
happened then new polygon uses vertices that inside in window.

67 (2020-2021 ( ‫قسم علوم الحاسبات‬ ‫ فرح قيس عبد هللا‬.‫ د‬-: ‫مدرسة المادة‬

You might also like