SlideShare a Scribd company logo
Program 5
Program to implement Cohen-Suderland Line
Clipping Algorithm. Make provision to specify
the input line, window for clipping and view
port for displaying the clipped image.
Cohen-Sutherland Line-Clipping:
• It is a very good algorithm for clipping pictures that are
larger than the screen.
• This algorithm divides a 2D space into 9 parts, of which
only the middle part (view port) is visible.
• The 9 regions can be uniquely identified using a 4 bit
code. This 4 bit code is called outcode.
Cohen-Sutherland Line-Clipping
Outcode
9 8 10
1 0 2
5 4 6
We use the order Left,
Right, Bottom, Top for
these 4 bits.
Cohen-Sutherland Line-Clipping
Outcodes
1001 1000 1010
0001 0000 0010
0101 0100 0110
We use the order Left,
Right, Bottom, Top for
these 4 bits.
Cohen-Sutherland Line-Clipping
Region Outcodes
1001 1000 1010
0001 0000 0010
0101 0100 0110
Xmin Xmax
Ymin
Ymax
Xmin
Xmax
Ymin
Ymax
Cohen-Sutherland Line-Clipping
Region Outcodes
Bit Number 1 0
Fourth (LSB) Left of left edge X<Xmin Right of left edge X>Xmin
Third Right of right edge X>Xmax Left of right edge X<Xmax
Second Below bottom edge Y<Ymin Above bottom edge Y>Ymin
First (MSB) Above top edge Y>Ymax Below top edge Y<Ymax
1001 1000 1010
0001 0000 0010
0101 0100 0110
Xmin
Xmax
Ymin
Ymax
Line-Clipping Algorithm
Assume two endpoints are p0 and p1
1. If code(p0) OR code(p1) is 0000,
• the line can be trivially accepted.
• the line is drawn.
2. If code(p0) AND code(p1) is NOT 0000,
• the line can be trivially rejected.
• the line is not drawn at all.
3. Otherwise, compute the intersection points of the
line segment and window boundary lines (make
sure to check all the boundary lines)
Line-Clipping
1001 1000 1010
0001 0000 0010
0101 0100 0110
Xmin
Xmax
Ymin
Ymax
L1
L2
L3
Line L1
code1 = 0000, code2 = 0000
Is code1|code2==0? Trivial accept
Is code1 & code2!=0? Trivial reject
Else clip the line until one code becomes 0
code1 | code2 = 0000 | 0000 = 0000
Trivial accept completely – no need to clip.
Line-Clipping
1001 1000 1010
0001 0000 0010
0101 0100 0110
Xmin
Xmax
Ymin
Ymax
L1
L2
L5
Line L2
code1 = 0101, code2 = 0100
Is code1|code2==0? Trivial accept
Is code1 & code2!=0? Trivial reject
Else clip the line until one code becomes 0
code1 | code2 = 0101 | 0100 = 0101 (NOT 0000) : No Trivial accept
Code1 & code2 = 0101 & 0100 = 0100 (NOT 0000) : Trivial reject
Line-Clipping
1001 1000 1010
0001 0000 0010
0101 0100 0110
Xmin
Xmax
Ymin
Ymax
L1
L2
L5
Line L5
code1 = 0000, code2 = 1010
Is code1|code2==0? Trivial accept
Is code1 & code2!=0? Trivial reject
Else clip the line until one code becomes 0
code1 | code2 = 0000 | 1010 = 1010 (NOT 0000) : No Trivial accept
code1 & code2 = 0000 & 1010 = 0000 : No Trivial reject
Clip the line
Line-Clipping Example
1001 1000 1010
0001 0000 0010
0101 0100 0110
Xmin Xmax
Ymin
Ymax
a
c
f
e
d
b
x0,y0
x1,y1
Intersection computation
Line equation
y = y0 + m(x - x0)
where
m = y1 - y0
x1 - x0
x=x min x=x max
y=y max
y=y min
Line-Clipping Example
1001 1000 1010
0001 0000 0010
0101 0100 0110
Xmin Xmax
Ymin
Ymax
a
c
f
e
d
b
x0,y0
x1,y1
Line intersection with the
left vertical boundary
Assume the intersection is c
x = xmin
y = y0 +m(xmin – x0)
Line ab is clipped w.r.t. x= xmin
now ab becomes cb
Intersection
computation
Line equation
y = y0 +m(x – x0)
where
m = y1 – y0
x1 – x0
x=x max
y=y max
y=y min
x=x min x=x max
y=y max
y=y min
Line-Clipping Example
1001 1000 1010
0001 0000 0010
0101 0100 0110
Xmin Xmax
Ymin
Ymax
c
f
e
d
b
x0,y0
x1,y1
Line intersection with the
bottom boundary
Assume the intersection is f
y = ymin
x = (1/m) (ymin – y0) + x0
Line cb is clipped w.r.t. y= ymax
Line cb becomes fb
Intersection
computation
Line equation
y = y0 +m(x – x0)
where
m = y1 – y0
x1 – x0
x=x min x=x max
y=y max
y=y min
Line-Clipping Example
1001 1000 1010
0001 0000 0010
0101 0100 0110
Xmin Xmax
Ymin
Ymax
f
e
x0,y0
Line intersection with the
top boundary
Assume the intersection is d
y = ymax
x = 1/m (ymax – y0) + x0
Line fb is clipped w.r.t. y=ymax
line fb becomes fd
Intersection
computation
Line equation
y = y0 +m(x – x0)
where
m = y1 – y0
x1 – x0
x=x min x=x max
y=y max
y=y min
f
e
d
x1,y1
b
Line-Clipping Example
1001 1000 1010
0001 0000 0010
0101 0100 0110
Xmin Xmax
Ymin
Ymax
f
e
x0,y0
Line intersection with the
right boundary
Assume the intersection is e
x = xmax
y = y0 + m(xmax – x0)
Line fd is clipped w.r.t. x=xmax
line fd becomes fe
Intersection
computation
Line equation
y = y0 +m(x – x0)
where
m = y1 – y0
x1 – x0
x=x min x=x max
y=y max
y=y min
d
Line-Clipping Example
1001 1000 1010
0001 0000 0010
0101 0100 0110
Xmin Xmax
Ymin
Ymax
f
e
x0,y0
x1,y1
FINALLY…………….
ab -----> cb
cb -----> fb
fb -----> fd
fd -----> fe
x=x min x=x max
y=y max
y=y min
#include<GL/glut.h>
#define outcode int
double xmin=50,ymin=50, xmax=100,ymax=100;
// Window boundaries
double xvmin=200,yvmin=200,xvmax=300,yvmax=300;
// Viewport boundaries
double m;
//bit codes for the right, left, top, & bottom
const int LEFT = 1;
const int RIGHT = 2;
const int BOTTOM = 4;
const int TOP = 8;
//used to compute bit codes of a point
outcode ComputeOutCode (double x, double y);
//Cohen-Sutherland clipping algorithm clips a line from
//P0 = (x0, y0) to P1 = (x1, y1) against a rectangle with
//diagonal from (xmin, ymin) to (xmax, ymax).
void CohenSutherlandLineClipAndDraw(double x0, double y0,double x1, double y1)
{
//Outcodes for P0, P1, and whatever point lies outside the clip rectangle
outcode outcode0, outcode1, outcodeOut;
bool accept = false, done = false;
outcode0 = ComputeOutCode (x0, y0);
outcode1 = ComputeOutCode (x1, y1);
m= (y1-y0)/(x1-x0);
do
{
if (!(outcode0 | outcode1)) //logical or is 0 Trivially accept & exit
{
accept = true;
done = true;
}
else if (outcode0 & outcode1)
//logical and is not 0.Trivially reject &exit
done = true;
else
{
/* failed both tests, so calculate the line segment to clip from an outside point to
an intersection with clip edge */
double x, y;
outcodeOut = outcode0? outcode0: outcode1;
if (outcodeOut& TOP) //point is above the clip rectangle
{
x = x0 + (1/m) * (ymax – y0);
y = ymax;
}
else if (outcodeOut& BOTTOM) //point is below the clip rectangle
{
x = x0 + (1/m) * (ymin – y0);
y = ymin;
}
else if(outcodeOut& RIGHT) //point is to right of clip rectangle
{
y = y0 +m*(xmax – x0);
x = xmax;
}
else // Left //point is to the left of clip rectangle
{
y = y0 +m*(xmin – x0);
x = xmin;
}
//Now we move outside point to intersection point to clip
//and get ready for next pass.
if (outcodeOut == outcode0)
{
x0 = x;
y0 = y;
outcode0 = ComputeOutCode (x0, y0);
}
else
{
x1 = x;
y1 = y;
outcode1= ComputeOutCode (x1, y1);
}
}
}while (!done);
if (accept)
{ double sx=(xvmax-xvmin)/(xmax-xmin);
double sy=(yvmax-yvmin)/(ymax-ymin);
double vx0=xvmin+(x0-xmin)*sx;
double vy0=yvmin+(y0-ymin)*sy;
double vx1=xvmin+(x1-xmin)*sx;
double vy1=yvmin+(y1-ymin)*sy;
glColor3f(1.0, 0.0, 0.0); // new view port in red color
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin, yvmin);
glVertex2f(xvmax, yvmin);
glVertex2f(xvmax, yvmax);
glVertex2f(xvmin, yvmax);
glEnd();
glColor3f(0.0,0.0,1.0); // clipped line in blue color
glBegin(GL_LINES);
glVertex2d (vx0, vy0);
glVertex2d (vx1, vy1);
glEnd();
}
}
outcode ComputeOutCode (double x, double y)
{
outcode code = 0;
if (y >ymax) //above the clip window
code |= TOP;
else if (y <ymin) //below the clip window
code |= BOTTOM;
if (x >xmax) //to the right of clip window
code |= RIGHT;
else if (x <xmin) //to the left of clip window
code |= LEFT;
return code;
}
Bit Number 1 0
Fourth (LSB) Left of left edge X<Xmin Right of left edge X>Xmin
Third Right of right edge X>Xmax Left of right edge X<Xmax
Second Below bottom edge Y<Ymin Above bottom edge Y>Ymin
First (MSB) Above top edge Y>Ymax Below top edge Y<Ymax
void display()
{
double x0=60,y0=20,x1=80,y1=120;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0); //draw the line with red color
glBegin(GL_LINES);
glVertex2d (x0, y0);
glVertex2d (x1, y1);
glEnd();
glColor3f(0.0, 0.0, 1.0); //draw a blue colored window
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
CohenSutherlandLineClipAndDraw(x0,y0,x1,y1);
glFlush();
}
void myinit()
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
}
void main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("Cohen Suderland Line Clipping Algorithm");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}
10CSL67 CG LAB PROGRAM 5
Cohen-Sutherland Line-Clipping
1001 1000 1010
0001 0000 0010
0101 0100 0110
Xmin
Xmax
Ymin
Ymax
Algorithm
Is code1|code2==0? Trivial accept
Is code1 & code2!=0? Trivial reject
Else clip the line until one code becomes 0
Intersection computation
Line equation
y = y0 +m(x – x0)
where
m = y1 – y0
x1 – x0
Line intersection with the left boundary
x = xmin
y = y0 +m(xmin – x0)
Line intersection with the right boundary
x = xmax
y = y0 + m(xmax – x0)
Line intersection with the bottom boundary
y = ymin
x = (1/m) (ymin – y0) + x0
Line intersection with the top boundary
y = ymax
x = 1/m (ymax – y0) + x0
1001 1000 1010
0001 0000 0010
0101 0100 0110
Xmin Xmax
Ymin
Ymax
Bit Number 1 0
Fourth (LSB) Left of left edge X<Xmin Right of left edge X>Xmin
Third Right of right edge X>Xmax Left of right edge X<Xmax
Second Below bottom edge Y<Ymin Above bottom edge Y>Ymin
First (MSB) Above top edge Y>Ymax Below top edge Y<Ymax
Checking the Edges

More Related Content

PPTX
line clipping
PPT
Clipping Algorithm In Computer Graphics
PPT
Polygon clipping
PPTX
5. Error Coding
PDF
PPTX
PPT
Booth Multiplier
PPTX
January 23, 2015
line clipping
Clipping Algorithm In Computer Graphics
Polygon clipping
5. Error Coding
Booth Multiplier
January 23, 2015

What's hot (16)

DOCX
Computer graphics question for exam solved
PPTX
Mux decod pld2_vs2
PDF
IEEE Floating Point
PPT
Lec13 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Sh...
PPTX
#KPC #CST #Clipping
PPTX
Chapter 4. logic function and boolean algebra
PDF
FYBSC IT Digital Electronics Unit I Chapter II Number System and Binary Arith...
PPT
Cit 1101 lec 03
PPT
Class10
PPTX
Decoder
PDF
Combinational logic 1
PDF
Combinational logic 2
PDF
Sequential Circuit
DOC
Digital logic circuits important question and answers for 5 units
PDF
Chapter 02 Logic Functions and Gates
DOC
Dpsd lecture-notes
Computer graphics question for exam solved
Mux decod pld2_vs2
IEEE Floating Point
Lec13 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Sh...
#KPC #CST #Clipping
Chapter 4. logic function and boolean algebra
FYBSC IT Digital Electronics Unit I Chapter II Number System and Binary Arith...
Cit 1101 lec 03
Class10
Decoder
Combinational logic 1
Combinational logic 2
Sequential Circuit
Digital logic circuits important question and answers for 5 units
Chapter 02 Logic Functions and Gates
Dpsd lecture-notes
Ad

Viewers also liked (17)

DOC
COMPUTER GRAPHICS LAB MANUAL
PPTX
Frequent english words
PDF
Algorithms and Their Explanations
PPTX
Compututer Graphics - Color Modeling And Rendering
PDF
Region filling and object removal by exemplar based image inpainting
PDF
Computer graphics lab manual
PPTX
Area filling algo
DOCX
Graphics practical lab manual
PPT
Fill area algorithms
PPTX
Computer graphics
PPT
Lecture filling algorithms
DOCX
Computer Graphics Lab File C Programs
DOCX
Computer Graphics Practical
PPTX
Output primitives computer graphics c version
PDF
Computer Graphics
PDF
region-filling
COMPUTER GRAPHICS LAB MANUAL
Frequent english words
Algorithms and Their Explanations
Compututer Graphics - Color Modeling And Rendering
Region filling and object removal by exemplar based image inpainting
Computer graphics lab manual
Area filling algo
Graphics practical lab manual
Fill area algorithms
Computer graphics
Lecture filling algorithms
Computer Graphics Lab File C Programs
Computer Graphics Practical
Output primitives computer graphics c version
Computer Graphics
region-filling
Ad

Similar to 10CSL67 CG LAB PROGRAM 5 (20)

PDF
19BCS2605_Krishna_Kumar_Computer_Graphics_exp_3.1.pdf
PDF
Comparison of Various Line Clipping Algorithm for Improvement
PPT
99995327.ppt
PPTX
Unit2- line clipping.pptx
PPTX
clippiNG COMPUTER GRAPHICS A NEW ERA.pptx
PPTX
Clipping
PPTX
Clipping computer graphics
PPTX
ibuib.pptx
PPTX
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
PPTX
kgv.pptx
PPTX
An Efficient Model for Line Clipping Operation
PPTX
Computer Graphic - Clipping
PPTX
PPT
Implementation
DOCX
Graphics point clipping c program
PPT
Lecture 2d point,curve,text,line clipping
PPTX
Clipping & Rasterization
PPTX
ohu.pptx
PPT
Cohen and Sutherland Algorithm for 7-8 marks
19BCS2605_Krishna_Kumar_Computer_Graphics_exp_3.1.pdf
Comparison of Various Line Clipping Algorithm for Improvement
99995327.ppt
Unit2- line clipping.pptx
clippiNG COMPUTER GRAPHICS A NEW ERA.pptx
Clipping
Clipping computer graphics
ibuib.pptx
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
kgv.pptx
An Efficient Model for Line Clipping Operation
Computer Graphic - Clipping
Implementation
Graphics point clipping c program
Lecture 2d point,curve,text,line clipping
Clipping & Rasterization
ohu.pptx
Cohen and Sutherland Algorithm for 7-8 marks

More from Vanishree Arun (9)

PDF
10CSL67 CG LAB PROGRAM 10
PDF
10CSL67 CG LAB PROGRAM 9
PDF
10CSL67 CG LAB PROGRAM 8
PDF
10CSL67 CG LAB PROGRAM 7
PDF
10CSL67 CG LAB PROGRAM 6
PDF
10CSL67 CG LAB PROGRAM 4
PDF
10CSL67 CG LAB PROGRAM 3
PDF
10CSL67 CG LAB PROGRAM 2
PDF
10CSL67 CG LAB PROGRAM 1
10CSL67 CG LAB PROGRAM 10
10CSL67 CG LAB PROGRAM 9
10CSL67 CG LAB PROGRAM 8
10CSL67 CG LAB PROGRAM 7
10CSL67 CG LAB PROGRAM 6
10CSL67 CG LAB PROGRAM 4
10CSL67 CG LAB PROGRAM 3
10CSL67 CG LAB PROGRAM 2
10CSL67 CG LAB PROGRAM 1

Recently uploaded (20)

PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PPTX
additive manufacturing of ss316l using mig welding
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PPTX
Artificial Intelligence
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Construction Project Organization Group 2.pptx
PPTX
Geodesy 1.pptx...............................................
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Sustainable Sites - Green Building Construction
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
Fundamentals of safety and accident prevention -final (1).pptx
III.4.1.2_The_Space_Environment.p pdffdf
additive manufacturing of ss316l using mig welding
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Artificial Intelligence
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Mechanical Engineering MATERIALS Selection
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Construction Project Organization Group 2.pptx
Geodesy 1.pptx...............................................
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Sustainable Sites - Green Building Construction
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION

10CSL67 CG LAB PROGRAM 5

  • 1. Program 5 Program to implement Cohen-Suderland Line Clipping Algorithm. Make provision to specify the input line, window for clipping and view port for displaying the clipped image.
  • 2. Cohen-Sutherland Line-Clipping: • It is a very good algorithm for clipping pictures that are larger than the screen. • This algorithm divides a 2D space into 9 parts, of which only the middle part (view port) is visible. • The 9 regions can be uniquely identified using a 4 bit code. This 4 bit code is called outcode.
  • 3. Cohen-Sutherland Line-Clipping Outcode 9 8 10 1 0 2 5 4 6 We use the order Left, Right, Bottom, Top for these 4 bits.
  • 4. Cohen-Sutherland Line-Clipping Outcodes 1001 1000 1010 0001 0000 0010 0101 0100 0110 We use the order Left, Right, Bottom, Top for these 4 bits.
  • 5. Cohen-Sutherland Line-Clipping Region Outcodes 1001 1000 1010 0001 0000 0010 0101 0100 0110 Xmin Xmax Ymin Ymax Xmin Xmax Ymin Ymax
  • 6. Cohen-Sutherland Line-Clipping Region Outcodes Bit Number 1 0 Fourth (LSB) Left of left edge X<Xmin Right of left edge X>Xmin Third Right of right edge X>Xmax Left of right edge X<Xmax Second Below bottom edge Y<Ymin Above bottom edge Y>Ymin First (MSB) Above top edge Y>Ymax Below top edge Y<Ymax 1001 1000 1010 0001 0000 0010 0101 0100 0110 Xmin Xmax Ymin Ymax
  • 7. Line-Clipping Algorithm Assume two endpoints are p0 and p1 1. If code(p0) OR code(p1) is 0000, • the line can be trivially accepted. • the line is drawn. 2. If code(p0) AND code(p1) is NOT 0000, • the line can be trivially rejected. • the line is not drawn at all. 3. Otherwise, compute the intersection points of the line segment and window boundary lines (make sure to check all the boundary lines)
  • 8. Line-Clipping 1001 1000 1010 0001 0000 0010 0101 0100 0110 Xmin Xmax Ymin Ymax L1 L2 L3 Line L1 code1 = 0000, code2 = 0000 Is code1|code2==0? Trivial accept Is code1 & code2!=0? Trivial reject Else clip the line until one code becomes 0 code1 | code2 = 0000 | 0000 = 0000 Trivial accept completely – no need to clip.
  • 9. Line-Clipping 1001 1000 1010 0001 0000 0010 0101 0100 0110 Xmin Xmax Ymin Ymax L1 L2 L5 Line L2 code1 = 0101, code2 = 0100 Is code1|code2==0? Trivial accept Is code1 & code2!=0? Trivial reject Else clip the line until one code becomes 0 code1 | code2 = 0101 | 0100 = 0101 (NOT 0000) : No Trivial accept Code1 & code2 = 0101 & 0100 = 0100 (NOT 0000) : Trivial reject
  • 10. Line-Clipping 1001 1000 1010 0001 0000 0010 0101 0100 0110 Xmin Xmax Ymin Ymax L1 L2 L5 Line L5 code1 = 0000, code2 = 1010 Is code1|code2==0? Trivial accept Is code1 & code2!=0? Trivial reject Else clip the line until one code becomes 0 code1 | code2 = 0000 | 1010 = 1010 (NOT 0000) : No Trivial accept code1 & code2 = 0000 & 1010 = 0000 : No Trivial reject Clip the line
  • 11. Line-Clipping Example 1001 1000 1010 0001 0000 0010 0101 0100 0110 Xmin Xmax Ymin Ymax a c f e d b x0,y0 x1,y1 Intersection computation Line equation y = y0 + m(x - x0) where m = y1 - y0 x1 - x0 x=x min x=x max y=y max y=y min
  • 12. Line-Clipping Example 1001 1000 1010 0001 0000 0010 0101 0100 0110 Xmin Xmax Ymin Ymax a c f e d b x0,y0 x1,y1 Line intersection with the left vertical boundary Assume the intersection is c x = xmin y = y0 +m(xmin – x0) Line ab is clipped w.r.t. x= xmin now ab becomes cb Intersection computation Line equation y = y0 +m(x – x0) where m = y1 – y0 x1 – x0 x=x max y=y max y=y min x=x min x=x max y=y max y=y min
  • 13. Line-Clipping Example 1001 1000 1010 0001 0000 0010 0101 0100 0110 Xmin Xmax Ymin Ymax c f e d b x0,y0 x1,y1 Line intersection with the bottom boundary Assume the intersection is f y = ymin x = (1/m) (ymin – y0) + x0 Line cb is clipped w.r.t. y= ymax Line cb becomes fb Intersection computation Line equation y = y0 +m(x – x0) where m = y1 – y0 x1 – x0 x=x min x=x max y=y max y=y min
  • 14. Line-Clipping Example 1001 1000 1010 0001 0000 0010 0101 0100 0110 Xmin Xmax Ymin Ymax f e x0,y0 Line intersection with the top boundary Assume the intersection is d y = ymax x = 1/m (ymax – y0) + x0 Line fb is clipped w.r.t. y=ymax line fb becomes fd Intersection computation Line equation y = y0 +m(x – x0) where m = y1 – y0 x1 – x0 x=x min x=x max y=y max y=y min f e d x1,y1 b
  • 15. Line-Clipping Example 1001 1000 1010 0001 0000 0010 0101 0100 0110 Xmin Xmax Ymin Ymax f e x0,y0 Line intersection with the right boundary Assume the intersection is e x = xmax y = y0 + m(xmax – x0) Line fd is clipped w.r.t. x=xmax line fd becomes fe Intersection computation Line equation y = y0 +m(x – x0) where m = y1 – y0 x1 – x0 x=x min x=x max y=y max y=y min d
  • 16. Line-Clipping Example 1001 1000 1010 0001 0000 0010 0101 0100 0110 Xmin Xmax Ymin Ymax f e x0,y0 x1,y1 FINALLY……………. ab -----> cb cb -----> fb fb -----> fd fd -----> fe x=x min x=x max y=y max y=y min
  • 17. #include<GL/glut.h> #define outcode int double xmin=50,ymin=50, xmax=100,ymax=100; // Window boundaries double xvmin=200,yvmin=200,xvmax=300,yvmax=300; // Viewport boundaries double m; //bit codes for the right, left, top, & bottom const int LEFT = 1; const int RIGHT = 2; const int BOTTOM = 4; const int TOP = 8; //used to compute bit codes of a point outcode ComputeOutCode (double x, double y); //Cohen-Sutherland clipping algorithm clips a line from //P0 = (x0, y0) to P1 = (x1, y1) against a rectangle with //diagonal from (xmin, ymin) to (xmax, ymax).
  • 18. void CohenSutherlandLineClipAndDraw(double x0, double y0,double x1, double y1) { //Outcodes for P0, P1, and whatever point lies outside the clip rectangle outcode outcode0, outcode1, outcodeOut; bool accept = false, done = false; outcode0 = ComputeOutCode (x0, y0); outcode1 = ComputeOutCode (x1, y1); m= (y1-y0)/(x1-x0); do { if (!(outcode0 | outcode1)) //logical or is 0 Trivially accept & exit { accept = true; done = true; } else if (outcode0 & outcode1) //logical and is not 0.Trivially reject &exit done = true;
  • 19. else { /* failed both tests, so calculate the line segment to clip from an outside point to an intersection with clip edge */ double x, y; outcodeOut = outcode0? outcode0: outcode1; if (outcodeOut& TOP) //point is above the clip rectangle { x = x0 + (1/m) * (ymax – y0); y = ymax; } else if (outcodeOut& BOTTOM) //point is below the clip rectangle { x = x0 + (1/m) * (ymin – y0); y = ymin; }
  • 20. else if(outcodeOut& RIGHT) //point is to right of clip rectangle { y = y0 +m*(xmax – x0); x = xmax; } else // Left //point is to the left of clip rectangle { y = y0 +m*(xmin – x0); x = xmin; }
  • 21. //Now we move outside point to intersection point to clip //and get ready for next pass. if (outcodeOut == outcode0) { x0 = x; y0 = y; outcode0 = ComputeOutCode (x0, y0); } else { x1 = x; y1 = y; outcode1= ComputeOutCode (x1, y1); } } }while (!done);
  • 22. if (accept) { double sx=(xvmax-xvmin)/(xmax-xmin); double sy=(yvmax-yvmin)/(ymax-ymin); double vx0=xvmin+(x0-xmin)*sx; double vy0=yvmin+(y0-ymin)*sy; double vx1=xvmin+(x1-xmin)*sx; double vy1=yvmin+(y1-ymin)*sy; glColor3f(1.0, 0.0, 0.0); // new view port in red color glBegin(GL_LINE_LOOP); glVertex2f(xvmin, yvmin); glVertex2f(xvmax, yvmin); glVertex2f(xvmax, yvmax); glVertex2f(xvmin, yvmax); glEnd(); glColor3f(0.0,0.0,1.0); // clipped line in blue color glBegin(GL_LINES); glVertex2d (vx0, vy0); glVertex2d (vx1, vy1); glEnd(); } }
  • 23. outcode ComputeOutCode (double x, double y) { outcode code = 0; if (y >ymax) //above the clip window code |= TOP; else if (y <ymin) //below the clip window code |= BOTTOM; if (x >xmax) //to the right of clip window code |= RIGHT; else if (x <xmin) //to the left of clip window code |= LEFT; return code; } Bit Number 1 0 Fourth (LSB) Left of left edge X<Xmin Right of left edge X>Xmin Third Right of right edge X>Xmax Left of right edge X<Xmax Second Below bottom edge Y<Ymin Above bottom edge Y>Ymin First (MSB) Above top edge Y>Ymax Below top edge Y<Ymax
  • 24. void display() { double x0=60,y0=20,x1=80,y1=120; glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,0.0,0.0); //draw the line with red color glBegin(GL_LINES); glVertex2d (x0, y0); glVertex2d (x1, y1); glEnd(); glColor3f(0.0, 0.0, 1.0); //draw a blue colored window glBegin(GL_LINE_LOOP); glVertex2f(xmin, ymin); glVertex2f(xmax, ymin); glVertex2f(xmax, ymax); glVertex2f(xmin, ymax); glEnd(); CohenSutherlandLineClipAndDraw(x0,y0,x1,y1); glFlush(); }
  • 25. void myinit() { glClearColor(1.0,1.0,1.0,1.0); glColor3f(1.0,0.0,0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,499.0,0.0,499.0); } void main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,500); glutCreateWindow("Cohen Suderland Line Clipping Algorithm"); glutDisplayFunc(display); myinit(); glutMainLoop(); }
  • 27. Cohen-Sutherland Line-Clipping 1001 1000 1010 0001 0000 0010 0101 0100 0110 Xmin Xmax Ymin Ymax Algorithm Is code1|code2==0? Trivial accept Is code1 & code2!=0? Trivial reject Else clip the line until one code becomes 0
  • 28. Intersection computation Line equation y = y0 +m(x – x0) where m = y1 – y0 x1 – x0
  • 29. Line intersection with the left boundary x = xmin y = y0 +m(xmin – x0) Line intersection with the right boundary x = xmax y = y0 + m(xmax – x0) Line intersection with the bottom boundary y = ymin x = (1/m) (ymin – y0) + x0 Line intersection with the top boundary y = ymax x = 1/m (ymax – y0) + x0 1001 1000 1010 0001 0000 0010 0101 0100 0110 Xmin Xmax Ymin Ymax
  • 30. Bit Number 1 0 Fourth (LSB) Left of left edge X<Xmin Right of left edge X>Xmin Third Right of right edge X>Xmax Left of right edge X<Xmax Second Below bottom edge Y<Ymin Above bottom edge Y>Ymin First (MSB) Above top edge Y>Ymax Below top edge Y<Ymax Checking the Edges