# Liang-Barsky Algorithm

The **Liang-Barsky algorithm** is a line clipping algorithm. This algorithm is more efficient than Cohen–Sutherland line clipping algorithm and can be extended to 3-Dimensional clipping. This algorithm is considered to be the faster parametric line-clipping algorithm. The following concepts are used in this clipping:

- The parametric equation of the line.
- The inequalities describing the range of the clipping window which is used to determine the intersections between the line and the clip window.

The parametric equation of a line can be given by,

X = x_{1}+ t(x_{2}-x_{1}) Y = y_{1}+ t(y_{2}-y_{1})

Where, t is between 0 and 1.

Then, writing the point-clipping conditions in the parametric form:

xw_{min}<= x_{1}+ t(x_{2}-x_{1}) <= xw_{max}yw_{min}<= y_{1}+ t(y_{2}-y_{1}) <= yw_{max}

The above 4 inequalities can be expressed as,

tp_{k}<= q_{k}

Where k = 1, 2, 3, 4 (correspond to the left, right, bottom, and top boundaries, respectively).

The p and q are defined as,

p_{1}= -(x_{2}-x_{1}), q_{1}= x_{1}- xw_{min}(Left Boundary) p_{2}= (x_{2}-x_{1}), q_{2}= xw_{max}- x_{1}(Right Boundary) p_{3}= -(y_{2}-y_{1}), q_{3}= y_{1}- yw_{min}(Bottom Boundary) p_{4}= (y_{2}-y_{1}), q_{4}= yw_{max}- y_{1}(Top Boundary)

When the line is parallel to a view window boundary, the p value for that boundary is zero.

When p_{k} < 0, as t increase line goes from the outside to inside (entering).

When p_{k} > 0, line goes from inside to outside (exiting).

When p_{k} = 0 and q_{k} < 0 then line is trivially invisible because it is outside view window.

When p_{k} = 0 and q_{k} > 0 then the line is inside the corresponding window boundary.

Using the following conditions, the position of line can be determined:

Condition | Position of line |
---|---|

p_{k} = 0 | parallel to the clipping boundaries |

p_{k} = 0 and q_{k} < 0 | completely outside the boundary |

p_{k} = 0 and q_{k} >= 0 | inside the parallel clipping boundary |

p_{k} < 0 | line proceeds from outside to inside |

p_{k} > 0 | line proceeds from inside to outside |

Parameters t_{1} and t_{2} can be calculated that define the part of line that lies within the clip rectangle.

When,

- p
_{k}< 0, maximum(0, q_{k}/p_{k}) is taken. - p
_{k}> 0, minimum(1, q_{k}/p_{k}) is taken.

If t_{1} > t_{2}, the line is completely outside the clip window and it can be rejected. Otherwise, the endpoints of the clipped line are calculated from the two values of parameter t.

**Algorithm –**

- Set t
_{min}=0, t_{max}=1. - Calculate the values of t (t(left), t(right), t(top), t(bottom)),

(i) If t < t_{min}ignore that and move to the next edge.

(ii) else separate the t values as entering or exiting values using the inner product.

(iii) If t is entering value, set t_{min}= t; if t is existing value, set t_{max}= t. - If t
_{min}< t_{max}, draw a line from (x_{1}+ t_{min}(x_{2}-x_{1}), y_{1}+ t_{min}(y_{2}-y_{1})) to (x_{1}+ t_{max}(x_{2}-x_{1}), y_{1}+ t_{max}(y_{2}-y_{1})) - If the line crosses over the window, (x
_{1}+ t_{min}(x_{2}-x_{1}), y_{1}+ t_{min}(y_{2}-y_{1})) and (x_{1}+ t_{max}(x_{2}-x_{1}), y_{1}+ t_{max}(y_{2}-y_{1})) are the intersection point of line and edge.

This algorithm is presented in the following code. Line intersection parameters are initialised to the values t_{1} = 0 and t_{2} = 1.

`#include"graphics.h"` `#define ROUND(a) ((int)(a+0.5))` `int` `clipTest (` `float` `p,` `float` `q, ` `float` `* tl, ` `float` `* t2)` `{` `float` `r ;` `int` `retVal = TRUE;` ` ` `//line entry point` `if` `(p < 0.0) { ` ` ` ` ` `r = q /p ;` ` ` ` ` `// line portion is outside the clipping edge` ` ` `if` `( r > *t2 ) ` ` ` `retVal = FALSE;` ` ` ` ` `else` ` ` `if` `(r > *t1 )` ` ` `*tl = r; ` `}` ` ` `else` ` ` `//line leaving point` `if` `(p>0.0) { ` ` ` `r = q/p ;` ` ` ` ` `// line portion is outside ` ` ` `if` `( r<*t1 ) ` ` ` `retVal = FALSE; ` ` ` ` ` `else` `i f (r<*t2)` ` ` `*t2 = r;` `}` ` ` `// p = 0, so line is parallel to this clipping edge ` `else` ` ` `// Line is outside clipping edge ` `if` `(q<0.0) ` `retVal = FALSE;` ` ` `return` `( retVal ) ;` `}` ` ` `void` `clipLine (dcPt winMin, dcPt winMax, wcPt2 pl , wcPt2 p2) ` ` ` `{ ` `float` `t1 = 0.0, t2 = 1.0, dx = p2.x-p1.x, dy;` ` ` ` ` `// inside test wrt left edge` `if` `(clipTest (-dx, p1.x - winMin.x, &t1, &t2)) ` ` ` ` ` `// inside test wrt right edge ` `if` `(clipTest (dx, winMax.x - p1.x, &t1, &t2)) ` ` ` `{ ` ` ` `dy = p2.y - p1.y;` ` ` ` ` `// inside test wrt bottom edge ` ` ` `if` `(clipTest (-dy, p1.y - winMin.y, &t1, &t2))` ` ` ` ` `// inside test wrt top edge ` ` ` `if` `(clipTest (dy, winMax.y - p1.y, &t1, &t2)) {` ` ` ` ` `if` `(t2 < 1.0) {` ` ` `p2.x = p1.x + t2*dx;` ` ` `p2.y = p1.y + t2*dy;` ` ` `}` ` ` ` ` `if` `(t1 > 0.0) {` ` ` `p1.x += t1*dx;` ` ` `p1.y += t1*dy;` ` ` `}` ` ` ` ` `lineDDA ( ROUND(p1.x), ROUND(p1.y), ROUND(p2.x), ROUND(p2.y) );` ` ` ` ` `}` `}` ` ` `} ` |

**Reference:** Computer Graphics by Donald Hearn, M.Pauline Baker