Upload
vanishree-arun
View
40
Download
1
Embed Size (px)
Citation preview
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
XminXmax
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
XminXmax
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 theline segment and window boundary lines (makesure to check all the boundary lines)
Line-Clipping
1001 1000 1010
0001 0000 0010
0101 0100 0110
XminXmax
Ymin
Ymax
L1
L2
L3
Line L1
code1 = 0000, code2 = 0000Is code1|code2==0? Trivial accept
Is code1 & code2!=0? Trivial reject
Else clip the line until one code becomes 0
code1 | code2 = 0000 | 0000 = 0000Trivial accept completely – no need to clip.
Line-Clipping
1001 1000 1010
0001 0000 0010
0101 0100 0110
XminXmax
Ymin
Ymax
L1
L2
L5
Line L2code1 = 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 acceptCode1 & code2 = 0101 & 0100 = 0100 (NOT 0000) : Trivial reject
Line-Clipping
1001 1000 1010
0001 0000 0010
0101 0100 0110
XminXmax
Ymin
Ymax
L1
L2
L5
Line L5
code1 = 0000, code2 = 1010Is 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 acceptcode1 & code2 = 0000 & 1010 = 0000 : No Trivial reject Clip the line
Line-Clipping Example1001 1000 1010
0001 0000 0010
0101 0100 0110
Xmin Xmax
Ymin
Ymax
a
c
f
e
d
b
x0,y0
x1,y1Intersection computationLine equationy = y0 + m(x - x0)wherem = y1 - y0
x1 - x0
x=x min x=x max
y=y max
y=y min
Line-Clipping Example1001 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 cx = xmin
y = y0 +m(xmin – x0)
Line ab is clipped w.r.t. x= xmin
now ab becomes cb
Intersection computationLine equationy = y0 +m(x – x0)wherem = 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 Example1001 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 fy = ymin
x = (1/m) (ymin – y0) + x0Line cb is clipped w.r.t. y= ymax
Line cb becomes fb
Intersection computationLine equationy = y0 +m(x – x0)wherem = y1 – y0
x1 – x0
x=x min x=x max
y=y max
y=y min
Line-Clipping Example1001 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 dy = ymax
x = 1/m (ymax – y0) + x0Line fb is clipped w.r.t. y=ymax
line fb becomes fd
Intersection computationLine equationy = y0 +m(x – x0)wherem = y1 – y0
x1 – x0
x=x min x=x max
y=y max
y=y minf
e
dx1,y1
b
Line-Clipping Example1001 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 ex = xmax
y = y0 + m(xmax – x0)Line fd is clipped w.r.t. x=xmax
line fd becomes fe
Intersection computationLine equationy = y0 +m(x – x0)wherem = y1 – y0
x1 – x0
x=x min x=x max
y=y max
y=y min
d
Line-Clipping Example1001 1000 1010
0001 0000 0010
0101 0100 0110
Xmin Xmax
Ymin
Ymax
f
e
x0,y0
x1,y1
FINALLY…………….
ab -----> cbcb -----> fbfb -----> fdfd -----> 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 rectangleoutcode 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 &exitdone = 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 colorglBegin(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 colorglBegin(GL_LINES);
glVertex2d (vx0, vy0);glVertex2d (vx1, vy1);
glEnd();}
}
outcode ComputeOutCode (double x, double y){
outcode code = 0;if (y >ymax) //above the clip windowcode |= TOP;else if (y <ymin) //below the clip windowcode |= BOTTOM;if (x >xmax) //to the right of clip windowcode |= RIGHT;else if (x <xmin) //to the left of clip windowcode |= 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 colorglBegin(GL_LINES);
glVertex2d (x0, y0);glVertex2d (x1, y1);
glEnd();glColor3f(0.0, 0.0, 1.0); //draw a blue colored windowglBegin(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
XminXmax
Ymin
Ymax
AlgorithmIs code1|code2==0? Trivial acceptIs code1 & code2!=0? Trivial rejectElse clip the line until one code becomes 0
Line intersection with the left boundaryx = xmin
y = y0 +m(xmin – x0)
Line intersection with the right boundaryx = xmax
y = y0 + m(xmax – x0)
Line intersection with the bottom boundaryy = ymin
x = (1/m) (ymin – y0) + x0
Line intersection with the top boundaryy = ymax
x = 1/m (ymax – y0) + x0
1001 1000 1010
0001 0000 0010
0101 0100 0110
Xmin Xmax
Ymin
Ymax