Well, there were problems in my trigonometry functions. But I spent a lot of time hunting bugs in them that weren't there. It turns out the real bug was in my tree algorithm: I wasn't backtracking correctly.
However, this now works, and you may play with it if you wish.

