Liang–Barsky algorithm

This is an old revision of this page, as edited by Raakesh gowda (talk | contribs) at 05:50, 5 June 2011. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer graphics, the Liang–Barsky algorithm is a line clipping algorithm. The Liang–Barsky algorithm uses the parametric equation of a line and inequalities describing the range of the clipping box to determine the intersections between the line and the clipping box. With these intersections it knows which portion of the line should be drawn. This algorithm is significantly more efficient than Cohen–Sutherland.

The idea of the Liang-Barsky clipping algorithm is to do as much testing as possible before computing line intersections. Consider first the usual parametric form of a straight line:

A point is in the clip window, if

and

,

which can be expressed as the 4 inequalities

,

where

(left)
(right)
(bottom)
(top)

To compute the final line segment:

  1. A line parallel to a clipping window edge has for that boundary.
  2. If for that , , the line is completely outside and can be eliminated.
  3. When the line proceeds outside to inside the clip window and when , the line proceeds inside to outside.
  4. For nonzero , gives the intersection point.
  5. For each line, calculate and . For , look at boundaries for which (outside -> in). Take to be the largest among . For , look at boundaries for which (inside -> out). Take to be the minimum of . If , the line is outside and therefore rejected.

Implementation of algorithm in c/c++:

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

if(cliptest(dx,xmax-x0,&te,&tl)) // inside test wrt right edge

if(cliptest(-dy,y0-ymin,&te,&tl)) // inside test wrt bottom edge

if(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 mappings

double sx=(xvmax-xvmin)/(xmax-xmin); // Scale parameters

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;

//draw a red colored viewport

glColor3f(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 line

glBegin(GL_LINES);

glVertex2d (vx0, vy0);

glVertex2d (vx1, vy1);

glEnd();

} }// end of line clipping



See also

Algorithms used for the same purpose: