Liang–Barsky Algorithm
   HOME

TheInfoList



OR:

In
computer graphics Computer graphics deals with generating images with the aid of computers. Today, computer graphics is a core technology in digital photography, film, video games, cell phone and computer displays, and many specialized applications. A great de ...
, 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 This is a glossary of terms relating to computer graphics. For more general computer hardware terms, see glossary of computer hardware terms. 0–9 A B ...
. With these intersections it knows which portion of the line should be drawn. So 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 \Delta x, :y = y_0 + t (y_1 - y_0) = y_0 + t \Delta y. A point is in the clip window, if :x_\text \le x_0 + t \Delta x \le x_\text and :y_\text \le y_0 + t \Delta y \le y_\text, which can be expressed as the 4 inequalities :t p_i \le q_i, \quad i = 1, 2, 3, 4, where : \begin p_1 &= -\Delta x, & q_1 &= x_0 - x_\text, & &\text \\ p_2 &= \Delta x, & q_2 &= x_\text - x_0, & &\text \\ p_3 &= -\Delta y, & q_3 &= y_0 - y_\text, & &\text \\ p_4 &= \Delta y, & q_4 &= y_\text - y_0. & &\text \end To compute the final line segment: # A line parallel to a clipping window edge has p_i = 0 for that boundary. # If for that i, q_i < 0, then the line is completely outside and can be eliminated. # When p_i < 0, the line proceeds outside to inside the clip window, and when p_i > 0, the line proceeds inside to outside. # For nonzero p_i, u = q_i / p_i gives t for the intersection point of the line and the window edge (possibly projected). # The two actual intersections of the line with the window edges, if they exist, are described by u_1 and u_2, calculated as follows. For u_1, look at boundaries for which p_i < 0 (i.e. outside to inside). Take u_1 to be the largest among \. For u_2, look at boundaries for which p_i > 0 (i.e. inside to outside). Take u_2 to be the minimum of \. # If u_1 > u_2, the line is entirely outside the clip window. If u_1 < 0 < 1 < u_2 it is entirely inside it. // Liang—Barsky line-clipping algorithm #include #include #include using namespace std; // this function gives the maximum float maxi(float arr[],int n) // this function gives the minimum float mini(float arr[], int n) void liang_barsky_clipper(float xmin, float ymin, float xmax, float ymax, float x1, float y1, float x2, float y2) int main()


See also

Algorithms used for the same purpose: *
Cyrus–Beck algorithm In computer graphics, the Cyrus–Beck algorithm is a generalized algorithm for line clipping. It was designed to be more efficient than the Cohen–Sutherland algorithm, which uses repetitive clipping.Nicholl–Lee–Nicholl algorithm *
Fast clipping In computer graphics, line clipping is the process of removing ( clipping) lines or portions of lines outside an area of interest (a viewport or view volume). Typically, any part of a line which is outside of the viewing area is removed. There ...


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.

'. Addison-Wesley Professional, 1996. p. 117.


External links

* http://hinjang.com/articles/04.html#eight

{{DEFAULTSORT:Liang- Line clipping algorithms