Liang–Barsky algorithm

From Infogalactic: the planetary knowledge core
(Redirected from Liang–Barsky)
Jump to: navigation, search

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 clipping 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 + u (x_1 - x_0) = x_0 + t \Delta x\,\!
y = y_0 + u (y_1 - y_0) = y_0 + t \Delta y\,\!

A point is in the clip window, if

x_{\text{min}} \le x_0 + t \Delta x \le x_{\text{max}}\,\!

and

y_{\text{min}} \le y_0 + t \Delta y \le y_{\text{max}}\,\!,

which can be expressed as the 4 inequalities

t p_i \le q_i, \quad i = 1, 2, 3, 4\,\!,

where

p_1 = -\Delta x  ,               q_1 = x_0 - x_{\text{min}}\,\! (left)
p_2 = \Delta x  ,                q_2 = x_{\text{max}} - x_0\,\! (right)
p_3 = -\Delta y  ,               q_3 = y_0 - y_\text{min}\,\! (bottom)
p_4 = \Delta y   ,               q_4 = y_\text{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 = \frac{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 \left\{ 0,\frac{q_i}{p_i} \right\}. For t_2, look at boundaries for which p_i > 0 (i.e. inside to outside). Take u_2 to be the minimum of \left\{ 1, \frac{q_i}{p_i} \right\}. If t_1 > t_2, the line is outside and therefore rejected.

See also

Algorithms used for the same purpose:

References

  • Liang, Y.D., and Barsky, B., "A New Concept and Method for Line Clipping", ACM Transactions on Graphics, 3(1):1-22, January 1984.
  • Liang, Y.D., B.A., Barsky, and M. Slater, Some Improvements to a Parametric Line Clipping Algorithm, CSD-92-688, Computer Science Division, University of California, Berkeley, 1992.
  • James D. Foley. Computer graphics: principles and practice. Addison-Wesley Professional, 1996. p. 117.

External links