# Liang–Barsky 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. 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:

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

A point is in the clip window, if

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

and

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

which can be expressed as the 4 inequalities

${\displaystyle tp_{i}\leq q_{i},\quad i=1,2,3,4,}$

where

{\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 ${\displaystyle p_{i}=0}$ for that boundary.
2. If for that ${\displaystyle i}$, ${\displaystyle q_{i}<0}$, then the line is completely outside and can be eliminated.
3. When ${\displaystyle p_{i}<0}$, the line proceeds outside to inside the clip window, and when ${\displaystyle p_{i}>0}$, the line proceeds inside to outside.
4. For nonzero ${\displaystyle p_{k}}$, ${\displaystyle u=q_{i}/p_{i}}$ gives the intersection point.
5. For each line, calculate ${\displaystyle u_{1}}$ and ${\displaystyle u_{2}}$. For ${\displaystyle u_{1}}$, look at boundaries for which ${\displaystyle p_{i}<0}$ (i.e. outside to inside). Take ${\displaystyle u_{1}}$ to be the largest among ${\displaystyle \{0,q_{i}/p_{i}\}}$. For ${\displaystyle u_{2}}$, look at boundaries for which ${\displaystyle p_{i}>0}$ (i.e. inside to outside). Take ${\displaystyle u_{2}}$ to be the minimum of ${\displaystyle \{1,q_{i}/p_{i}\}}$. If ${\displaystyle u_{1}>u_{2}}$, the line is outside and therefore rejected.
// 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 posarr[5], negarr[5];
int posind = 1, negind = 1;
posarr[0] = 1;
negarr[0] = 0;

rectangle(xmin, 467 - ymin, xmax, 467 - ymax); // drawing the clipping window

if ((p1 == 0 && q1 < 0) || (p3 == 0 && q3 < 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) {
negarr[negind++] = r1; // for negative p1, add it to negative array
posarr[posind++] = r2; // and add p2 to positive array
} else {
negarr[negind++] = r2;
posarr[posind++] = r1;
}
}
if (p3 != 0) {
float r3 = q3 / p3;
float r4 = q4 / p4;
if (p3 < 0) {
negarr[negind++] = r3;
posarr[posind++] = r4;
} else {
negarr[negind++] = r4;
posarr[posind++] = r3;
}
}

float xn1, yn1, xn2, yn2;
float rn1, rn2;
rn1 = maxi(negarr, negind); // maximum of negative array
rn2 = mini(posarr, posind); // minimum of positive array

if (rn1 > rn2)  { // reject
outtextxy(80, 80, "Line is outside the clipping window!");
return;
}

xn1 = x1 + p2 * rn1;
yn1 = y1 + p4 * rn1; // computing new points

xn2 = x1 + p2 * rn2;
yn2 = y1 + p4 * rn2;

setcolor(CYAN);

line(xn1, 467 - yn1, xn2, 467 - yn2); // the drawing the new line

setlinestyle(1, 1, 0);

line(x1, 467 - y1, xn1, 467 - yn1);
line(x2, 467 - y2, xn2, 467 - yn2);
}

int main() {
cout << "\nLiang-barsky line clipping";
cout << "\nThe system window outlay is: (0,0) at bottom left and (631, 467) at top right";
cout << "\nEnter the co-ordinates of the window(wxmin, wxmax, wymin, wymax):";
float xmin, xmax, ymin, ymax;
cin >> xmin >> ymin >> xmax >> ymax;
cout << "\nEnter the end points of the line (x1, y1) and (x2, y2):";
float x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;

int gd = DETECT, gm;

// using the winbgim library for C++, initializing the graphics mode
initgraph(&gd, &gm, "");
liang_barsky_clipper(xmin, ymin, xmax, ymax, x1, y1, x2, y2);
getch();
closegraph();
}