Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Liang-Barsky Line Clipping Algorithm

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 10

Liang-Barsky Line Clipping Algorithm

Parametric Representation-Lines

p1  p(1)

p0  p(0) p( )

p
()(
1
)p 
0p1
Liang-Barsky Clipping
• Consider the parametric form of a line segment

p() = (1-)p1+ p2 1  0

p2

p1

• We can distinguish between the cases by looking at the ordering of the values of
 where the line determined by the line segment crosses the lines that determine
the window
Liang-Barsky Parametric Clipping
p
()
p 
0(p
1p
0)

Edge Ei

Outside Inside
p Ei
p1
N 
ip
()p 0
 
E

i

ip
N ( )p
Ei
0

p0
N 
ip
()p
Ei
0
Ni
Liang-Barsky 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 clipping window. With these intersections it knows which portion of
the line should be drawn. This algorithm is significantly more efficient than Cohen–
Sutherland. Consider first the usual parametric form of a straight line:
X=x0+u(x1-x0)=x0+uΔx,
Y=y0+u(y1-y0)=y0+uΔy, where 0<=u<=1.
A point is in the clip window, if Xwmin<=x0+uΔx<=xwmax and
Ywmin<=y0+uΔy<=Ywmax, which can be expressed as the 4 inequalities u.pk<qk,
k=1,2,3,4 where
P1=- Δx,
p2= Δx,
p3=- Δy,
p4= Δy,
q1=x0-xmin(left),
q2=xmax-x0(right),q3=y0-ymin(bottom),q4=ymax-y0(top).
• When pk < 0, as u increases
- line goes from outside to inside - entering
• When pk > 0,
- line goes from inside to outside - exiting
• When pk = 0,
- line is parallel to an edge
• If there is a segment of the line inside the clip region, a sequence of infinite line
intersections must go: entering, entering, exiting, exiting.

Algorithm:-
1. Set umin = 0 and umax = 1.
2. Calculate the u values:
3. If u < umin or u > umax ignore it.
Otherwise classify the u values as entering or exiting.
4. If umin < umax then draw a line from: ( x0 + Δx · umin, y0 + Δy · umin ) to
( x0 + Δx · umax, y0 + Δy · umax ).
We have umin = 1/4 and umax = 3/4
Pend - P0 = (15+5,9-3) = (20,6)
 If umin < umax , there is a line segment
- compute endpoints by substituting u values
 Draw a line from (-5+(20)·(1/4), 3+(6)·(1/4)) to (-5+(20)·(3/4), 3+(6)·(3/4))
• We have umin = 4/5 and umax = 2/3
Pend - P0 = (2+8, 14-2) = (10, 12)
• umin > umax , there is no line segment do draw.
 To compute the final line segment:
1. A line parallel to a clipping window edge has pk=0 for that boundary.

2. If for that k, qk<0, the line is completely outside and can be eliminated.

3. When pk<0 the line proceeds outside to inside the clip window and when pk>0, the
line proceeds inside to outside.

4. For nonzero pk, u=qk/pk gives the intersection point.

5. For each line, calculate u1 and u2. For u1, look at boundaries for which pk<0 (i.e.
outside to inside). Take to be the largest among . For u2, look at boundaries for
which pk>0 (i.e. inside to outside). Take u2 to be the minimum of {1,qk/pk}.
If u1>u2, the line is outside and therefore rejected.
Liang-Barsky vs Cohen-Sutherland:-

LB generally more efficient:

- LB: updates of u1 and u2 use one division, window intersections computed


only once with final u1 and u2.

- CS: may repeatedly calculate intersections along a line, even if line is totally
exterior to clip window and each intersection computation uses division and
multiplication.

You might also like