Okay this was homework. I searched for a really long time for a javascript implementation of cohen’s clipping algorithms and could find none. Professor said write it in c but its hard to program mouse clicks in c. With javascript, all it takes is a browser.

  1. Cohen-sutherland - line clipping algorithm in javascript

  2. Sutherland-Hodgman - polygon clipping algorithm in javascript.

Clipped polygon.

I believe the code is pretty readable - I had commented lavishly. Save them as html files, open in a browser, and keep clicking left mouse button.

And yes, the implementation is not perfect. I basically drew over the edges in white to “erase” it and that is why you see a very thin line outside the rectangle in the image.