You are on page 1of 4

Liang-Barsky Line Clipping

The ideas for clipping line of Liang-Barsky and Cyrus-Beck are the same. The only difference is Liang-Barsky
algorithm has been optimized for an upright rectangular clip window. So we will study only the idea of Liang-
Barsky.

Liang and Barsky have created an algorithm that uses floating-point arithmetic but finds the appropriate end
points with at most four computations. This algorithm uses the parametric equations for a line and solves four
inequalities to find the range of the parameter for which the line is in the viewport.

Let be the line which we want to study. The parametric equation of the line segment
from gives x-values and y-values for every point in terms of a parameter tthat ranges from 0 to 1. The equations
are

and

We can see that when t = 0, the point computed is P(x1,y1); and when t = 1, the point computed is Q(x2,y2).

Algorithm
1. Set and
2. Calculate the values of tL, tR, tT, and tB (tvalues).
o if or ignore it and go to the next edge
o otherwise classify the tvalue as entering or exiting value (using inner product to classify)
o if t is entering value set ; if t is exiting value set
3. If then draw a line from (x1 + dx*tmin, y1 + dy*tmin) to (x1 + dx*tmax, y1 + dy*tmax)
4. If the line crosses over the window, you will see (x1 + dx*tmin, y1 + dy*tmin) and (x1 + dx*tmax, y1 +
dy*tmax) are intersection between line and edge.

t values
• p1 = -Dx, q1 = x1 – xwmin (left , k=1)

• p2 = Dx, q2 = xwmax - x1 (right, k=2)

• p3 = -Dy, q3 = y1 – ywmin (bottom, k=3)

• p4 = -Dy, q4 = ywmax - y1 (top, k=4)

Entering or Exiting Value

We can classify if entering or exiting value by using inner product.

Let be the line

and be the normal vector(outward).

If , the parameter t is entering.

If , the parameter t is exiting.

Example

Let P = (1,3), Q = (-4,2) while the edge equation is x+2y-4 = 0. Determine if the vector from P to Q is entering or
exiting the edge.

We can determine n from the equation ax + by + c = o, where n = (a,b). Therfore, n = (1,2).

Q-P = (-4-1,2-3) = (-5,-1).

n(Q-P) = (1,2) * (-5,-1) = -5-2 = -7 which is less than 0.

So, this line is entering the edge.

Intersection between line and edge


From the parametric equation and the tvalues, we can calculate the intersection between a line and an edge as
follows

Intersection on left edge


Intersection on right edge

Intersection on top edge

Intersection on bottom edge

Example 1 - Line Passing Through Window

The next step we consider if tvalue is entering or exiting by using inner product.

(Q-P) = (15+5,9-3) = (20,6)

At left edge (Q-P)nL = (20,6)(-10,0) = -200 < 0 entering so we set tmin = 1/4

At right edge (Q-P)nR = (20,6)(10,0) = 200 > 0 exiting so we set tmax = 3/4

Because then we draw a line from (-5+(20)*(1/4), 3+(6)*(1/4)) to (-5+(20)*(3/4), 3+(6)*(3/4))


Example 2 - Line Not Passing Through Window

The next step we consider if tvalue is entering or exiting by using inner product.

(Q-P) = (2+8,14-2) = (10,12)

At top edge (Q-P)nT = (10,12)(0,10) = 120 > 0 exiting so we set tmax = 8/12

At left edge (Q-P)nL = (10,12)(-10,0) = -100 < 0 entering so we set tmin = 8/10

Because tmin > tmax then we don't draw a line.

You might also like