d3 / d3-delaunay

Compute the Voronoi diagram of a set of two-dimensional points.
https://d3js.org/d3-delaunay
ISC License
611 stars 57 forks source link

Subtle out of bounds checking need documenting #128

Closed martinfrances107 closed 3 years ago

martinfrances107 commented 3 years ago

I appreciate the sparse documentation in this code. The coding style is good enough that the code mostly speaks for itself.

I want to talk about the implicit out of bounds checking which I think needs documenting as it will go unnoticed by the casual reader.

This can be seen by running the second half of ""zero-length edges are removed" in a debugger.

tape("zero-length edges are removed", test => {
  //  const voronoi1 = Delaunay.from([[50, 10], [10, 50], [10, 10], [200, 100]]).voronoi([40, 40, 440, 180]);
  //  test.equal(voronoi1.cellPolygon(0).length, 4);
   const voronoi2 = Delaunay.from([[10, 10], [20, 10]]).voronoi([0, 0, 30, 20]);
   test.deepEqual(voronoi2.cellPolygon(0), [[15, 20], [0, 20], [0, 0], [15, 0], [15, 20]]);
});

By out of bounds checking I mean :- P[j] frequently becomes undefined as the code runs off the end of the P array and the if statement functions in ways which are not obvious.

I just think anyone in future modifying this code would want this part of the design highlighted.