Polygon Subdivision in JavaScript