rewrote algo via triangulation