SlideShare une entreprise Scribd logo
1  sur  30
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Mux decod pld2_vs2
Mux decod pld2_vs2Mux decod pld2_vs2
Mux decod pld2_vs2
WanNurdiana
 
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
 

Tendances (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
 

En vedette

Lecture filling algorithms
Lecture  filling algorithmsLecture  filling algorithms
Lecture filling algorithms
avelraj
 
Computer Graphics Practical
Computer Graphics PracticalComputer Graphics Practical
Computer Graphics Practical
Neha Sharma
 

En vedette (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
 

Similaire à 10CSL67 CG LAB PROGRAM 5

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

Similaire à 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
 
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 22
Clipping 22Clipping 22
Clipping 22
 
Clipping 22
Clipping 22Clipping 22
Clipping 22
 
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygonsLiang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
 
Chapter4.pdf
Chapter4.pdfChapter4.pdf
Chapter4.pdf
 

Plus de Vanishree Arun

Plus de 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
 

Dernier

"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
Health
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 

Dernier (20)

data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdf
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 

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