0% found this document useful (0 votes)
37 views

ch6 2

The Liang-Barsky algorithm clips lines faster than previous methods by representing the line segment parametrically and deriving constraints on the parameter based on the clipping region boundaries. It calculates parameter values for potential entry and exit points and clips the line segment between these values if they are within the valid parameter range.

Uploaded by

Maisa Arham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

ch6 2

The Liang-Barsky algorithm clips lines faster than previous methods by representing the line segment parametrically and deriving constraints on the parameter based on the clipping region boundaries. It calculates parameter values for potential entry and exit points and clips the line segment between these values if they are within the valid parameter range.

Uploaded by

Maisa Arham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Liang-Barsky Algorithm

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,

where 0 <= u <= 1,


x = x2 - x1 and
y = y2 - y1
Liang-Barsky Algorithm
• we first write the point-clipping in a parameteric way:
xmin <= x1 + u x <= xmax
ymin <= y1 + u y <= ymax
Rearranging, we get
-u∆x ≤ (x1 - xmin)
u∆x ≤ (xmax - x1)
-u∆y ≤ (y1 - ymin)
u∆y ≤ (ymax - y1)
In general: u * pk ≤ qk
Liang-Barsky Algorithm
• four inequalities can be expressed as
 
u * pk <= qk,

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;

int p[4],q[4],i,accept=1; // To decide if line has to be shown are not


float u[4],umin=0,umax=1;

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

if(accept==1 && umax>umin){


int x3,y3,x4,y4;
x3 = x1 + (x2-x1)*umin;
y3 = y1 + (y2-y1)*umin;
x4 = x1 + (x2-x1)*umax;
y4 = y1 + (y2-y1)*umax;

line(x3,y3,x4,y4);
}

You might also like