Notes CGR-Unit 04
Notes CGR-Unit 04
xv-xvmin=xvmax-xvmin(xw-xwmin/xwmax-xwmin)
xv-xvmin=xw-xwmin(xvmax-xvmin/xwmax-xwmin)
xv-xvmin=xw-xwmin(sx)
xv= xvmin + (xw-xwmin)sx
For y coordinate,
Yv-Yvmin=Yvmax-Yvmin(Yw-Ywmin/Ywmax-Ywmin)
Yv-Yvmin=Yw-Ywmin(Yvmax-Yvmin/Ywmax-Ywmin)
Yv-Yvmin=Yw-Ywmin(sY)
Yv= Yvmin + (Yw-Ywmin)sY
so, after calculating for x and y coordinate, we get
where, sx is scaling factor of x coordinate and sy is
scaling factor of y coordinate
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 viewporti.e ( Xv, Yv ).
Note:
1.World coordinate system is selected according to the
application program.
2.Screen coordinate system is chosen according to the need
of design.
3.Viewing transformation is selected as a bridge between the
world and screen coordinate.
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.
3.Line can be partially inside the window (We will find the
intersection point and draw only that portion of line that is
inside the region).Both endpoints are in different regions. In
this case, the algorithm finds one of the two points that is
outside the rectangular region. The intersection of the line
from outside point and rectangular window becomes new
Algorithm
Step 1 − Assign a region code for each endpoint.
Step 3.1 − If the result is not 0000, then reject the line.
if(xa>xh)
a[2] = 1;
else a[2] = 0;
if(xb>xh)
b[2] = 1;
else b[2] = 0;
if(yb < yl)
b[1] = 1;
else b[1] = 0;
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)).
Disadvantages:
Sometimes it performs needless clipping.
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)/2
=15
Pmy=(-5+10)/2
=2.5
Pm(15,2.5)
Here we replace pm =p1
p1(15,2.5)
Step 4: Processing line segment pm p2 as p1 p2
pm(15,2.5)=p2(20,-5)
pmx=(15+20)/2
=17.5
pmy=(2.5-5)/2
=-1.25
Pm(15.5,-1.25)
Step 5:Processing line segment p1 pm as p1 p2
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)
Step 6: Processing line segment pm p1 as p1 p2
pm(10,10)=p1(-10,40)
pmx=(-10+10)/2
=0
pmy=(40+10)/2
=25
Pm(0,25)
3.Cyrus-Beck Line Clipping Algorithm:-
8.If dot product is positive i.e. n.[P(t) - f] > 0 Then the vector
P(t) - f is pointed towards the interior of R as Shown in fig. (b)
Example: Consider the line from P1(-2,1) to P2(8,4)
clipped to the rectangle region R as shown in the Fig.
(c) .The line P1P2 intersects the windows.Calculate
the intersect points.
Solution:-
1.The parametric representation of the line P1P2 is
P(t)=P1+(P2- P1)t =[-2 1]+([8 4]-[-2 1])
=[-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
Step1 :Choosing f(2,0) for the left edge gives
p(t)-f = (10t - 2) i +(3t+1) j - (2,0)
=(10t - 4) i + (3t + 1) j
nL.[P(t)- f] =10t - 4=0
t = 4/10= 2/5
Substituting values of t in the parametric equation we get.
P(2 / 5 )=[-2 1]+[10 + 3] (2 / 5) (consider parametric equation)
=[-2 1] + [4 6/5]
=[2 2.2]
Step 2: Choosing f(7,5) for the right edge gives
p(t)-f = (10t - 2) i +(3t+1) j - (7,5)
=(10t - 9) i + (3t - 4) j and
NR .[P(t) - f] = - (10 t - 9) = 0
t= 9 / 10
Therefore the substitution value of t in parametric equation we
get.
P(9/10)=[-2 1] + [10 3](9/10)
=[-2 1] + [9 27/10]
=[7 37/10]
=[7 3.7]
Step 3: using f(2,0) for the bottom edge gives
NB .[P(t) - f] =3t + 1 = 0
t= -1/3
Subtituting value of t in parametric equation
=[-2 1] [10 3] x-1/3
=[-2 1] [-10/3 -1]
=[-12/3 0]
=[-4 0]
tL=-W.n/D.n
Else
tU= -W.n/D.n
End if
8.Repeat steps 4 through 7 for each of the clipping
windows
9.If maximum lower limit and minimum upper limit do
not satisfy condition 0<=t<=1 then ignore the line.
10.Calculate the intersection points by substituting
values of maximum lower limit and minimum upper
limit in the parametric equation of the line p1 p2
11.Draw the line segment P(tL) to P(tU).
12. Stop.
Derivative
n.(p(t)-f)=0
n.(p1+(p2-p1)t-f)=0
n(p1+dt-f)=0
n(w+dt)=0
nw+ndt=0 -wn=ndt t=-wn/dn
Liang-Barsky Line Clipping
1.The ideas for the clipping line of Liang-Barsky and Cyrus-
Beck are the same.
2.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.
3.Liang and Barsky have created an algorithm that uses
floating-point arithmetic but finds the appropriate endpoints
with at most four computations.
4.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.
Algorithm steps
Step1:
Δx=x2-x1
Δy=y2-y1
k=1,2,3,4
Step2:
P1= -Δx , q1= x1- xwmin
P2= Δx, q2= Xwmax- X1
P3= -Δy, q3= Y1- Ywmin
P4= Δy, q4= Ywmax -Y1
Step 5:
PK > 0 find t2 final time
t2= min(1,qK/PK)
x=X1+t2Δx
y=Y1+t2Δy
Here t2!=1 outside
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
=8-12=-4
Step2:
PK qK
P1= -Δx =-4 q1=x1- Xwmin=4-5= -1
P2= Δx = 4 q2 = xwmax-x1==9-4=5
P3= -Δy= 4 q3= y1- ywmin=12-5=7
P4= Δy = -4 q4= ywmax - y1=9 - 12 = -3
Step 3:
If any PK=0 line is parallel to the window corresponding
qK < 0 Line outside.
qK> 0 Line inside.
qK=0 within the boundary.
Step 4:
If any PK < 0 find t1 initial time
t1=max(0,qK / PK)
t1= max(0,-1/-4,-3/-4)
t1=3/4
x=x1+t1Δx t1=0 no clipping
=4+3/4x4=7
y=y1+t1Δy t1!=0 need clipping.
=12+3/4x(-4)=9
Step 5:
If any pk >0 find t2 final time
t2=min(1,qK / PK)
t2=min(1,5/4,7/4)
t2=1
x=x1+t2Δx t2=1(no clipping)
x=8
y=y1+t2Δy t2!=1 (clipping)
y=8
Sutherland-Hodgeman polygon clipping algorithm
The polygon clipping algorithm deals with four different
clipping cases. The output of each case is input for the next
case.
Case1) Left clip: In the left side polygon clipping, we only
remove the left part of the polygon, which is outside the
window. We only save the portion which is inside the window.
Case 2) 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