Menu
Home Explore People Places Arts History Plants & Animals Science Life & Culture Technology
On this page
Liang–Barsky algorithm
Algorithm

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. 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.

The algorithm uses the parametric form of a straight line:

x = x 0 + t ( x 1 − x 0 ) = x 0 + t Δ x , {\displaystyle x=x_{0}+t(x_{1}-x_{0})=x_{0}+t\Delta x,} y = y 0 + t ( y 1 − y 0 ) = y 0 + t Δ y . {\displaystyle y=y_{0}+t(y_{1}-y_{0})=y_{0}+t\Delta y.}

A point is in the clip window, if

x min ≤ x 0 + t Δ x ≤ x max {\displaystyle x_{\text{min}}\leq x_{0}+t\Delta x\leq x_{\text{max}}}

and

y min ≤ y 0 + t Δ y ≤ y max , {\displaystyle y_{\text{min}}\leq y_{0}+t\Delta y\leq y_{\text{max}},}

which can be expressed as the 4 inequalities

t p i ≤ q i , i = 1 , 2 , 3 , 4 , {\displaystyle tp_{i}\leq q_{i},\quad 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) {\displaystyle {\begin{aligned}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}.&&{\text{(top)}}\end{aligned}}}

To compute the final line segment:

  1. A line parallel to a clipping window edge has p i = 0 {\displaystyle p_{i}=0} for that boundary.
  2. If for that i {\displaystyle i} , q i < 0 {\displaystyle q_{i}<0} , then the line is completely outside and can be eliminated.
  3. When p i < 0 {\displaystyle p_{i}<0} , the line proceeds outside to inside the clip window, and when p i > 0 {\displaystyle p_{i}>0} , the line proceeds inside to outside.
  4. For nonzero p i {\displaystyle p_{i}} , u = q i / p i {\displaystyle u=q_{i}/p_{i}} gives t {\displaystyle t} for the intersection point of the line and the window edge (possibly projected).
  5. The two actual intersections of the line with the window edges, if they exist, are described by u 1 {\displaystyle u_{1}} and u 2 {\displaystyle u_{2}} , calculated as follows. For u 1 {\displaystyle u_{1}} , look at boundaries for which p i < 0 {\displaystyle p_{i}<0} (i.e. outside to inside). Take u 1 {\displaystyle u_{1}} to be the largest among { 0 , q i / p i } {\displaystyle \{0,q_{i}/p_{i}\}} . For u 2 {\displaystyle u_{2}} , look at boundaries for which p i > 0 {\displaystyle p_{i}>0} (i.e. inside to outside). Take u 2 {\displaystyle u_{2}} to be the minimum of { 1 , q i / p i } {\displaystyle \{1,q_{i}/p_{i}\}} .
  6. If u 1 > u 2 {\displaystyle u_{1}>u_{2}} , the line is entirely outside the clip window. If u 1 < 0 < 1 < u 2 {\displaystyle u_{1}<0<1<u_{2}} it is entirely inside it.
// Liang–Barsky line-clipping algorithm #include<iostream> #include<graphics.h> #include<math.h> using namespace std; // this function gives the maximum float maxi(float arr[], int n) { float m = 0; for (int i = 0; i < n; ++i) if (m < arr[i]) m = arr[i]; return m; } // this function gives the minimum float mini(float arr[], int n) { float m = 1; for (int i = 0; i < n; ++i) if (m > 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 < 0) || (p2 == 0 && q2 < 0) || (p3 == 0 && q3 < 0) || (p4 == 0 && q4 < 0)) { outtextxy(80, 80, "Line is parallel to clipping window!"); return; } if (p1 != 0) { float r1 = q1 / p1; float r2 = q2 / p2; if (p1 < 0) { entryParams[entryIndex++] = r1; exitParams[exitIndex++] = r2; } else { entryParams[entryIndex++] = r2; exitParams[exitIndex++] = r1; } } if (p3 != 0) { float r3 = q3 / p3; float r4 = q4 / p4; if (p3 < 0) { entryParams[entryIndex++] = r3; exitParams[exitIndex++] = r4; } else { entryParams[entryIndex++] = r4; exitParams[exitIndex++] = r3; } } float clippedX1, clippedY1, clippedX2, clippedY2; float u1, u2; u1 = maxi(entryParams, entryIndex); // maximum of entry points u2 = mini(exitParams, exitIndex); // minimum of exit points if (u1 > 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 << "\nLiang-Barsky Line Clipping"; cout << "\nThe system window layout is: (0,0) at bottom left and (631, 467) at top right"; cout << "\nEnter the coordinates of the window (xmin, ymin, xmax, ymax): "; float xmin, ymin, xmax, ymax; cin >> xmin >> ymin >> xmax >> ymax; cout << "\nEnter the endpoints of the line (x1, y1) and (x2, y2): "; float x1, y1, x2, y2; cin >> 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(); }
We don't have any images related to Liang–Barsky algorithm yet.
We don't have any YouTube videos related to Liang–Barsky algorithm yet.
We don't have any PDF documents related to Liang–Barsky algorithm yet.
We don't have any Books related to Liang–Barsky algorithm yet.
We don't have any archived web articles related to Liang–Barsky algorithm yet.

See also

Algorithms used for the same purpose: