ch6 2
ch6 2
u=1
rtop
u=0
rbottom
rleft rright
Liang-Barsky Line Clipping Algorithm
• Faster line clippers based on analysis of the
parametric equation of a line segment:
x = x1 + u x
y = y1 + u y,
for k = 1, 2, 3, 4
u = qk / pk
Liang-Barsky Algorithm
• Where parameters p and q are defined as:
p1 = -x,q1 = x1 – xmin LEFT
p2 = x, q2 = xmax - x1 RIGHT
p3 = -y,q3 = y1 – ymin BOTTOM
p4 = y, q4 = ymax - y1 TOP
Liang-Barsky Algorithm
• When pk<0, as t increases line goes from outside
to inside - enter
• When pk>0, line goes from inside to outside -
leave
• When pk=0, line is parallel to an edge
Liang-Barsky Algorithm
• If pk=0 (line parallel to clipping window edge)
– If qk<0, the line is completely outside the boundary (clip)
– If qk≥0, the line is completely inside the parallel clipping border
• When pk<0, infinite extension of line proceeds from outside
to inside of the infinite extension of this particular clipping
window edge
• When pk>0, line proceeds from inside to outside
• For non-zero pk, we can calculate the value of u that
corresponds to the point where the infinitely extended line
intersects the extension of the window edge k as u=q k/pk
7
Liang-Barsky Algorithm
• If pk=0 and qk<0 for any k, clip the line and stop. Otherwise,
go to next step
• For all k such that pk<0 (outside-inside), calculate rk=qk/pk. Let
u1 be the max of {0, rk}
• For all k such that pk>0 (inside-outside), calculate rk=qk/pk. Let
u2 be the min of {rk, 1}
• If u1>u2, clip the line since it is completely outside.
Otherwise, use u1 and u2 to calculate the endpoints of the
clipped line
• Example: (u1<u2)
• u1=max{0, rleft, rbottom}
• u2=min{rtop, rright,1}
Liang-Barsky Algorithm
Liang-Barsky Algorithm
int xl,yl,xh,yh;
int x1,y1,x2,y2;
p[0] = -(x2-x1);
p[1] = (x2-x1);
p[2] = (y2-y1); //These two equations have to be inverted as in computer
p[3] = -(y2-y1); //we have y increasing as we go below
q[0] = x1-xl;
q[1] = xh-x1;
q[2] = yl-y1; //These two equations have to be inverted for same
reason
q[3] = y1-yh;
Liang-Barsky Algorithm
for(i=0;i<4;i++)
{
if(p[i]!=0)
{
u[i] = (float)q[i]/p[i];
if(p[i]<0 && u[i]>umin) //Line is entering and therefore check for umin
umin = u[i];
else if(p[i]>0 && u[i]<umax) //Line is exiting and therefore check for umax
umax = u[i];
}
else if(p[i]==0 && q[i]<0) //Line is invisible as pk=0 & qk<0
accept=0;
}
Liang-Barsky Algorithm
line(x3,y3,x4,y4);
}