• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
August 25, 2022 |3.0K Views
Liang Barsky Line Clipping Algorithm in Computer Graphics
Description
Discussion

In this video, we will be understanding the Liang Barsky Algorithm from Computer Graphics in detail. 

What is Liang Barsky Algo?
It is also a line clipping algorithm. We use the parametric equation of the line & four inequalities to find the range of the parameter for which the line is in the viewport are solved. 

It is more efficient. It is considered to be the faster parametric line-clipping algorithm.

There are two concepts used in Liang Barsky Algorithm:

  • Parametric equation of the line
  • The inequalities explain the range of the clipping window that helps in determining the intersections between the line and the clip window.

Algorithm:

For any given line, the parametric equation is given as,

  • X = x1 + t(x2-x1)
  • Y = y1 + t(y2-y1)

where, 0< t <1

Point-clipping conditions in the parametric form are shown below:

  • xwmin <= x1 + t(x2-x1) <= xwmax
  • ywmin <= y1 + t(y2-y1) <= ywmax 

All these inequalities can be expressed as,

tpk <= qk

Where, k=1,2,3,4  (indicates left, right, bottom, and top boundaries)

Where p and q are defined as,

  •  p1 = -(x2-x1), q1 = x1 - xwmin (Left Boundary) 
  •  p2 = (x2-x1), q2 = xwmax - x1 (Right Boundary)
  •  p3 = -(y2-y1), q3 = y1 - ywmin (Bottom Boundary)
  •  p4 = (y2-y1), q4 = ywmax - y1 (Top Boundary)

If the line is parallel to the view boundary, then the p-value is zero.

  • If pk<0, the line goes from outside to inside called entering
  • If pk>0, the line goes from inside to outside called exiting.
  • If pk=0 and qk<0, then the line is trivially invisible (outside view window).
  • If pk=0 and qk>0, then the line is inside the corresponding boundary window.

Parameters t1 and t2 can be calculated which define the part of the line that lies within the clip rectangle.

When,

  • pk < 0, maximum(0, qk/pk) is taken.
  • pk > 0, minimum(1, qk/pk) is taken.

If t1 > t2,  then the line is rejected because the line is completely outside the clip window. Otherwise, the endpoints of the clipped line are calculated from the two values of parameter t.

Liang-Barsky Algorithm
https://www.geeksforgeeks.org/liang-barsky-algorithm/

Read More