Fil / d3-geo-voronoi

Voronoi / Delaunay tessellations on the sphere
ISC License
229 stars 24 forks source link

Added hemisphere test #41

Closed martinfrances107 closed 3 years ago

martinfrances107 commented 3 years ago

The code below labelled

// two hemispheres

is specialisation logic that is invoked only when two sites are input.

This is not covered by any existing test.

I had to develop this test to expose a bug in my port of this module to rust. so from a certain limited perspective this is a upstream push..

to critise my own work floating point number are hard-coded into the test .. depending on what people think I could refactor and compare the individual floats to within 1e-6?

src/delaunay.rs

 if (triangles.length === 0) {
    if (points.length < 2) return { polygons, centers };
    if (points.length === 2) {
      // two hemispheres
      const a = cartesian(points[0]),
        b = cartesian(points[1]),
        m = normalize(cartesianAdd(a, b)),
        d = normalize(cross(a, b)),
        c = cross(m, d);
      const poly = [
        m,
        cross(m, c),
        cross(cross(m, c), c),
        cross(cross(cross(m, c), c), c)
      ]
        .map(spherical)
        .map(supplement);
      return (
        polygons.push(poly),
        polygons.push(poly.slice().reverse()),
        { polygons, centers }
      );
    }
  }
Fil commented 3 years ago

It's ok, just a unit test :) An alternative approach if the test fails somewhere because of precision could be to JSON.stringify the result and truncate the numbers with String.replace().