Skip to content
This repository has been archived by the owner on Oct 2, 2019. It is now read-only.

TypeError: Cannot read property 'circle' of null #24

Open
micooz opened this issue Jul 5, 2017 · 6 comments
Open

TypeError: Cannot read property 'circle' of null #24

micooz opened this issue Jul 5, 2017 · 6 comments

Comments

@micooz
Copy link

micooz commented Jul 5, 2017

I encountered an issue that special input data can crash voronoi.polygons.

Reproduce Demo

var d3 = require("d3");

var width = 400;
var height = 400;

// change width and height to 100 works well
// var width = 100;
// var height = 100;

var x = d3.scaleLinear();
var y = d3.scaleLinear();

x.domain([0, 1]).range([0, width]).nice();
y.domain([0, 1]).range([height, 0]).nice();

var voronoi = d3.voronoi()
  .extent([[-1, -1], [width + 1, height + 1]])
  .x((pt) => x(pt.x))
  .y((pt) => y(pt.y));

// TypeError: Cannot read property 'circle' of null
voronoi.polygons([
  {x: 0.9999994485078688, y: 0},
  {x: 0.9999994512180275, y: 0}
]);

I thought it was my scale range problem, but the following code works:

voronoi.polygons([
  {x: 0.1, y: 0},
  {x: 0.2, y: 0}
]);

https://runkit.com/micooz/595cabe5640c440013382792

@mbostock
Copy link
Member

mbostock commented Jul 5, 2017

Here’s a reduction:

var voronoi = d3.voronoi();

voronoi([
  [399.999779, 0],
  [399.999780, 0]
]);

In short the problem is the rounding applied during construction isn’t enough to prevent certain numerical instabilities; these two points are approximately, but not exactly, coincident. This is probably not something I can fix since this code is ported from Raymond Hill’s implementation of Steven Fortune’s algorithm. However, it might be fixable if this library is rewritten to use Vladimir Agafonkin’s delaunator, so that it first computes the Delaunay triangulation and then derives the dual Voronoi diagram. I believe this is generally considered the more numerically stable approach, and it’ll probably be faster…

@Fil
Copy link
Member

Fil commented Jun 29, 2018

https://beta.observablehq.com/d/2e32a5a801b9ed02 shows this is probably fixed now in d3-delaunay

I'm saying “probably” because d3-delaunay can't yet handle cases with n=2 points, cf d3/d3-delaunay#19

@garrickf
Copy link

Does d3-delaunay allow for a mapping from the Voronoi polygons to data points (with the other attributes in our original data), like d3-voronoi does? I wanted to use this because of the ability to make it easier to hover over points in a scatter plot, but I'm also running into the issue of some of my data points being approximately coincident.

@mbostock
Copy link
Member

Does d3-delaunay allow for a mapping from the Voronoi polygons to data points

Yes. Cell i in the Voronoi diagram corresponds to input point i.

@garrickf
Copy link

Does d3-delaunay allow for a mapping from the Voronoi polygons to data points

Yes. Cell i in the Voronoi diagram corresponds to input point i.

Thanks for the quick reply! I think my issue might be different: the number of cells I get is less than the number of points I pass in, so perhaps some of my points are actually coincident? I found a way to create a mapping for at least some of my points that have cells, which will probably suffice for hovering over information. Thanks for your help!

@Fil
Copy link
Member

Fil commented May 25, 2019

It will indeed be fixed by d3/d3-delaunay#64

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

4 participants