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();
}
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

What's hot

Computer graphics question for exam solved
Computer graphics question for exam solvedComputer graphics question for exam solved
Computer graphics question for exam solved
Kuntal Bhowmick
 
Mux decod pld2_vs2
Mux decod pld2_vs2Mux decod pld2_vs2
Mux decod pld2_vs2
WanNurdiana
 
IEEE Floating Point
IEEE Floating PointIEEE Floating Point
IEEE Floating Point
Jason Ricardo Thomas
 
Lec13 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Sh...
Lec13 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Sh...Lec13 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Sh...
Lec13 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Sh...
Hsien-Hsin Sean Lee, Ph.D.
 
#KPC #CST #Clipping
#KPC #CST #Clipping #KPC #CST #Clipping
#KPC #CST #Clipping
KEIKolkata
 
Chapter 4. logic function and boolean algebra
Chapter 4. logic function and boolean algebraChapter 4. logic function and boolean algebra
Chapter 4. logic function and boolean algebra
Ashish KC
 
FYBSC IT Digital Electronics Unit I Chapter II Number System and Binary Arith...
FYBSC IT Digital Electronics Unit I Chapter II Number System and Binary Arith...FYBSC IT Digital Electronics Unit I Chapter II Number System and Binary Arith...
FYBSC IT Digital Electronics Unit I Chapter II Number System and Binary Arith...
Arti Parab Academics
 
Cit 1101 lec 03
Cit 1101 lec 03Cit 1101 lec 03
Cit 1101 lec 03
sohag sikder
 
Class10
Class10Class10
Class10
Wasi Ahmad
 
Decoder
DecoderDecoder
Decoder
sahed dewan
 
Combinational logic 1
Combinational logic 1Combinational logic 1
Combinational logic 1
Heman Pathak
 
Combinational logic 2
Combinational logic 2Combinational logic 2
Combinational logic 2
Heman Pathak
 
Sequential Circuit
Sequential CircuitSequential Circuit
Sequential Circuit
Heman Pathak
 
Digital logic circuits important question and answers for 5 units
Digital logic circuits important question and answers for 5 unitsDigital logic circuits important question and answers for 5 units
Digital logic circuits important question and answers for 5 units
Lekashri Subramanian
 
Chapter 02 Logic Functions and Gates
Chapter 02 Logic Functions and GatesChapter 02 Logic Functions and Gates
Chapter 02 Logic Functions and Gates
SSE_AndyLi
 
Dpsd lecture-notes
Dpsd lecture-notesDpsd lecture-notes
Dpsd lecture-notes
AVC College of Engineering
 

What's hot (16)

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

Viewers also liked

COMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALCOMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUAL
Vivek Kumar Sinha
 
Frequent english words
Frequent english wordsFrequent english words
Frequent english words
Prince Soni
 
Algorithms and Their Explanations
Algorithms and Their ExplanationsAlgorithms and Their Explanations
Algorithms and Their Explanations
Marco Benini
 
Compututer Graphics - Color Modeling And Rendering
Compututer Graphics - Color Modeling And RenderingCompututer Graphics - Color Modeling And Rendering
Compututer Graphics - Color Modeling And Rendering
Prince Soni
 
Region filling and object removal by exemplar based image inpainting
Region filling and object removal by exemplar based image inpaintingRegion filling and object removal by exemplar based image inpainting
Region filling and object removal by exemplar based image inpainting
Woonghee Lee
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
Ankit Kumar
 
Area filling algo
Area filling algoArea filling algo
Area filling algo
Prince Soni
 
Graphics practical lab manual
Graphics practical lab manualGraphics practical lab manual
Graphics practical lab manual
Vivek Kumar Sinha
 
Polygon filling
Polygon fillingPolygon filling
Fill area algorithms
Fill area algorithmsFill area algorithms
Fill area algorithms
Kumar
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
Nanhen Verma
 
Lecture filling algorithms
Lecture  filling algorithmsLecture  filling algorithms
Lecture filling algorithms
avelraj
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
Kandarp Tiwari
 
Computer Graphics Practical
Computer Graphics PracticalComputer Graphics Practical
Computer Graphics Practical
Neha Sharma
 
Output primitives computer graphics c version
Output primitives   computer graphics c versionOutput primitives   computer graphics c version
Output primitives computer graphics c version
Marwa Al-Rikaby
 
Computer Graphics
Computer GraphicsComputer Graphics
Computer Graphics
Griffinder VinHai
 
region-filling
region-fillingregion-filling
region-filling
Kumar
 

Viewers also liked (17)

COMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALCOMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUAL
 
Frequent english words
Frequent english wordsFrequent english words
Frequent english words
 
Algorithms and Their Explanations
Algorithms and Their ExplanationsAlgorithms and Their Explanations
Algorithms and Their Explanations
 
Compututer Graphics - Color Modeling And Rendering
Compututer Graphics - Color Modeling And RenderingCompututer Graphics - Color Modeling And Rendering
Compututer Graphics - Color Modeling And Rendering
 
Region filling and object removal by exemplar based image inpainting
Region filling and object removal by exemplar based image inpaintingRegion filling and object removal by exemplar based image inpainting
Region filling and object removal by exemplar based image inpainting
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
Area filling algo
Area filling algoArea filling algo
Area filling algo
 
Graphics practical lab manual
Graphics practical lab manualGraphics practical lab manual
Graphics practical lab manual
 
Polygon filling
Polygon fillingPolygon filling
Polygon filling
 
Fill area algorithms
Fill area algorithmsFill area algorithms
Fill area algorithms
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
Lecture filling algorithms
Lecture  filling algorithmsLecture  filling algorithms
Lecture filling algorithms
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
 
Computer Graphics Practical
Computer Graphics PracticalComputer Graphics Practical
Computer Graphics Practical
 
Output primitives computer graphics c version
Output primitives   computer graphics c versionOutput primitives   computer graphics c version
Output primitives computer graphics c version
 
Computer Graphics
Computer GraphicsComputer Graphics
Computer Graphics
 
region-filling
region-fillingregion-filling
region-filling
 

Similar to 10CSL67 CG LAB PROGRAM 5

Unit2- line clipping.pptx
Unit2- line clipping.pptxUnit2- line clipping.pptx
Unit2- line clipping.pptx
RYZEN14
 
2D viewing & clipping
2D viewing & clipping2D viewing & clipping
2D viewing & clipping
MdAlAmin187
 
Cohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithmCohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithm
Shilpa Hait
 
Line clipping
Line clippingLine clipping
Line clipping
Ankit Garg
 
Computer Graphics - clipping
Computer Graphics - clippingComputer Graphics - clipping
Computer Graphic - Clipping
Computer Graphic - ClippingComputer Graphic - Clipping
Computer Graphic - Clipping
2013901097
 
19BCS2605_Krishna_Kumar_Computer_Graphics_exp_3.1.pdf
19BCS2605_Krishna_Kumar_Computer_Graphics_exp_3.1.pdf19BCS2605_Krishna_Kumar_Computer_Graphics_exp_3.1.pdf
19BCS2605_Krishna_Kumar_Computer_Graphics_exp_3.1.pdf
KrishnaKumar2309
 
Output Primitives in Computer Graphics and Multimedia
Output Primitives in Computer Graphics and MultimediaOutput Primitives in Computer Graphics and Multimedia
Output Primitives in Computer Graphics and Multimedia
saranyan75
 
Computer Graphics and Multimedia Output primitives
Computer Graphics and Multimedia Output primitivesComputer Graphics and Multimedia Output primitives
Computer Graphics and Multimedia Output primitives
saranyan75
 
Chapter-04.pdf
Chapter-04.pdfChapter-04.pdf
Chapter-04.pdf
ssuserf7cd2b
 
Introduction to Computer Graphics using OpenGLCan someone tell me .pdf
Introduction to Computer Graphics using OpenGLCan someone tell me .pdfIntroduction to Computer Graphics using OpenGLCan someone tell me .pdf
Introduction to Computer Graphics using OpenGLCan someone tell me .pdf
fathimafancyjeweller
 
raster algorithm.pdf
raster algorithm.pdfraster algorithm.pdf
raster algorithm.pdf
Mattupallipardhu
 
Lect 5 2d clipping
Lect 5 2d clippingLect 5 2d clipping
Lect 5 2d clipping
majicyoung
 
Cohen Sutherland Line Clipping Algorithm Example
Cohen Sutherland Line Clipping Algorithm ExampleCohen Sutherland Line Clipping Algorithm Example
Cohen Sutherland Line Clipping Algorithm Example
Kasun Ranga Wijeweera
 
Clipping computer graphics
Clipping  computer graphicsClipping  computer graphics
Clipping computer graphics
ShaishavShah8
 
7-Clipping-16 (1).pdf
7-Clipping-16 (1).pdf7-Clipping-16 (1).pdf
7-Clipping-16 (1).pdf
nikamomkarshahaji
 
Clipping
ClippingClipping
Clipping
AMIT VIRAMGAMI
 
Clipping
ClippingClipping
Clipping
nehrurevathy
 
Clipping 22
Clipping 22Clipping 22
Clipping 22
lokesh503
 
Clipping 22
Clipping 22Clipping 22
Clipping 22
Lokesh Reddy
 

Similar to 10CSL67 CG LAB PROGRAM 5 (20)

Unit2- line clipping.pptx
Unit2- line clipping.pptxUnit2- line clipping.pptx
Unit2- line clipping.pptx
 
2D viewing & clipping
2D viewing & clipping2D viewing & clipping
2D viewing & clipping
 
Cohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithmCohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithm
 
Line clipping
Line clippingLine clipping
Line clipping
 
Computer Graphics - clipping
Computer Graphics - clippingComputer Graphics - clipping
Computer Graphics - clipping
 
Computer Graphic - Clipping
Computer Graphic - ClippingComputer Graphic - Clipping
Computer Graphic - Clipping
 
19BCS2605_Krishna_Kumar_Computer_Graphics_exp_3.1.pdf
19BCS2605_Krishna_Kumar_Computer_Graphics_exp_3.1.pdf19BCS2605_Krishna_Kumar_Computer_Graphics_exp_3.1.pdf
19BCS2605_Krishna_Kumar_Computer_Graphics_exp_3.1.pdf
 
Output Primitives in Computer Graphics and Multimedia
Output Primitives in Computer Graphics and MultimediaOutput Primitives in Computer Graphics and Multimedia
Output Primitives in Computer Graphics and Multimedia
 
Computer Graphics and Multimedia Output primitives
Computer Graphics and Multimedia Output primitivesComputer Graphics and Multimedia Output primitives
Computer Graphics and Multimedia Output primitives
 
Chapter-04.pdf
Chapter-04.pdfChapter-04.pdf
Chapter-04.pdf
 
Introduction to Computer Graphics using OpenGLCan someone tell me .pdf
Introduction to Computer Graphics using OpenGLCan someone tell me .pdfIntroduction to Computer Graphics using OpenGLCan someone tell me .pdf
Introduction to Computer Graphics using OpenGLCan someone tell me .pdf
 
raster algorithm.pdf
raster algorithm.pdfraster algorithm.pdf
raster algorithm.pdf
 
Lect 5 2d clipping
Lect 5 2d clippingLect 5 2d clipping
Lect 5 2d clipping
 
Cohen Sutherland Line Clipping Algorithm Example
Cohen Sutherland Line Clipping Algorithm ExampleCohen Sutherland Line Clipping Algorithm Example
Cohen Sutherland Line Clipping Algorithm Example
 
Clipping computer graphics
Clipping  computer graphicsClipping  computer graphics
Clipping computer graphics
 
7-Clipping-16 (1).pdf
7-Clipping-16 (1).pdf7-Clipping-16 (1).pdf
7-Clipping-16 (1).pdf
 
Clipping
ClippingClipping
Clipping
 
Clipping
ClippingClipping
Clipping
 
Clipping 22
Clipping 22Clipping 22
Clipping 22
 
Clipping 22
Clipping 22Clipping 22
Clipping 22
 

More from Vanishree Arun

10CSL67 CG LAB PROGRAM 10
10CSL67 CG LAB PROGRAM 1010CSL67 CG LAB PROGRAM 10
10CSL67 CG LAB PROGRAM 10
Vanishree Arun
 
10CSL67 CG LAB PROGRAM 9
10CSL67 CG LAB PROGRAM 910CSL67 CG LAB PROGRAM 9
10CSL67 CG LAB PROGRAM 9
Vanishree Arun
 
10CSL67 CG LAB PROGRAM 8
10CSL67 CG LAB PROGRAM 810CSL67 CG LAB PROGRAM 8
10CSL67 CG LAB PROGRAM 8
Vanishree Arun
 
10CSL67 CG LAB PROGRAM 7
10CSL67 CG LAB PROGRAM 710CSL67 CG LAB PROGRAM 7
10CSL67 CG LAB PROGRAM 7
Vanishree Arun
 
10CSL67 CG LAB PROGRAM 6
10CSL67 CG LAB PROGRAM 610CSL67 CG LAB PROGRAM 6
10CSL67 CG LAB PROGRAM 6
Vanishree Arun
 
10CSL67 CG LAB PROGRAM 4
10CSL67 CG LAB PROGRAM 410CSL67 CG LAB PROGRAM 4
10CSL67 CG LAB PROGRAM 4
Vanishree Arun
 
10CSL67 CG LAB PROGRAM 3
10CSL67 CG LAB PROGRAM 310CSL67 CG LAB PROGRAM 3
10CSL67 CG LAB PROGRAM 3
Vanishree Arun
 
10CSL67 CG LAB PROGRAM 2
 10CSL67 CG LAB PROGRAM 2 10CSL67 CG LAB PROGRAM 2
10CSL67 CG LAB PROGRAM 2
Vanishree Arun
 
10CSL67 CG LAB PROGRAM 1
10CSL67 CG LAB PROGRAM 110CSL67 CG LAB PROGRAM 1
10CSL67 CG LAB PROGRAM 1
Vanishree Arun
 

More from Vanishree Arun (9)

10CSL67 CG LAB PROGRAM 10
10CSL67 CG LAB PROGRAM 1010CSL67 CG LAB PROGRAM 10
10CSL67 CG LAB PROGRAM 10
 
10CSL67 CG LAB PROGRAM 9
10CSL67 CG LAB PROGRAM 910CSL67 CG LAB PROGRAM 9
10CSL67 CG LAB PROGRAM 9
 
10CSL67 CG LAB PROGRAM 8
10CSL67 CG LAB PROGRAM 810CSL67 CG LAB PROGRAM 8
10CSL67 CG LAB PROGRAM 8
 
10CSL67 CG LAB PROGRAM 7
10CSL67 CG LAB PROGRAM 710CSL67 CG LAB PROGRAM 7
10CSL67 CG LAB PROGRAM 7
 
10CSL67 CG LAB PROGRAM 6
10CSL67 CG LAB PROGRAM 610CSL67 CG LAB PROGRAM 6
10CSL67 CG LAB PROGRAM 6
 
10CSL67 CG LAB PROGRAM 4
10CSL67 CG LAB PROGRAM 410CSL67 CG LAB PROGRAM 4
10CSL67 CG LAB PROGRAM 4
 
10CSL67 CG LAB PROGRAM 3
10CSL67 CG LAB PROGRAM 310CSL67 CG LAB PROGRAM 3
10CSL67 CG LAB PROGRAM 3
 
10CSL67 CG LAB PROGRAM 2
 10CSL67 CG LAB PROGRAM 2 10CSL67 CG LAB PROGRAM 2
10CSL67 CG LAB PROGRAM 2
 
10CSL67 CG LAB PROGRAM 1
10CSL67 CG LAB PROGRAM 110CSL67 CG LAB PROGRAM 1
10CSL67 CG LAB PROGRAM 1
 

Recently uploaded

22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
21UME003TUSHARDEB
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
Roger Rozario
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
LAXMAREDDY22
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Welding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdfWelding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdf
AjmalKhan50578
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
gowrishankartb2005
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
Introduction to AI Safety (public presentation).pptx
Introduction to AI Safety (public presentation).pptxIntroduction to AI Safety (public presentation).pptx
Introduction to AI Safety (public presentation).pptx
MiscAnnoy1
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
PKavitha10
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
AI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptxAI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptx
architagupta876
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 

Recently uploaded (20)

22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Welding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdfWelding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdf
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
Introduction to AI Safety (public presentation).pptx
Introduction to AI Safety (public presentation).pptxIntroduction to AI Safety (public presentation).pptx
Introduction to AI Safety (public presentation).pptx
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
AI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptxAI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptx
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 

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(); }
  • 26.
  • 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