0% found this document useful (0 votes)
10 views51 pages

Clipping

Uploaded by

dahalgaurab96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views51 pages

Clipping

Uploaded by

dahalgaurab96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

CLIPPING

Khem Nath Khatiwada


• Clipping algorithms for lines, circles, and ellipses
• Polygon clipping algorithms: overview and basic principles
CLIPPING
• What is Clipping?
• Clipping: Technique for not showing that part of the image/drawing which one
is not interested.
• The primary use of clipping in computer graphics is to remove objects, lines, or
line segments that are outside the viewing pane.
• Clipping = Procedure that identify those portion of a picture hat are either
Inside or Outside of a specific region is known as Clipping Algorithm.
• We can apply clipping algorithm to world coordinates. World coordinates
clipping remove those primitives outside the window.
• world coordinates clipping refers to the process of removing parts of
primitives (basic shapes or objects) that lie outside the defined viewing
window or viewport. This optimization step ensures that only the visible
portions of the scene are processed and rendered.
What is Clipping window?
• The region against which an object is clipped.
• Clipping Algorithm for following primitives types:
1. Point clipping
2. LIne Clipping
3. curve Clipping
4. Text Clipping
Polygon Clipping{area}
What is Point Clipping?
The Point Clipping Algorithm is a fundamental algorithm used in Computer Graphics to determine
whether a point lies inside or outside a specific region or boundary.
It is particularly useful when dealing with objects that have complex shapes or when displaying only a
portion of an image.
• Get the minimum and maximum coordinates of both viewing pane.
• Get the coordinates for a point.
• Check whether given input lies between minimum and maximum coordinate of viewing pane.
• If yes display the point which lies inside the region otherwise discard it.
pseudocode
• Assume, clipping window is a rectangle in a Standard Position.
• Save point P(x,y) for display: if the following inequalities are satisfied.
• XWmin ≤ X ≤ XW max
• YWmin < Y < YW Wmax
• Here, Edges of clipping window are: XWmin, XWmax, YWmin, YWmax can be the world coordinate
window boundaries or viewport boundaries.
• NOTE: if any one of these inequalities is not satisfied, the point is clipped [i.e., Not Saved for
Display].
• 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 coordinate of the given point is
inside the window, if Y lies in between Wy1 ≤ Y ≤ Wy2.
#include <iostream> pane int Xmin = 0;
using namespace std; cout<<"\n"<< endl; int Xmax = 350;
// Function for point clipping cout << "Point outside the viewing int Ymin = 0;
pane:"<<endl;
void pointClip(int XY[][2], int n, int Xmin, int int Ymax = 350;
Ymin, int Xmax, int Ymax) for (int i = 0; i < n; i++)
pointClip(XY, 6, Xmin, Ymin, Xmax, Ymax);
{ {
return 0;
cout << "Point inside the viewing pane:" << if ((XY[i][0] < Xmin) || (XY[i][0] > Xmax))
endl; }
cout << "[" << XY[i][0] << "," << XY[i][1]
for (int i = 0; i < n; i++) << "] ";
{ if ((XY[i][1] < Ymin) || (XY[i][1] > Ymax))
if ((XY[i][0] >= Xmin) && (XY[i][0] <= Xmax)) cout << "[" << XY[i][0] << "," << XY[i][1]
<< "] ";
{ }
if ((XY[i][1] >= Ymin) && (XY[i][1] <= }
Ymax))
// Driver code
cout <<"[" << XY[i][0] <<","<<XY[i]
[1]<<"] "; int main()

} {

} int XY[6][2] = {{10, 10}, {-10, 10}, {400, 100},


{100, 400}, {400, 400}, {100, 40}};
// print point coordinate outside viewing
What is Line Clipping?
• The concept of line clipping is same as point clipping. In line clipping, we
will cut the portion of line which is outside of window and keep only the
portion that is inside the window.
• Lines that do not intersect the clipping window are either completely
inside / outside the window.
• Clipping categories for Line Segments:
1. Visible: Both endpoints of line segments lie within the window.
2. Non-Visible: Line Segments lie outside the window.
Line Clipping Algorithm Includes:
a)Cohen-Sutherland Method
b)Liang Barsky Method
c)Nicholl Lee Nicholl Method
• Cohen–Sutherland
• In computer graphics, the Cohen–Sutherland algorithm (named after
Danny Cohen and Ivan Sutherland) is a line-clipping algorithm.
• The algorithm divides a 2D space into 9 regions, of which only the
middle part (viewport) is visible.
• In 1967, flight-simulation work by Danny Cohen led to the
development of the Cohen–Sutherland computer graphics two- and
three-dimensional line clipping algorithms, created with Ivan
Sutherland.
• Cohen Sutherland Line Clipping Algorithm:
• In the algorithm, first of all, it is detected whether line lies inside the
screen or it is outside the screen. All lines come under any one of the
following categories:
• Visible
• Not Visible
• Clipping Case
• 1. Visible: If a line lies within the window, i.e., both endpoints of the line
lies within the window. A line is visible and will be displayed as it is.
• 2. Not Visible: If a line lies outside the window it will be invisible and
rejected. Such lines will not display. If any one of the following
inequalities is satisfied, then the line is considered invisible.
• This algorithm uses the clipping window as shown in the following
figure. The minimum coordinate for the clipping region is
(XWmin,YWmin)
and the maximum coordinate for the clipping region is (XWmax,YWmax).
• 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.
• 3. Clipping Case: If the line is neither visible case nor invisible case. It is
considered to be clipped case. First of all, the category of a line is found
based on nine regions given below. All nine regions are assigned codes. Each
code is of 4 bits. If both endpoints of the line have end bits zero, then the
line is considered to be visible. The center area is having the code, 0000,
i.e., region 5 is considered a rectangle window.
Following figure show lines of various types

Line AB is the visible case


Line OP is an invisible case
Line PQ is an invisible line
Line IJ are clipping candidates
Line MN are clipping candidate
Line CD are clipping candidate

Advantage of Cohen Sutherland Line Clipping:


• It calculates end-points very quickly and
rejects and accepts lines quickly.
• It can clip pictures much large than screen
size.
• Algorithm of Cohen Sutherland Line else
Clipping: And=0000
Step1:Calculate positions of both Line is considered the clipped case.
endpoints of the line
Step2:Perform OR operation on
both of these end-points
Step3:If the OR operation gives
0000
Then
line is considered to be visible
else
Perform AND operation on both
endpoints
If And ≠ 0000
then the line is invisible
• Cohen-Sutherland Method - Example
• Use the Cohen Sutherland Algorithm to Clip line P1(70,20), P2(100,10)
against a window. Consider rectangular window ABCD, A(50,10),
B(50,40), C(80,40) and D(80,10).
• Step 5:
• Find the Intersection of line P1,P2 with right edge of window i.e.,
Point P2.
• Intersection point is found by solving the equation representing the
time segment and boundary lines.
• Line Intersection and Clipping
• For Left window Edge, intersection point will be (xL,y)
• For Right window Edge, intersection point will be (XR,y)
• For Top window Edge, intersection point will be (x, yT)
• For Bottom window Edge, intersection point will be (x, yB)
• Illustrating the Cohen-Sutherland algorithm with practical examples and exercises Clip a
line A (-1,5) and B (3,8) using the Cohen Sutherland algorithm with window coordinates
(-3,1) and (2,6).
• Here xmin =-3 and ymin=1 with xmax =1 and ymax=6 with x1=-1, y1=5 and x2=3, y2=8.
• The (x1,y1) has the code (0000) and (x2,y2) has the code (1010).
Which category this line belongs to: Find, does it require clipping?
• Logical AND of both the region codes are (0000) * (1010) = (0*1, 0*0 ,0*1,0*0) = (0000).
• Since the logical AND of codes is zero, so the line belongs to third category- Clipping
category =>Computer would clip it.
• The line “Needs Clipping” based on the calculation above.
• S-4: Since Bit 1 is one therefore intersection point is y=ymax and x=x1+(y1-ymax)/m. =>
y=6 and x= -1+(6-5)/(3/4) => -1+4/3 => 1/3.
• So the intersection point would be (x=1/3,y=6).
• S-5: Find the region code of newfound point, C(x=1/3 and y=6) =>(0000)
• Since the starting point, A(-1,5) has (0000) and new Point C is also (0000) that means both
endpoints are visible. The line does not need more clipping. The algorithm would stop
here.
• Use the Cohen Sutherland Algorithm to clip the lines P1(70, 20) and
P2(100, 10) against a window lower left corner (50, 10) and upper
right corner (80, 40).
• Given,
• P1 = (70, 20), P2 = (100, 10)
• (Xwmin, Ywmin) = (50, 10)
• (Xwmax, Ywmax) = (80, 40)
• Region Code of P1 = 0000
• Region Code of P2 = 0010
• P1 && P2 = 0000
• The line lies partially inside the window boundary.
• slope of P1.P2 = 10– 20 / 100–70. = -1/3
• Slope of P1.M = y — 20/ 80–70. = y — 20/10
• Since, Slope of P1.P2 == Slope of P1.M and since it is completely one single line,
• - 1/3 = y — 20/10
• 3y — 60 + 10 = 0
• 3y — 50 = 0
• y = 16.67
• Visible Portion of P1.M, P1(70, 20) and M (80, 16.67)
• Use the Cohen Sutherland algorithm to clip two lines P1 (35,10),
−P2(62,40) and P3 (65,20)−P4(95,10) against a window A
(50,10),B(80,10),C(80,40),D(50,40).
• Use the Cohen Sutherland algorithm to clip two lines P1(35,10)-
P2(65,40) and P3(65,20)-P4(95,10) against a window A(50,10),
B(80,10), C(80,40) and D(50,40).
• let ABCD be a rectangle window with
A(20,20),B(90,20),C990,70),D(20,70).Find the region codes for the
end points and use Cohen Sutherland line clipping algorithm to clip
the following linesP1P2 with p1(10,30)and p2(80,90)and Q1Q2with
Q1(10,10)and Q2(70,60).
• SOLUTION
• Lets check the line P1P2 ,P1-(10,30) P2-(80,90)
• As clipping window is 0000 and P1 lies to the left of the window the
area code of P1 is 0001
• P2 lies at the top of the clipping window SO its area code is 1000.
• Lets LOGICAL AND AREA CODES OF P1 & P2.
• 0001 ^ 1000 = 0000
• Since the logical AND operation of P1&P2 is 0000 P1P2 lies partially
inside the window.
• Now lets find out location of P1 & P2 | using slope of the line P1P2.
• slope = (y2-y1)/ (x2-x1)
Slope of P1P2 = (90-30)/ (80-10 )=60/ 70
Now we know the slope. Since P1l is a point on line P1P2, the slope will remain same for P1 P2.
• Since P1 is on line AD the x-co-ordinate of P1 is 20. Now we only need to find its y-co-ordinate.
• Lets again use the SLOPE formula but this time for P1 P2.
• Slope of P1| P2 = 90-y1/80-20 = 60/70
• 90-y1=60x60/70
• 90-y1=3600/70
• y1=38.57
• For simplicity lets keep it 38.
• Therefore co-ordinates of P1 are (20,38).
• Now lets take a look at line Q1Q2.
• Q1 lies outside the clipping window and its code will be 0101 as its in the bottom left corner as
compared to clipping window.
• The code of Q2 will be 0000 as it lies inside the clipping window.
• Lets perform AND operation on codes of Q1&Q2.
• 0101 ^ 0000 = 0000.
• SO line Q1Q2 lies partially inside the window.
• Now as Q1' lies on line AB its y-coordinate will be 20, to find its x-coordinate lets
use the slope formula.
• For Line Q1'Q2 slope will be same as Q1Q2 as its part of the same line.
• slope =(60-20)/ 70-x1 =40/(70-x1 )=0.83
• 70-x1= 40/ 0.83
• x1=70-48.19=21.80
• For simplicity let assume it as 21.
• Therefore the co-ordinates of Q1' are (21,20)
Since Q1 lies outside the Clipping window, it will be clipped and we will have only
Q1'Q2 inside the window.
To find co-ordinate positions of Q1' we first find the slope.
slope = y2-y1 /x2-x1
Where x1 & y1 are co-ordinates of Q1 and x2 & y2 are co-ordinates of Q2.
slope = 60-10 / 70-10 =50/60= 0.83
• Now lets take a look at line Q1Q2.
• Q1 lies outside the clipping window and its code will be 0101 as its in the bottom left
corner as compared to clipping window.
• The code of Q2 will be 0000 as it lies inside the clipping window.
• Lets perform AND operation on codes of Q1&Q2.
• 0101 ^ 0000 = 0000.
• SO line Q1Q2 lies partially inside the window
• Now as Q1' lies on line AB its y-coordinate will be 20, to find its x-coordinate lets use the
slope formula.
• For Line Q1'Q2 slope will be same as Q1Q2 as its part of the same line.
slope = 60-20 /70-x1 =40/70-x1 = 0.83
• (70-x1)=40/0.83
• x1=70-48.19=21.80
• For simplicity let assume it as 21.
• Therefore the co-ordinates of Q1' are (21,20)
• Let ABCD be a rectangular window with A (20,20), B (90,20), ((90,70) and
D(20,70). find region codes for the endpoints and use Cohen Sutherland
algo. to clip the line Plp2with P1 (10,30) and P2 (80,90).
• Solution
• finding Region code
• P₁ = 0001
• P2=1000
• ANDing p1 and p2 = 0000
• Since it is 0000, luie is partially visible so needs clipping = 6 = 0.857 60
• slope, m =y2-y1/x2-x1=90-30/80-10=60/70=6/7=.857
• Therefore,
• y=m(xmin-x1)+y1
• Y=0.857(20-10)+30= 8057 +30=38.57
• Pseudocode
• Step 1 : Assign a region code for two endpoints of given line
• Step 2 : If both endpoints have a region code 0000 then given line is completely
inside and we will keep this line
• Step 3 : If step 2 fails, perform the logical AND operation for both region codes.
• Step 3.1 : If the result is not 0000, then given line is completely outside.
• Step 3.2 : Else line is partially inside.
• Step 3.2.a : Choose an endpoint of the line that is outside the given rectangle.
• Step 3.2.b : Find the intersection point of the rectangular boundary (based on
region code).
• Step 3.2.c : Replace endpoint with the intersection point and update the region
code.
• Step 3.2.d : Repeat step 2 until we find a clipped line either trivially accepted or
rejected.
• Step 4 : Repeat step 1 for all lines
#include<iostream> { { if(accept==TRUE) cin>>xmin;
#include<graphics.h> if(outcode0==0 && y=y0+(y1-y0)*(xmin- { cout<<"Enter the left coordinates
typedef unsigned int outcode; outcode1==0) x0)/(x1-x0); line(x0,y0,x1,y1); of the window:";
enum{TOP=0x1,BOTTOM=0x2,RIG { x=xmin; } cin>>ymin;
HT=0x4,LEFT=0x8}; accept=TRUE; } } cout<<"Enter the right
using namespace std; done=TRUE; else coordinates of the window:";
outcode CompOutCode(double
outcode } { x,double y,double xmin,double cin>>xmax;
CompOutCode(double ,double ,d else if(outcode0 & outcode1) y=y0+(y1-y0)*(xmax- xmax,double ymin,double ymax) cout<<"Enter the top coordinates
ouble ,double ,double ,double ); x0)/(x1-x0); { of the window:";
{
void CSLCAD(double x0,double x=xmax; outcode code=0; cin>>ymax;
y0,double x1,double y1,double done=TRUE;
xmin,double xmax,double } } if(y>ymax)
rectangle(xmin,ymin,xmax,ymax);
ymin,double ymax) else if(ocd==outcode0) code|=TOP;
cout<<"Enter the
{ { { if(y<ymin) coordinates(Terminal Points) of
outcode double x,y; x0=x; code|=BOTTOM; the line: ";
outcode0,outcode1,outcodeout; y0=y; if(x>xmax) cin>>x0>>y0;
int ocd=outcode0 ?
boolean accept=FALSE, outcode0:outcode1; code|=RIGHT; cin>>x1>>y1;
done=FALSE; outcode0=CompOutCode(x0,y0,x
if(ocd & TOP) if(x<xmin) line(x0,y0,x1,y1);
min,xmax,ymin,ymax);
outcode0=CompOutCode(x0,y0,x { code|=LEFT; delay(5000);
}
min,xmax,ymin,ymax); x=x0+(x1-x0)*(ymax- return code; cleardevice();
y0)/(y1-y0); else
}
outcode1=CompOutCode(x1,y1,x y=ymax; { CSLCAD(x0,y0,x1,y1,xmin,xmax,y
int main()
min,xmax,ymin,ymax); } x1=x; min,ymax);
{
else if(ocd & BOTTOM) y1=y;
cout<<"outcode0="<<outcode0<< string ch; rectangle(xmin,ymin,xmax,ymax);
endl; { double
outcode1=CompOutCode(x1,y1,x delay(50000);
x=x0+(x1-x0)*(ymin- min,xmax,ymin,ymax); xmin,xmax,ymin,ymax,x0,y0,x1,y
closegraph();
cout<<"outcode1="<<outcode1<<y0)/(y1-y0); }
1;
endl; y=ymin; initwindow(500,600); }
}
do } cout<<"Enter the bottom co-
}while(done==FALSE); ordinates of window:";
else if(ocd & LEFT)
#include<iostream.h> drawwindow(); void drawwindow() { ptemp.y = p.y; (p1.code[2] == '1')) {
#include<conio.h> getch(); setcolor(RED); return (ptemp); m = (float)(p2.y - p1.y) / (p2.x -
#include<stdlib.h> drawline(p1, p2, 15); line(150, 100, 450, 100); } p1.x);
#include<dos.h> getch(); line(450, 100, 450, 350); int visibility(PT p1, PT p2) { k = (p1.y + (m * (x - p1.x)));
#include<math.h> p1 = setcode(p1); line(450, 350, 150, 350); int i, flag = 0; temp.y = k;
#include<graphics.h> p2 = setcode(p2); line(150, 350, 150, 100); for (i = 0; i < 4; i++) { temp.x = x;
v = visibility(p1, p2); } if ((p1.code[i] != '0') || for (i = 0; i < 4; i++)
typedef struct coordinate { switch (v) { void drawline(PT p1, PT p2, int cl) { (p2.code[i] != '0')) temp.code[i] = p1.code[i];
int x, y; case 0: setcolor(cl); flag = 1; if (temp.y <= 350 && temp.y
} >= 100)
char code[4]; cleardevice(); line(p1.x, p1.y, p2.x, p2.y);
if (flag == 0) return (temp);
} drawwindow(); PT setcode(PT p) {
return (0); }
PT; drawline(p1, p2, 15); PT ptemp;
for (i = 0; i < 4; i++) { if (p1.code[0] == '1')
void drawwindow(); break; if (p.y < 100)
if ((p1.code[i] == p2.code[i]) y = 100;
void drawline(PT p1, PT p2, int cl); case 1: ptemp.code[0] = '1';
&& (p1.code[i] == '1')) if (p1.code[1] == '1')
PT setcode(PT p); cleardevice(); else
flag = 0; y = 350;
int visibility(PT p1, PT p2); drawwindow(); ptemp.code[0] = '0';
} if ((p1.code[0] == '1') ||
PT resetendpt(PT p1, PT p2); break; if (p.y > 350) (p1.code[1] == '1')) {
if (flag == 0)
main() { case 2: ptemp.code[1] = '1'; m = (float)(p2.y - p1.y) / (p2.x -
return (1);
int gd = DETECT, gm, v; cleardevice(); else p1.x);
return (2);
PT p1, p2, ptemp; p1 = resetendpt(p1, p2); ptemp.code[1] = '0'; k = (float) p1.x + (float)(y -
} p1.y) / m;
initgraph( & gd, & gm, "c:\\tc\\ p2 = resetendpt(p2, p1); if (p.x > 450)
bgi "); PT resetendpt(PT p1, PT p2) { temp.x = k;
drawwindow(); ptemp.code[2] = '1';
cleardevice(); PT temp; temp.y = y;
drawline(p1, p2, 15); else
cout << "\n\n\t\tENTER END- int x, y, i; for (i = 0; i < 4; i++)
break; ptemp.code[2] = '0';
POINT 1 (x,y): "; float m, k; temp.code[i] = p1.code[i];
} if (p.x < 150)
cin >> p1.x >> p1.y; if (p1.code[3] == '1') return (temp);
getch(); ptemp.code[3] = '1';
cout << "\n\n\t\tENTER END- x = 150; } else
POINT 2 (x,y): "; closegraph(); else
if (p1.code[2] == '1') return (p1);
cin >> p2.x >> p2.y; return (0); ptemp.code[3] = '0';
x = 450; }
cleardevice(); } ptemp.x = p.x;
if ((p1.code[3] == '1') ||
• Line clipping
• Line clipping is the process of removing (clipping) lines or portions of lines
outside an area of interest (a viewport or view volume). Typically, any part
of a line which is outside of the viewing area is removed.
• There are two common algorithms for line clipping: Cohen–Sutherland and
Liang–Barsky.
• 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 area or
volume.
• Then, intersection calculations are carried out with one or more clipping
boundaries.
• 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.
• Step4: • (c) If bit 3 is "1" line intersects with bottom boundary
• If a line is clipped case, find an intersection with
boundaries of the window • X3=X1+(y-y1)/m
• m=(y2-y1 )(x2-x1)
• (a) If bit 1 is "1" line intersects with left boundary of • where y = ywmin
rectangle window ywmin is the minimum value of Y co-
ordinate of the window
• y3=y1+m(x-X1 )
• (d) If bit 4 is "1" line intersects with the top
• where X = Xwmin boundary
• X3=X1+(y-y1)/m
• where Xwminis the minimum value of X co-ordinate of
• where y = ywmax
window
• (b) If bit 2 is "1" line intersect with right boundary • ywmax is the maximum value of Y co-ordinate of the
window
• y3=y1+m(X-X1)

• where X = Xwmax

• where X more is maximum value of X co-ordinate of


the window
Example of Cohen-Sutherland Line Clipping Algorithm:
Let R be the rectangular window whose lower left-hand corner is at L (-3, 1) and upper right-hand corner is at R (2, 6). Find
the region codes for the endpoints in fig:

So
A (-4, 2)→ 0001 F (1, 2)→ 0000
B (-1, 7) → 1000 G (1, -2)
→0100
C (-1, 5)→ 0000 H (3, 3) →
0100
D (, 8) → 1010 I (-4, 7) → 1001
E (-2, 3) → 0000 J (-2, 10) →
1000
• The region code for point (x, y) is set according to the scheme
Bit 1 = sign (y-ymax)=sign (y-6) Bit 3 = sign (x-xmax)= sign (x-2)
Bit 2 = sign (ymin-y)=sign(1-y) Bit 4 = sign (xmin-x)=sign(-3-
So
x) A (-4, 2)→ 0001 F (1, 2)→ 0000
B (-1, 7) → 1000 G (1, -2)
• Here →0100
C (-1, 5)→ 0000 H (3, 3) → 0100
D (, 8) → 1010 I (-4, 7) → 1001
E (-2, 3) → 0000 J (-2, 10) →
1000
We place the line segments in their appropriate categories by testing the
region codes found in the problem.
Category1 (visible): EF since the region code for both endpoints is 0000.
Category2 (not visible): IJ since (1001) AND (1000) =1000 (which is not
0000).
Category 3 (candidate for clipping): AB since (0001) AND (1000) = 0000,
CD since (0000) AND (1010) =0000, and GH. since (0100) AND (0010)
=0000.
The candidates for clipping are AB, CD, and GH.
• We place the line segments in their appropriate categories by testing the
region codes found in the problem.
• Category1 (visible): EF since the region code for both endpoints is 0000.
• Category2 (not visible): IJ since (1001) AND (1000) =1000 (which is not
0000).
• Category 3 (candidate for clipping): AB since (0001) AND (1000) =
0000, CD since (0000) AND (1010) =0000, and GH. since (0100) AND (0010)
=0000.
• The candidates for clipping are AB, CD, and GH.
• The algorithm
• The algorithm includes, excludes or partially includes the line based on whether:
• Both endpoints are in the viewport region (bitwise OR of endpoints = 0000): trivial accept.
• Both endpoints share at least one non-visible region, which implies that the line does not
cross the visible region. (bitwise AND of endpoints ≠ 0000): trivial reject.
• Both endpoints are in different regions: in case of this nontrivial situation the algorithm
finds one of the two points that is outside the viewport region (there will be at least one
point outside).
• The intersection of the outpoint and extended viewport border is then calculated (i.e.
with the parametric equation for the line), and this new point replaces the outpoint.
• The algorithm repeats until a trivial accept or reject occurs.
• The numbers in the figure below are called outcodes. An outcode is computed for each of
the two points in the line. The outcode will have 4 bits for two-dimensional clipping, or 6
bits in the three-dimensional case.
• The first bit is set to 1 if the point is above the viewport. The bits in the 2D outcode
represent: top, bottom, right, left. For example, the outcode 1010 represents a point that
is top-right of the viewport.
left central right
top 1001 1000 1010 top, bottom, right, left
central 0001 0000 0010
bottom 0101 0100 0110

Note that the outcodes for endpoints must be recalculated on


each iteration after the clipping occurs.

The Cohen–Sutherland algorithm can be used only on a


rectangular clip window.

You might also like