Professional Documents
Culture Documents
Let P1 (-1, -2), P2 (2, 4) XL = 0, XR = 1, YB = 0, YT = 1 dx = 2 - (-1) = 3 dy = 4 - (-2) = 6 P1 = -dx = -3 q1 = x1 - XL = -1 - 0 = -1 q1 / P1 = 1/3 P2 = dx = 3 q2 = XR - x1 = 1 - (-1) = 2 q2 / P2 = 2/3 P3 = -dy = -6 q3 = y1 - YR = -2 q3 / P3 = 1/3 P4 = dy = 6 q4 = YT - y1 = 3 q4 / P4 = 1/2 for (Pi < 0) t1="MAX" ( 1 / 3, 1 / 3, 0 )="1" / 3 for (Pi > 0) t2 = MIN ( 2 / 3, 1 / 2, 1 ) = 1 / 2 Since t1 < t2 there is a visible section compute new endpoints t1 = 1 / 3 x1' = x1 + dx . t1 = -1 + (3 . 1 / 3) = 0 y1' = y1 + dy . t1 = -2 + (6 . 1 / 3) = 0 t2 = 1 / 2 x2' = x1 + dx . t2 = -1 + (3 . 1 / 2) = 1 / 2 y2' = y1 + dy . t2 = -1 + (6 . 1 / 2) = 1
Example of Liang-Barsky Line Clipping The Liang-Barsky line clipping algorithm in a nutshell!
At Skytopia, we pride ourselves in making some of the most difficult concepts as easy to understand as possible. Well, we've outdone ourselves again, and present you with the easiest tutorial to the Liang Barsky line clipping algorithm on the whole net! It was created after I went through torture trying to research it myself for my uni course. We hope you agree that the colour coding, examples, numerous diagrams (pixel-precise and to scale!), and the clearly nested step-by-step instructions will all help to make this otherwise torturous algorithm a little less painful.
Diagram 1
Now the main idea (and what takes up 99% of the algorithm) is to clip these two values to something a bit more like t0=0.3 and t1=0.8 (that's my rough guess, but you can see that these will approximately represent the points shown by the green squares in the diagram). We need to find out some values before we proceed. These two (Xdelta and Ydelta) are the most frequently used throughout the whole algorithm, so get them stuck into your brain:
Xdelta = P1x-P0x = 280-30 = 250 Ydelta = P1y-P0y = 160-20 = 140 // Horizontal diff between P0 and P1. // Vertical diff between P0 and P1.
Okay, now we're ready to start. What we'll do is go through each edge (border) in turn (Left, Right, Bottom, Top), and at each stage we will either tweak t0 or t1, or we may find out that the line is outside the border completely, and therefore doesn't need drawing - in which case we will cut the algorithm there and then! (NB., this won't happen with our example as shown by the diagram, since at least some of the line IS inside, so we can guarantee that we will complete all four stages of the Left-Right-Down-Up checks). Anyway, we start with a...
After calculating those values, we now see if any of the line is to the right of the LEFT EDGE, and if so then that's where to clip, and if not, then we can quit the whole algorithm, since ALL of the line would be to the left of the left edge (70). Observe:
Diagram 1b
It isn't in our case, but if P = 0 AND Q < 0, then we already quit. In this case, the line would parallel to the left edge ( P=0 : remember, P represents the Xdelta), and outside the edge (Q<0), so we don't need to worry about anything else, and no line is drawn. 'Thankfully', although Q is less than zero, P is not equal to zero, so we can continue.... If P is less than zero (which it is in our case, since -250 < 0 ), that means, the line from P0 to P1 is travelling in the opposite direction of our edge name (which in our case is 'left', so the line is going rightwards). Anyway, if this is the case (which it is for us), we do this check:
Now if R > t1 (is 0.16 > 1 ?), that must mean the line is too short to meet the left edge, so it must be outside. Thus we would scrap everything (don't draw the line!). Otherwise, if R > t0 (is 0.16 > 0 ?), then set t0 to R !!! (clip the beginning of the line to something higher than zero). So for us, t0 = 0.16 ! Now go to next edge. Otherwise, skip to next edge.
Diagram 2
If P is more than zero however (which it isn't in this example btw - we've already had the above executed, so the below boxout is only for future edge checks!), that means the line is travelling in the
Now this time, if R < t0 (is 0.16 < 0 ?), that must mean the (other end of the) line is too short to meet the left edge, so it must be outside. Thus scrap *everything*. Otherwise if R < t1 (is 0.16 < 1 ?), then set t1 = R ! Now go to next edge. Otherwise, skip to next edge.
Diagram 1b
We can 'optimize' the above five lines of Diagram 3 code by using this instead. It basically avoids any unnecessary calculations if the line was already completely inside the edges to start with:
// Stick with original values = P0x; = P0y; // Values need clipping = P0x + t0*Xdelta; = P0y + t0*Ydelta; // Stick with original values
Finisimo!
for(int edge=0; edge<4; edge++) { bottom, top edges. if (edge==0) { p = -xdelta; if (edge==1) { p = xdelta; if (edge==2) { p = -ydelta; if (edge==3) { p = ydelta; r = q/p; if(p==0 && q<0) return false; line outside) if(p<0) { if(r>t1) return false; else if(r>t0) t0=r; } else if(p>0) { if(r<t0) return false; else if(r<t1) t1=r; } } x0clip y0clip x1clip y1clip = = = = x0src y0src x0src y0src + + + + t0*xdelta; t0*ydelta; t1*xdelta; t1*ydelta;
// Don't draw line at all. // Line is clipped! // Don't draw line at all. // Line is clipped!