0% found this document useful (0 votes)
28 views

Notes CGR-Unit 04

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Notes CGR-Unit 04

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 90

Windowing And Clipping Concepts:-

1.Window to Viewport Transformation is the process of


transforming a 2D world-coordinate object to device
coordinates.
2.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 coordinate 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
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,

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

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

2.There are four 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

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


the following figure, where the rectangle indicates the window.

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

3.The X-coordinate of the given point is inside the window, if


X lies in between Wx1 ≤ X ≤ Wx2.

4.Same way, Y coordinates of the given point is inside the


window, if Y lies in between Wy1 ≤ Y ≤ Wy2.
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})$.
2.
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 −

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

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

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


//#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void bytecode();
void sutherland();
int a[4],b[4];
float m,xnew,ynew;
float xl = 100, yl = 100, xh = 300, yh = 300,xa = 10,ya =
200,xb = 250, yb = 150;
void main()
{
int gd = DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setcolor(5);
line(xa,ya,xb,yb);
setcolor(12);
rectangle(xl,yl,xh,yh);
m = (yb-ya)/(xb-xa);
bytecode();
sutherland();
getch();
}
void bytecode()
{
if(xa < xl)
a[3] = 1;
else a[3] = 0;

if(xa>xh)
a[2] = 1;
else a[2] = 0;

if(ya < yl)


a[1] = 1;
else a[1] = 0;
if (ya > yh)
a[0] = 1;
else a[0] = 0;

if(xb < xl)


b[3] = 1;
else b[3] = 0;

if(xb>xh)
b[2] = 1;
else b[2] = 0;
if(yb < yl)
b[1] = 1;
else b[1] = 0;

if (yb > yh)


b[0] = 1;
else b[0] = 0;
void sutherland()
{
printf("press a key to continue");
getch();
if(a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3]
== 0 && b[0] == 0 && b[1] == 0 && b[2] == 0 &&
b[3] == 0 )
{
printf("no clipping");
line(xa,ya,xb,yb);
}
else if(a[0]&&b[0] || a[1]&&b[1] || a[2]&&b[2] ||
a[3]&&b[3])
{
// clrscr();
printf("line discarded");
rectangle(xl,yl,xh,yh);
}
else
{
if(a[3] == 1 && b[3]==0)
{
ynew = (m * (xl-xa)) + ya;
setcolor(12);
rectangle(xl,yl,xh,yh);
Setcolor(0);
line(xa,ya,xb,yb);
setcolor(15);
line(xl,ynew,xb,yb);
}
else if(a[2] == 1 && b[2] == 0)
{
ynew = (m * (xh-xa)) + ya;
setcolor(12);
rectangle(xl,yl,xh,yh);
setcolor(0);
line(xa,ya,xb,yb);
setcolor(15);
line(xl,ynew,xb,yb);
}
else if(a[1] == 1 && b[1] == 0)
{
xnew = xa + (yl-ya)/m;
setcolor(0);
line(xa,ya,xb,yb);
setcolor(15);
line(xnew,yh,xb,yb);
}
else if(a[0] == 1 && b[0] == 0)
{
xnew = xa + (yh-ya)/m;
setcolor(0);
line(xa,ya,xb,yb);
setcolor(15);
line(xnew,yh,xb,yb);
}
}
}
Problems of Cohen Sutherland

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
=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
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

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

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}
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:
It is easy to use and implement.
We can perform clipping and testing in a particular manner.
It is a fast algorithm.

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

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 is a boundary point of


the convex region R and n is an inner normal for one of itd
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

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

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


rejected

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


nT.[P(t) - f] = -(3t - 4) =0
t= 4 / 3
Subtituting values of t in parametric equation
=[-2 1] [10 3]x4/3
=[10 3]x4/3
= [10x4/3 3x4/3]
= [40/3 4]
=[40/3 5]
=[40/3 5]
=[13.3 5]

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

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

Step3: if any PK=0 line is parallel to window corresponding


qK< 0 Line is outside
qK> 0 Line is inside
qK = 0 within Boundary
Step 4:
if PK < 0 find t1 initial time
t1= max(0,qK/PK)
x=X1+t1Δx
y=Y1+t1Δy
Here t1!=0 line is outside

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

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

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.
//#include<iostream.h>
//#include<conio.h>
#include<graphics.h>
#define round(a) ((int)(a+0.5))
int k;
float xmin,ymin,xmax,ymax,arr[20],m;
void clipl(float x1,float y1,float x2,float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
if(x1 >= xmin && x2 >= xmin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1 < xmin && x2 >= xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1 >= xmin && x2 < xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
k+=2;
}
}
void clipt(float x1,float y1,float x2,float y2)
{
if(y2-y1)
m=(x2-x1)/(y2-y1);
else
m=100000;
if(y1 <= ymax && y2 <= ymax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1 > ymax && y2 <= ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
arr[k+2]=x2;
arr[k+3]=y2;
}
if(y1 <= ymax && y2 > ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
k+=2;
}
}
void clipr(float x1,float y1,float x2,float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
if(x1 <= xmax && x2 <= xmax)
{
arr[k]=x2;
arr[k+1]=y2;
}
if(x1 > xmax && x2 <= xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1 <= xmax && x2 > xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
k+=2;
}
}
void clipb(float x1,float y1,float x2,float y2)
{
if(y2-y1)
m=(x2-x1)/(y2-y1);
else
m=100000;
if(y1 >= ymin && y2 >= ymin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1 < ymin && y2 >= ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
arr[k+2]=x2;
arr[k+3]=y2;
}
if(y1 >= ymin && y2 < ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
k+=2;
}
}
void main()
{
int gdriver=DETECT,gmode,n,poly[20];
float xi,yi,xf,yf,polyy[20];
clrscr();
cout<<"Coordinates of rectangular clip window
:\nxmin,ymin :";
cin>>xmin>>ymin;
cout<<"xmax,ymax :";
cin>>xmax>>ymax;
cout<<"\n\nPolygon to be clipped :\nNumber of sides :";
cin>>n;
cout<<"Enter the coordinates :";
for(int i=0;i < 2*n;i++)
cin>>polyy[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
for(i=0;i < 2*n+2;i++)
poly[i]=round(polyy[i]);
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
cout<<"\t\tUNCLIPPED POLYGON";
setcolor(WHITE);
fillpoly(n,poly);
getch();
cleardevice();
k=0;
for(i=0;i < 2*n;i+=2)
clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i < k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i < 2*n;i+=2)
clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i < k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i < 2*n;i+=2)
clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i < k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i < 2*n;i+=2)
clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
for(i=0;i < k;i++)
poly[i]=round(arr[i]);
if(k)
fillpoly(k/2,poly);
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
cout<<"\tCLIPPED POLYGON";
getch();
closegraph();
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 −
1.All or none string clipping
2.All or none character clipping
3.Text clipping

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

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

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

You might also like