Liang–Barsky algorithm
In computer graphics, the Liang–Barsky algorithm (named after You-Dong Liang and Brian A.[src] Barsky) is a line clipping algorithm first published in early 1984.[src] 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.[src] With these intersections, it knows which portion of the line should be drawn.[src] So this algorithm is significantly more efficient than Cohen–Sutherland.[src] The idea of the Liang–Barsky clipping algorithm is to do as much testing as possible before computing line intersections.[src] The algorithm uses the 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.[src] 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 : \begin{align} p_1 &= -\Delta x, & q_1 &= x_0 - x_\text{min}, & &\text{(left)} \\ p_2 &= \Delta x, & q_2 &= x_\text{max} - x_0, & &\text{(right)} \\ p_3 &= -\Delta y, & q_3 &= y_0 - y_\text{min}, & &\text{(bottom)} \\ p_4 &= \Delta y, & q_4 &= y_\text{max} - y_0.…[src]
In computer graphics, the Liang–Barsky algorithm (named after You-Dong Liang and Brian A.[src] Barsky) is a line clipping algorithm first published in early 1984.[src] 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.[src] With these intersections, it knows which portion of the line should be drawn.[src] So this algorithm is significantly more efficient than Cohen–Sutherland.[src] The idea of the Liang–Barsky clipping algorithm is to do as much testing as possible before computing line intersections.[src] The algorithm uses the 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.[src] 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 \begin{align} p_1 &= -\Delta x, & q_1 &= x_0 - x_\text{min}, & &\text{(left)} \\ p_2 &= \Delta x, & q_2 &= x_\text{max} - x_0, & &\text{(right)} \\ p_3 &= -\Delta y, & q_3 &= y_0 - y_\text{min}, & &\text{(bottom)} \\ p_4 &= \Delta y, & q_4 &= y_\text{max} - y_0.[src] & &\text{(top)} \end{align} To compute the final line segment: A line parallel to a clipping window edge has p_i = 0 for that boundary.[src] If for that i , q_i , then the line is completely outside and can be eliminated.[src] When p_i , the line proceeds outside to inside the clip window, and when p_i > 0 , the line proceeds inside to outside.[src] For nonzero p_i , u = q_i / p_i gives t for the intersection point of the line and the window edge (possibly projected).[src] 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.[src] For u_1 , look at boundaries for which p_i (i.e.[src] outside to inside).[src] Take u_1 to be the largest among \{0, q_i / p_i\} .[src] For u_2 , look at boundaries for which p_i > 0 (i.e.[src] inside to outside).[src] Take u_2 to be the minimum of \{1, q_i / p_i\} .[src] If u_1 > u_2 , the line is entirely outside the clip window.[src] If u_1 it is entirely inside it.[src] // Liang–Barsky line-clipping algorithm include include include using namespace std; // this function gives the maximum float maxi(float arr[], int n) { float m = 0; for (int i = 0; i arr[i]) m = arr[i]; return m; } void liang_barsky_clipper(float xmin, float ymin, float xmax, float ymax, float x1, float y1, float x2, float y2) { // defining variables float p1 = -(x2 - x1); float p2 = -p1; float p3 = -(y2 - y1); float p4 = -p3; float q1 = x1 - xmin; float q2 = xmax - x1; float q3 = y1 - ymin; float q4 = ymax - y1; float exitParams[5], entryParams[5]; int exitIndex = 1, entryIndex = 1; exitParams[0] = 1; entryParams[0] = 0; rectangle(xmin, ymin, xmax, ymax); // drawing the clipping window if ((p1 == 0 && q1 u2) { outtextxy(80, 80, "Line is outside the clipping window!"); return; } clippedX1 = x1 + (x2 - x1) * u1; clippedY1 = y1 + (y2 - y1) * u1; clippedX2 = x1 + (x2 - x1) * u2; clippedY2 = y1 + (y2 - y1) * u2; setcolor(CYAN); line(clippedX1, clippedY1, clippedX2, clippedY2); // draw clipped segment setlinestyle(1, 1, 0); line(x1, y1, clippedX1, clippedY1); // original start to clipped start line(x2, y2, clippedX2, clippedY2); // original end to clipped end } int main() { cout > xmin >> ymin >> xmax >> ymax; cout > x1 >> y1 >> x2 >> y2; int gd = DETECT, gm; initgraph(&gd, &gm, ""); // using winbgim liang_barsky_clipper(xmin, ymin, xmax, ymax, x1, y1, x2, y2); getch(); closegraph(); } ==See also== Algorithms used for the same purpose: Cyrus–Beck algorithm Nicholl–Lee–Nicholl algorithm Fast clipping ==References== Liang, Y.[src] D., and Barsky, B., "A New Concept and Method for Line Clipping", ACM Transactions on Graphics, 3(1):1–22, January 1984.[src] Liang, Y.[src] D., B.[src] A., Barsky, and M.[src] Slater, Some Improvements to a Parametric Line Clipping Algorithm, CSD-92-688, Computer Science Division, University of California, Berkeley, 1992.[src] James D.[src] Foley.[src] Computer Graphics: Principles and Practice.[src] Addison-Wesley Professional, 1996.[src] p. 117.[src] ==External links== https://hinjang.com/articles/04.html#eight Skytopia: The Liang-Barsky line clipping algorithm in a nutshell![src]
41 sentences · 41 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Liang–Barsky algorithm." encyclopegia.org, 2026, https://encyclopegia.org/wiki/liang-barsky-algorithm. Accessed 30 November 2026.
Chicago
"Liang–Barsky algorithm." encyclopegia.org. Accessed November 30, 2026. https://encyclopegia.org/wiki/liang-barsky-algorithm.
APA
encyclopegia.org. (2026). Liang–Barsky algorithm. Retrieved November 30, 2026, from https://encyclopegia.org/wiki/liang-barsky-algorithm
Accessed November 30, 2026 · CC BY-SA 4.0 · citation page · JSON
Claims
| Predicate | Value | Source |
|---|---|---|
| publication date | 1984 | Wikipedia |