Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Window to Viewport Transformation is the process of transforming a 2D world-coordinate object to device

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

(xv, yv): Corresponding point on Viewport

Chapter 04 Compiled By Dashrath B Kale


● we have to calculate the point (xv, yv)

Now the relative position of the object in Window and Viewport are the same​.
For x coordinate,

x​v​-x​vmin​=x​vmax​-x​vmin​(x​w​-x​wmin​/x​wmax​-x​wmin​)

x​v​-x​vmin=​x​w​-x​wmin​(x​vmax​-x​vmin/​x​wmax​-x​wmin​)

x​v​-x​vmin​=x​w​-x​wmin​(s​x​)

x​​ v= x​​ vmin +​​ (x​


​ w​-x​wmin​)s​x

For y coordinate,

Y​v​-Y​vmin​=Y​vmax​-Y​vmin​(Y​w​-Y​wmin​/Y​wmax​-Y​wmin​)

Chapter 04 Compiled By Dashrath B Kale


Y​v​-Y​vmin​=Y​w​-Y​wmin​(Y​vmax​-Y​vmin​/Y​wmax​-Y​wmin​)
Y​v​-Y​vmin​=Y​w​-Y​wmin​(s​Y​)
​Y​v= ​Y​vmin ​+​ ​(Y​w​-Y​wmin​)s​Y
● 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 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

5.So, now calculate the point on viewport ( Xv, Yv ).


Xv = 30 + ( 30 - 20 ) * ( 30 / 60 ) = 35

Chapter 04 Compiled By Dashrath B Kale


Yv = 40 + ( 80 - 40 ) * ( 20 / 40 ) = 60
7.So, the point on the window ( Xw, Yw ) = ( 30, 80 ) will be ​( Xv, Yv ) = ( 35, 60 ) ​on viewport.

Advantage of Viewing Transformation:


We can display pictures on devices or display systems according to our need and choice.

Note:

● World coordinate system is selected according to the application program.


● Screen coordinate system is chosen according to the need of design.
● 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.
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

Clipping a point from a given window is very easy. Consider the


following figure, where the rectangle indicates the window. Point
clipping tells us whether the given point (X, Y) is within the given
window or not; and decides whether we will use the minimum and
maximum coordinates of the window.
The X-coordinate of the given point is inside the window, if X lies in
between Wx1 ≤ X ≤ Wx2. Same way, Y coordinates of the given point is
inside the window, if Y lies in between Wy1 ≤ Y ≤ Wy2.

Chapter 04 Compiled By Dashrath B Kale


Line 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.

Cohen-Sutherland Line Clippings

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})$.

We will use 4-bits to divide the entire


region. These 4 bits represent the Top,
Bottom, Right, and Left of the region as
shown in the following figure. Here, the
TOP and LEFT bit is set to 1 because it is the
TOP-LEFT corner.

There are 3 possibilities for the line −


● Line can be completely inside the
window ​(This line should be accepted).
Bitwise OR of region of two end points of
line is 0 (Both points are inside the rectangle)
● Line can be completely outside of the window
(This line will be completely removed from the
region).​Both endpoints share at least one outside region
which implies that the line does not cross the visible
region. (bitwise AND of endpoints != 0).
● 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
corner point and the algorithm repeats
Algorithm

Step 1 − Assign a region code for each endpoint.


Step 2 − If both endpoints have a region code 0000 then accept this line.
Step 3 − Else, perform the logical ANDoperation for both region codes.
Step 3.1 − If the result is not 0000, then reject the line.
Step 3.2 − Else you need clipping.
Step 3.2.1 − Choose an endpoint of the line that is outside the window.
Step 3.2.2 − Find the intersection point at the window boundary (base on region code).
Step 3.2.3 − Replace endpoint with the intersection point and update the region code.
Step 3.2.4 − Repeat step 2 until we find a clipped line either trivially accepted or trivially rejected.
Step 4 − Repeat step 1 for other lines.

Problems of Cohen Sutherland

Chapter 04 Compiled By Dashrath B Kale


1.Use Cohen- sutherland algorithm to to clip the line p1(40,15) p2(75,45) against windows
A(50,10),B(80,10),C(80,40) and D(50,40)
Solution:-

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

Step 5: Calculate value of c for point A1


From y=mx+c
c=y-mx

c=15-(0.857)x40
=15-34.28
c=-19.28
Step 6: calculate value for Y
y=mx+c

Chapter 04 Compiled By Dashrath B Kale


=0.857x(50)+(-19.28)
=23.57
Therefor P1`=(50,23.57)
Step 7: B1 is 1000 ,intersect top edge y=40 so we have to compute only x co -ordinate of B1
Y=mx+c
x=[y-c]/m
x=[40+19.28]/0.857
=59.28/0.857
=69.17
Thus P2`=(69.17,40)
Step 8: Final endpoints
P1`=(50,23.57) and P2`=(69.17,40) is visible on the rectangle.

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

Step2:A1 OR B1= 0000 OR 0010


= 0010
Result is not zero so line is not completely
visible
Step3: A1 AND B1= 0000 AND 0010
=0000
Not completely outside
Step 4: calculate visible portion of line Point P2
x=xmin=80 therefore x coordinate 70 then compute y ?
Slope of line m=(y2-y1)/(x2-x1)
=(10-20)/(100-70)
= -10/30
=-0.33
Step 5: Calculate the the value of C
y=mx+c
c=y-mx
= 20+0.33*70
=43.1

Chapter 04 Compiled By Dashrath B Kale


Therefore y=-0.33x70+43.1
=16.7
Step 5: Final endpoint of P1 and P2
P1(70,20),P2(80,16.17)

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).

2.Midpoint subdivision algorithm

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

Chapter 04 Compiled By Dashrath B Kale


4.As shown in the figure (k), line P1 P2 is partially visible. It is subdivided in two equal Parts P1 P3 and P3 P2 (see
Fig. k (b)).

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.

Algorithm Midpoint Subdivision Line Clipping:


Step 1: Assign the bit code for both endpoints of the line.

Step 2: Now,implement OR operation on both endpoints of the line.

Step 3: If the OR = 0000,


Then
{The line is Visible}
Else
{Implement AND operation on endpoints}
Then
If AND ≠ 0000
Then
{The line is Invisible}
Else
AND = 0000
{The line is the partially visible}
Chapter 04 Compiled By Dashrath B Kale
Step 4: For the partially visible line, we need to find the midpoint.
Xm = (x1 + x2)/2 (For x coordinate)
Ym = (y1 + y2)/2 (For y coordinate)

Step 5: We Need to check that the line is near to the boundary of the window or not.

Step 6: If the line is visible or invisible, then repeat steps 1 to 5.

Step 7: Stop.

Advantages:

1. It is easy to use and implement.

2. We can perform clipping and testing in a particular manner.

3. It is a fast algorithm.

Disadvantages:

1. 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)/5
=15
Pmy=(-5+10)/2
=-2.5

Pm(15,2.5)

Here we replace pm =p1

Chapter 04 Compiled By Dashrath B Kale


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)

Chapter 04 Compiled By Dashrath B Kale


Chapter 04 Compiled By Dashrath B Kale
Chapter 04 Compiled By Dashrath B Kale
Chapter 04 Compiled By Dashrath B Kale
Chapter 04 Compiled By Dashrath B Kale
3.Cyrus-Beck Line Clipping Algorithm:-

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

Chapter 04 Compiled By Dashrath B Kale


Fig.(a) Convex region, boundary point and an inner normal

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

7. If dot product is zero i.e


n.[P(t) - f] = 0
Then P(t) - f is pointed parallel to the plane containing f and perpendicular to the normal.
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)

Chapter 04 Compiled By Dashrath B Kale


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]+[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
n​L​.[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
N​R .[P(t)
​ - f] = - (10 t - 9) = 0
t= 9 / 10
Therefore the substitution value of t in the parametric equation we get.
P(9/10)=[-2 1] + [10 3](9/10)
=[-2 1] + [9 27/10]
Chapter 04 Compiled By Dashrath B Kale
=[7 37/10]
=[7 3.7]

Step 3: using f(2,0) for the bottom edge gives

N​B​ .[P(t) - f] =3t + 1 = 0


t= -1/3

This value of t is outside the range of 0<=t <=1v and hence it is rejected

Step 4: using f(7,5) for the top edge given

n​T​.[P(t) - f] = -(3t - 4) =0
t= 4 / 3

This value of t is outside the range of 0<=t<=1 and hence it is rejected.

Thus we get two intersection points (2.2.2) and (7,3.7) with left edge and right edge respectively.

1.Cyrus -Beck Line Clipping Algorithm.

1.Read two end points of the line ,say p1 and p2


2.Read vertex coordinate of the clipping windows.
3.Calcualte D=P2-P1
4.Assign boundary point(f) with particular edge.
5.Find inner normal vector for corresponding edge.
6.Calculate D.n and W=P1-f
7.If D.n >0

t​L​=-W.n/D.n
Else

t​U​= -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(t​L) to
​ P(t​U​).
12. Stop.

Liang-Barsky Line Clipping

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.

Chapter 04 Compiled By Dashrath B Kale


Algorithm

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

Step3: if any P​K​=0 line is parallel to window corresponding

q​K​< 0 Line is outside

q​K​> 0 Line is inside

q​K =​ 0 within Boundary

Step 4: if px < 0 find t1 initial time

Chapter 04 Compiled By Dashrath B Kale


t1= max(0,q​K​/P​K​)

x=X1+t1​Δx

y=Y1+t1Δy

Here t1!=0 line is outside

Step 5: P​K​ > 0 find t2 final time

t2= min(1,​q​K​/P​K​)

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

Chapter 04 Compiled By Dashrath B Kale


=8-12=-4

Step2:

P​K q​K

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= ymax - y1=9 - 12 = -3

Step 3:

If any P​K​=0 line is parallel to the window corresponding

q​K​ < 0 Line outside.

q​K​> 0 Line inside.

q​K​=0 within the boundary.

Step 4:

If any P​K​ < 0 find t1 initial time

t1=max(0,q​K ​/ P​K​)

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,​q​K /​ P​K​)

t2=min(1,5/4,7/4)

t2=1

x=x1+t2Δx t2=1(no clipping)

Chapter 04 Compiled By Dashrath B Kale


x=8

y=y1+t2Δy t2!=1 (clipping)

ADVANTAGES OF LIANG BARSKY ALGORITHM


1.It is more efficient then the Sutherland and Cohen algorithm because intersection calculations are reduced.
2.It required only one division to update parameter p1 and p2.
3.Window intersection of the lines are computed only once.

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.

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.

Chapter 04 Compiled By Dashrath B Kale


Case4) Bottom clip: ​In the bottom side polygon clipping, we only remove the bottom part of the polygon, which is
outside the window. We only save the portion which is inside the window.

There should be the following conditions while we clip a polygon.


Condition 1(Out-In): ​If the first vertex of the polygon is outside and the second vertex is inside the window, then
the output will be the intersection point and second vertex.

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.

Chapter 04 Compiled By Dashrath B Kale


Condition 3(In-In): ​If both vertices of the polygon are inside the window, then the output will be the second
vertex.

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.

Sutherland-Hodgeman Polygon Clipping Algorithm:-

1. Read coordinates of all vertices of the Polygon.


2. Read coordinates of the dipping window
3. Consider the left edge of the window
4. Compare the vertices of each edge of the polygon, individually with the clipping plane.
5. Save the resulting intersections and vertices in the new list of vertices according to four possible
relationships between the edge and the clipping boundary.
6. Repeat steps 4 and 5 for remaining edges or the clipping window. Each time the resultant list of vertices is
successively passed to process the next edge of the clipping window.
7. Stop.1

Chapter 04 Compiled By Dashrath B Kale


Example :- For a polygon and clipping window shown in figure below give the list of vertices after each boundary
clipping.

Solution:- Original polygon vertices are V1, V2, V3, V4, and V5. After clipping each boundary the new vertices are
as shown in figure above.

Program Write aProgram using sutherland -Hodgeman line clipping algorithm

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

The following figure shows all or none string 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.

The following figure shows text clipping −

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.

Chapter 04 Compiled By Dashrath B Kale

You might also like