Professional Documents
Culture Documents
Chapter 04
Chapter 04
coordinates. Objects inside the world or clipping window are mapped to the viewport which is the area on the
screen where world coordinates are mapped to be displayed.
General Terms:
●World coordinate – It is the Cartesian coordinate w.r.t which we define the diagram, like Xwmin,
Xwmax, Ywmin, Ywmax
● Device Coordinate –It is the screen coordinate where the objects is to be displayed, like Xvmin,
Xvmax, Yvmin, Yvmax
● Window –It is the area on world coordinate selected for display.
● ViewPort –It is the area on device coordinates where graphics is to be displayed.
Mathematical Calculation of Window to Viewport:
It may be possible that the size of the Viewport is much smaller or greater than the Window. In these cases, we
have to increase or decrease the size of the Window according to the Viewport and for this, we need some
mathematical calculations.
(xw, yw): A point on Window
Now the relative position of the object in Window and Viewport are the same.
For x coordinate,
xv-xvmin=xvmax-xvmin(xw-xwmin/xwmax-xwmin)
xv-xvmin=xw-xwmin(xvmax-xvmin/xwmax-xwmin)
xv-xvmin=xw-xwmin(sx)
For y coordinate,
Yv-Yvmin=Yvmax-Yvmin(Yw-Ywmin/Ywmax-Ywmin)
Example:
Let's assume,
1.for window, Xwmin = 20, Xwmax = 80, Ywmin = 40, Ywmax = 80.
2.for viewport, Xvmin = 30, Xvmax = 60, Yvmin = 40, Yvmax = 60.
3.Now a point ( Xw, Yw ) be ( 30, 80 ) on the window. We have to calculate that point on the viewport i.e. ( Xv, Yv ).
4.First of all, calculate the scaling factor of x coordinate Sx and scaling factor of y coordinate Sy using the above
mentioned formula.
Sx = ( 60 - 30 ) / ( 80 - 20 ) = 30 / 60
Sy = ( 60 - 40 ) / ( 80 - 40 ) = 20 / 40
Note:
Line Clipping
1.In computer graphics, line clipping is the process of
removing lines or portions of lines outside an area of
interest. Typically, any line or part thereof which is outside
of the viewing area is removed.
2.There are two common algorithms for line clipping:
Cohen–Sutherland and Liang–Barsky,Cyrus Beck,midpoint
Subdivision
3.A line-clipping method consists of various parts. Tests are
conducted on a given line segment to find out whether it
lies outside the view volume. Afterwards, intersection
calculations are carried out with one or more clipping
boundaries.
4.Determining which portion of the line is inside or outside
of the clipping volume is done by processing the endpoints of the line with regards to the intersection.
1.Point Clipping
The concept of line clipping is the same as point clipping. In line clipping, we will cut the portion of line which is
outside of the window and keep only the portion that is inside the window.
1.This algorithm uses the clipping window as shown in the following figure. The minimum coordinate for the
clipping region is $(XW_{min,} YW_{min})$ and the maximum coordinate for the clipping region is $(XW_{max,}
YW_{max})$.
Step1:Line Point(P1,P2)
Outcode(P1)=0001=A1
Outcode(P2)=1000=B1
Step2: A1 OR B1= 0001 OR 1000=1001
Logical end of both end point is not 0000,so line is not completely visible
Step 3: A1 AND B1 =0001 AND 1000 =0000
Both end point result is 0000.So the line is not completely outside
Step 4: Let us calculate visible portion of line
X= Xmin=50
X coordinate is 50
Y coordinate ?
Slope of line m=
[y2-y1]/[x2-x1]
(45-12/75-40)=30/35=0.857
c=15-(0.857)x40
=15-34.28
c=-19.28
Step 6: calculate value for Y
y=mx+c
Question 2: Use cohen-sutherland algorithm to clip two line P1(70,20),P2(100,10) Against window
A(50,10),B(80,10),C(80,40),D(50,40)
Solution:-
Step1:Outcode(P1)=0000=A1
Outcode(P2)=0010=B1
Problem 3: Let R be a rectangle window Whose lower left hand corner is at L(-3,1) and upper right hand corner is
at R(2,6).find the region code for the endpoint and use Cohen Sutherland algorithm to clip the line segments.
Coordinates for line segments are. For line AB A(-2,3) and B(1,2).
1.In this algorithm, initially the line is tested for visibility. If a line is completely visible it is drawn and if it is
completely invisible it is rejected.
2.If a line is partially visible then it is subdivided in two equal parts. The visibility tests are then applied to each half.
3.This subdivision process is repeated until we get completely visible and completely invisible line segments. This is
illustrated in figure (k) below
5.Both the line segments are tested for visibility and found to be partially visible. Both line segments are then
subdivided in two equal parts to get midpoints P4 and P5 (see Fig. k (c)).
6.It is observed that line segments P1 P4 and P5 P2 are completely invisible and hence rejected. However, line
segment P3 P5 is completely visible and hence drawn.
7.The remaining line segment P4 P3 is still partially visible. It is then subdivided to get midpoint P6. It is observed
that P6 P3 is completely visible whereas P4 P6 is partially visible.
8.Thus P6 P3 line segment is drawn and P4 P6 line segment is further subdivided into equal parts to get midpoint
P7.
9. Now, it is observed that line segment P4 P7 is completely invisible and line segment P7 P6 is completely visible
(see Fig. k (f)), and there is no further partially visible segment.
Step 5: We Need to check that the line is near to the boundary of the window or not.
Step 7: Stop.
Advantages:
3. It is a fast algorithm.
Disadvantages:
Example: A window contains the size (0, 50, 0, 50). A line PQ has the coordinates (-10, 40) and (30, -20). Find the
visible point of the line using midpoint subdivision.
Solution: We have,
The coordinates for x and y = P (-10, 40)
The coordinates for x and y = Q (30, -20)
Now,
Step 1: We have to compute the
midpoint of the line segment PQ.
Pmx=(Px1+Px2)/2
=-10+30/2
=10
Pmy= (Py1+py2)/2
=40-20
=10
Pm(10,10)
Step 2: Processing on line segment
Pmp2 as p1p2
pm=p1
Pmx =(10+30)/2
=20
Pmy=(10-20)/2
=-5
Pm(20,-5)
Here we replace p2 as pm
Step3:
Processing line segment p1, Pm as p1 p2
pm(20,-5)=p1(10,10)
pmx=(20+10)/5
=15
Pmy=(-5+10)/2
=-2.5
Pm(15,2.5)
pmx=(15+20)/2
=17.5
pmy=(2.5-5)/2
=-1.25
Pm(15.5,-1.25)
pm(17.5,-1.25)= p1(15,2.5)
pmx=(15+17.5)/2
=16.25
Pmy=(-2.5-1.25)/2
=0.625
Pm(16.25,0.625)
pm(10,10)=p1(-10,40)
pmx=(-10+10)/2=0
pmy=(40+10)/2 =25
Pm(0,25)
1.It is developed as a generalized line clipping algorithm.This algorithm is applicable to an arbitrary convex region.
2. This algorithm uses a parametric equation of line segment to find the intersection points of line with the
clipping edges.
3.The parametric equation of line segment from p1 to p2 is
P(t)=P1+(P2-P1)t;
0<=t<=1
Where t is parametric,t=0 at P1 and t=1 at P2.
4.Consider a convex clipping region R,f i s a boundary point of the convex region R and n is an inner normal for one
of its boundaries as shown in the fig below
5.Then we can distinguish in which region point lie by looking at the value of the dot product n.[P(t)-f] , as shown
in above fig(b)
6.If dot product is negative,i.e
n.[P(t)-f] < 0 then the vector P(t) - f is pointed a away from interior of R
Solution:-
1.The parametric representation of the line P1P2 is
P(t)=P1+(P2- P1)t
=[-2 1]+[10 3]t
=(10t - 2) i + (3t +1 ) j; 0<=t<=1
2.Where i and j are the unit vectors in the x and y directions, respectively. The four inner normals are given as
Left : nL= i
Right : nR= -i
Bottom : NB= j
Top : nt= -j
This value of t is outside the range of 0<=t <=1v and hence it is rejected
nT.[P(t) - f] = -(3t - 4) =0
t= 4 / 3
Thus we get two intersection points (2.2.2) and (7,3.7) with left edge and right edge respectively.
tL=-W.n/D.n
Else
tU= -W.n/D.n
End if
The ideas for the clipping line of Liang-Barsky and Cyrus-Beck are the same. The only difference is that the
Liang-Barsky algorithm has been optimized for an upright rectangular clip window. So we will study only the idea of
Liang-Barsky.
Liang and Barsky have created an algorithm that uses floating-point arithmetic but finds the appropriate endpoints
with at most four computations. This algorithm uses the parametric equations for a line and solves four
inequalities to find the range of the parameter for which the line is in the viewport.
Step1:Δx=x2-x1
Δy=y2-y1
k=1,2,3,4
Step2:
x=X1+t1Δx
y=Y1+t1Δy
t2= min(1,qK/PK)
x=X1+t2Δx
y=Y1+t2Δy
Example: Consider the line from P1(4,14) to P2(8,8) clipped to the rectangle region R as shown in the Fig.(c) .The
line P1P2 intersects the windows.Calculate the intersect points using Liang Barsky algorithm.
Step1:
Δx=x2-x1
=8-4=4
Δy=y2-y1
Step2:
PK qK
P2= Δx = 4 q2 = xwmax-x1==9-4=5
Step 3:
Step 4:
t1=max(0,qK / PK)
t1= max(0,-1/-4,-3/-4)
t1=3/4
=4+3/4x4=7
=12+3/4x(-4)=9
Step 5:
t2=min(1,qK / PK)
t2=min(1,5/4,7/4)
t2=1
Case2) Right clip: In the right-side polygon clipping, we only remove the right part of the polygon, which is outside
the window. We only save the portion which is inside the window.
Case3) Top clip: On the top side polygon clipping, we only remove the top part of the polygon, which is outside the
window. We only save the portion which is inside the window.
Condition 2(In-Out): If the first vertex of the polygon is inside and the second vertex is outside the window, then
the output will be the intersection point.
Condition 4(Out-Out): If both vertices of the polygon are outside the window, then the output will be nothing. It
means we found a clipped polygon.
Solution:- Original polygon vertices are V1, V2, V3, V4, and V5. After clipping each boundary the new vertices are
as shown in figure above.
Text Clipping
Various techniques are used to provide text clipping in computer graphics. It depends on the methods used to
generate characters and the requirements of a particular application. There are three methods for text clipping
which are listed below −
● All or none string clipping
● All or none character clipping
● Text clipping
In all or none string clipping method, either we keep the entire string or we reject the entire string based on the
clipping window. As shown in the above figure, STRING2 is entirely inside the clipping window so we keep it and
STRING1 being only partially inside the window, we reject.
The following figure shows all or none character clipping −
Chapter 04 Compiled By Dashrath B Kale
This clipping method is based on characters rather than the entire string. In this method if the string is entirely
inside the clipping window, then we keep it. If it is partially outside the window, then −
● You reject only the portion of the string being outside
● If the character is on the boundary of the clipping window, then we discard that entire character and keep
the rest string.
This clipping method is based on characters rather than the entire string. In this method if the string is entirely
inside the clipping window, then we keep it. If it is partially outside the window, then
● You reject only the portion of string being outside.
● If the character is on the boundary of the clipping window, then we discard only that portion of character
that is outside of the clipping window.