Here's a series of examples showing how it works (not a fun animation like the OP, but a little easier to follow): http://jsfiddle.net/X8gXS/
Basically, you can get any triangle as long as at least one of the sides is horizontal or vertical.