Clipping Algo
Clipping Algo
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.
Xleft ≤ X ≤ Xright
Ybottom ≤ Y ≤ Ytop
Where :
Xleft , Xright , Ybottom , Ytop are the position of the edge of 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 )
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:
Note :
sign (a) = 1 if a is positive
sign (a) = 0 otherwise.
Example 1:
To fined the code for any end point
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
(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
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
These segments are called its edges or sides, and the points where two edges
meet are the polygon's vertices ( vertex) or corners.
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.
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
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.
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.
The solution :
(8,5)
a(5,6)
d(9,3)
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)