Post on 15-Feb-2017
PROGRAM 2
Program to implement Liang-Barsky
Line Clipping Algorithm.
Liang-Barsky Line Clipping Algorithm
Parametric definition of linexmin ≤ x0 + t Δx ≤ xmax Δx=x1-x0
ymin ≤ y0 + t Δy ≤ ymax Δy=y1-y0
0 ≤ t ≤ 1
Initializations• Set viewport points as xmin, xmax, ymin and ymax
• Set the line with 2 points A(x0,y0) and B (x1,y1)• Set line intersection parameters tentry (t1) = 0.0 and
tleaving (t2) = 1.0
Calculations• Find dx = x1 - x0 and dy = y1 - y0
• Update t1 or t2 depending upon dx or dy
x0, y0
x1, y1
Liang-Barsky Line Clipping Algorithm
Edge &K value
p q t
Left k=1 p1 = -dx q1 = x0 - xmin t = q1/p1
Right k=2 p2 = dx q2 = xmax - x0 t = q2/p2
Bottom k=3 p3 = -dy q3 = y0 - ymin t = q3/p3
Top k=4 p4 = dy q4= ymax - y0 t = q4/p4
Step 1 : Set line intersection parameters tentry (t1) = 0.0 and tleaving (t2) = 1.0
Step 2 :Obtain pk and qk for k 1: 4
Liang-Barsky Line Clipping Algorithm
Step 3 :If pk = 0, the line is parallel to the corresponding clippingboundary.
a) If pk = 0 and qk<0, the line is completely outsidethe boundary.
b) If pk = 0 and qk>=0, the line is inside the parallelclipping boundary.
(a) (b)
Liang-Barsky Line Clipping Algorithm
Step 4 :Using pk and qk (k1 :4), find if the line can be rejectedor intersection parameters (t1 and t2) must be adjusted.
• if pk < 0, update t1 as max[0, qk/pk] where k 1:4• if pk > 0, update t2 as min[1, qk/pk] where k 1:4
After the update,• If t1 > t2, reject the line• If t1 > 0, calculate new values of x0, y0• If t2 < 1, calculate new values of x1, y1
Liang-Barsky Line Clipping - Example
dx = P1x - P0x // Horizontal diff between P0 and P1
dx = x1 - x0 = 280-30 = 250
dy = P1y - P0y // Vertical diff between P0 and P1
dy = y1 - y0 = 160-20 = 140
dx = 250 , dy = 140
Initially
tentry (t1) = 0.0
tleaving(t2) = 1.0P030, 20
280, 160
P1
150
60
23070
Liang-Barsky Line Clipping - Example
Left edge check:p = -dx = -250 q = x0 - xmin = 30-70 = -40 t = q/p = 0.16
• If p< 0 // update tentry (t1)
if t > t1 set t1 = t
-250 < 0 TRUEcheck if t > t1 --> 0.16 > 0.0 TRUEt1 = tt1 = 0.16
t1 = 0.16 t2 = 1.0
dx = 250dy = 140
150
60
23070
Clipped
Newt1 = 0.16 t2 = 1.0P0
30, 20
280, 160
P1
Right edge check:p = dx = 250 q = xmax – x0 = 230-30 = 200 t = q/p = 0.8
• If p< 0 FALSE • If p > 0 // update tleaving (t2)
if t < t2 set t2 = t250 > 0 TRUEcheck if t < t2 ---> 0.8 < 1.0 TRUE
t2 = t = 0.8
t1 = 0.16 t2 = 0.8
Liang-Barsky Line Clipping - Exampledx = 250dy = 140
150
60
23070 Clipped
Newt1 = 0.16 t2 = 0.8P0
30, 20
280, 160
P1
Bottom edge check:p = -dy = -140 q = y0 - ymin = 20-60 = -40 t = q/p = 0.2857
• If p< 0 // update tentry (t1)
if t > t1 set t1 = t
-140 < 0 TRUEcheck if t > t1 -----> 0.2857 > 0.16 TRUEt1 = t----> t1 = 0.2857
t1 = 0.2857 t2 = 0.8
Liang-Barsky Line Clipping - Exampledx = 250dy = 140
150
60
23070
Newt1 = 0.29t2 = 0.8P0
30, 20
280, 160
P1
Clipped
Top edge check:p = dy = 140 q = ymax – y0 = 150 - 20 = 130 t = q/p = 0.928
• If p< 0 FALSE • If p > 0 // update tleaving (t2)
if t < t2 set t2 = t140 > 0 TRUEcheck if t < t2 -----> 0.928 < 0.8 FALSE
t1 = 0.2857 t2 = 0.8
Liang-Barsky Line Clipping - Exampledx = 250dy = 140
150
60
23070
No more clippings
P030, 20
280, 160
P1
Liang-Barsky Line Clipping - Example
if( tentry > 0.0 ) calculate new x0, y00.2857 > 0.0 TRUE
x0new = x0 + tentry *dxx0new = 30 + 0.2857 * 250 = 101.425y0new = y0 + tentry *dyy0new = 20 + 0.2857 * 140 = 60.0
t1 = 0.2857 t2 = 0.8dx = 250 dy = 140
Liang-Barsky Line Clipping - Example
if( tleaving < 1.0 ) calculate new x1, y10.8 > 0.0 TRUE
x1new = x0 + tleaving *dxx1new = 30 + 0.8 * 250 = 230y1new = y0 + tleaving *dyy1new = 20 + 0.8 * 140 = 132
Draw clipped line with the points A(x0new , y0new) and B(x1new , y1new)
t1 = 0.2857 t2 = 0.8dx = 250 dy = 140
150
60
23070
New P0
P030, 20
280, 160
P1
New P1
P060, 20
80, 120
P1
100
50
10050
Find t1 and t2
#include<stdio.h>#include<GL/glut.h>
double xmin=50,ymin=50, xmax=100,ymax=100; // Window boundaries
double xvmin=200,yvmin=200,xvmax=300,yvmax=300; // Viewport boundaries
int cliptest(double p, double q, double *t1, double *t2){
double t=q/p;if(p < 0.0) // potentially entry point, update te{
if( t > *t1) *t1=t;if( t > *t2) return(false); // line portion is outside
}
else if(p > 0.0) // Potentially leaving point, update tl{
if( t < *t2) *t2=t;if( t < *t1) return(false); // line portion is outside
}else if(p == 0.0){
if( q < 0.0) return(false); // line parallel to edge but outside}return(true);
}
void LiangBarskyLineClipAndDraw(double x0,double y0,double x1,double y1){
double dx=x1-x0, dy=y1-y0, te=0.0, tl=1.0;if(cliptest(-dx,x0-xmin,&te,&tl)) // inside test wrt left edgeif(cliptest(dx,xmax-x0,&te,&tl)) // inside test wrt right edgeif(cliptest(-dy,y0-ymin,&te,&tl)) // inside test wrt bottom edgeif(cliptest(dy,ymax-y0,&te,&tl)) // inside test wrt top edge{
if( tl< 1.0 ){
x1 = x0 + tl*dx;y1 = y0 + tl*dy;
}if( te> 0.0 ) { x0 = x0 + te*dx;
y0 = y0 + te*dy;}
// Window to viewport mappingsdouble sx=(xvmax-xvmin)/(xmax-xmin); // Scale parametersdouble 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;
//draw a red colored viewportglColor3f(1.0, 0.0, 0.0);glBegin(GL_LINE_LOOP);glVertex2f(xvmin, yvmin);glVertex2f(xvmax, yvmin);glVertex2f(xvmax, yvmax);glVertex2f(xvmin, yvmax);glEnd();
glColor3f(0.0,0.0,1.0); // draw blue colored clipped lineglBegin(GL_LINES);glVertex2d (vx0, vy0);glVertex2d (vx1, vy1);glEnd();
}}
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();LiangBarskyLineClipAndDraw(x0,y0,x1,y1);glFlush();
}
void myinit(){
glClearColor(1.0,1.0,1.0,1.0);glColor3f(1.0,0.0,0.0);glPointSize(1.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);glutInitWindowPosition(0,0);glutCreateWindow("Liang Barsky Line Clipping Algorithm");glutDisplayFunc(display);myinit();glutMainLoop();
}