Hain triangulation algorithm in JS (work in progress)