Trisha Shetty (Editor)

Liang–Barsky algorithm

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

In computer graphics, the Liang–Barsky algorithm (named after You-Dong Liang and Brian A. Barsky) is a line clipping algorithm. The Liang–Barsky algorithm uses the parametric equation of a line and inequalities describing the range of the clipping window to determine the intersections between the line and the clip window. 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:

x = x 0 + t ( x 1 x 0 ) = x 0 + t Δ x y = y 0 + t ( y 1 y 0 ) = y 0 + t Δ y

A point is in the clip window, if

x min x 0 + t Δ x x max

and

y min y 0 + t Δ y y max ,

which can be expressed as the 4 inequalities

t p i q i , i = 1 , 2 , 3 , 4 ,

where

p 1 = Δ x , q 1 = x 0 x min (left) p 2 = Δ x , q 2 = x max x 0 (right) p 3 = Δ y , q 3 = y 0 y min (bottom) p 4 = Δ y , q 4 = y max y 0 (top)

To compute the final line segment:

  1. A line parallel to a clipping window edge has p i = 0 for that boundary.
  2. If for that i , q i < 0 , the line is completely outside and can be eliminated.
  3. When p i < 0 the line proceeds outside to inside the clip window and when p i > 0 , the line proceeds inside to outside.
  4. For nonzero p k , u = q i p i gives the intersection point.
  5. For each line, calculate u 1 and u 2 . For u 1 , look at boundaries for which p i < 0 (i.e. outside to inside). Take u 1 to be the largest among { 0 , q i p i } . For u 2 , look at boundaries for which p i > 0 (i.e. inside to outside). Take u 2 to be the minimum of { 1 , q i p i } . If u 1 > u 2 , the line is outside and therefore rejected.

References

Liang–Barsky algorithm Wikipedia


Similar Topics