Liang-Barsky Polygon Clipping Example
Steve on Nov 8th 2009
According to site stats, my Sky Diver article is one of the popular ones so far, and in particular, people seem to hit it after searching for Liang-Barsky clipping. Since I’ve not had any comments or contacts relating to the code I posted, I don’t know if it’s what people are expecting or not. However, I do know that it’s not immediately usable without some thought, and I’ve wanted to provide a complete example for a while.
So, here it is. I’ve taken the Liang-Barsky polygon clip code I posted, and worked it into an example Java Applet, which you can hopefully see below if you have Java enabled in your browser. The code has only been changed to remove it’s fixed point nature (no more ‘<< 8′ to confuse things), and I’ve added a framework around it that calls the clip method, so you can hopefully see what’s going on.
I’ve not written up a complete tutorial on how the clipping works, like I said before, for that you need to read Computer Graphics, Principles and Practice by Foley, Van Dam, et. al. or search the internet for tutorials like this good one at Skytopia.
Here’s the applet for you to play with. Click on the image below to load and start it, then use the arrow keys to adjust the clip boundaries.

Note that I’ve used a little bit of Javascript to start the applet with a click on the image so that the full Java environment isn’t loaded every time this post is shown. You need Java and Javascript enabled for it all to work.
Full source code can be downloaded here.
Hope it’s useful.
Filed in Code | No responses yet