This algorithm uses the parametric equations for a line and solves
four inequalities, describing the range of the clipping box to
determine the intersections between the line and the clipping box, to find the range of the parameter for which the line is in the view port. With these intersections it knows which portion of the line should be drawn. This algorithm is significantly more efficient than Cohen-Sutherland. Liang-Barsky line clipping x = x1 + (x2 - x1) * u = x1 + dx * u
y = y1 + (y2 - y1) * u = y1 + dy * u
where 0.0 < u < 1.0
Express line segment in parametric form: xw min x xw max
yw min y yw max Any point P (x, y) on line segment which is inside window satisfies: If x = xw min implies an intersection of the line with left boundary or in Parametric form xw min x1 + dx*u xw max
yw min y1 + dy*u yw max
Each of these Four inequalities can be expressed as up k q k , k=1,2,3,4 (left, right, bottom, top) Where the parameters p and q are defined as P1= -dx q1 = x1-xw min (left)
P2 = dx q2 = xw max -x1 (right) P3 = -dy q3 = y1-yw min (bottom) P4 = dy q4 = yw max -y1 (top) If p k =0, Lines will be parallel to the clipping boundary for the value of k corresponding to that boundary. ( k=1 for left, k=2 for right, k=3 for bottom, k=4 for top). If p k < 0, the line proceeds from outside to inside. If pk>0, the line proceeds from inside to outside. Rules: If q k < 0, then the line is completely outside the boundary and can be eliminated. If qk 0, then the line is inside the boundary.
If p k 0, we can calculate the value of u, that corresponds to the point where line intersects the boundary k as, u = q k / p k
A value of u is calculated for each of these boundaries If u1 > u2, the line is completely outside the clip window and can be rejected. Otherwise, the end points of a clipped line are calculated from the two values of parameter u. i.e for each line, we calculate values for parameters u1 & u2 that define that part of the line that lies within the clip window. The value of u1 is determined by looking at the rectangle edges for which the line proceeds from the outside to inside (i.e p < 0) The value of u2 is determined by looking at the rectangle edges for which the line proceeds from the inside to outside (i.e p > 0) For these edges we calculate r k = q k / p k