-
Notifications
You must be signed in to change notification settings - Fork 60
Handle collinear (and cocircular) points? #12
Comments
The problem is that your input is (approximately) collinear: Unfortunately, this Voronoi implementation doesn’t gracefully handle such input. As Jason wrote in d3/d3#1895 (comment),
Your input data exhibits a similar error here: http://www.raymondhill.net/voronoi/rhill-voronoi.html If you click “Clear all sites”, paste this in, and then click “Parse as sites”:
You’ll see a TypeError: Cannot read property 'site' of undefined. I probably won’t be able to fix this in the near future… Sorry. You can probably work around by adding a tiny amount of random jitter to your points (though that is no guarantee, in the extremely unlikely case your jittered input happens to still be collinear or cocircular). |
Yeah, I thought it might be related to that existing issue as those sets of values are highly correlated. I'm building an interface in which I'm allowing users to plot different sets of values, including that rather uninteresting pair. It would be nice if it could fail gracefully so I could plot something else or render a message in its place. Right now the user has the potential to kill the browser window with an indefinite loop if they are an especially boring and/or unlucky individual :P I would like to take the opportunity Mike to thank you for this library, and the continuing work and support you have put in including the countless examples and high quality tutorials. Taking the time to learn your library helped me get my first job as a developer, and so I'm personally very grateful. |
Thank you. I expect that adding a tiny bit of random jitter would be safe if you don’t mind sacrificing accuracy. |
Yep, adding jitter with Math.random() to the scaled value in the accessors is a good enough trade off for my (and I imagine the majority of) cases. const voronoiGen = voronoi()
.x(d => x(d.x) + Math.random())
.y(d => y(d.y) + Math.random())
.extent([[0, 0], [w, h]]); |
I’d recommend subtracting by 0.5 so that the average jitter is still zero, like this example: var voronoi = d3.voronoi()
.x(function(d) { return d.x + Math.random() - 0.5; })
.y(function(d) { return d.y + Math.random() - 0.5; }); |
So I stumbled upon this bug with d3.voronoi with this particular dataset:
http://jsbin.com/sogamotefo/edit?html,js,console
The bug also exists in the most recent version of d3v3, and if you swap the accessors for x & y it actually causes the browser to hang indefinitely with no error messages as though it it stuck in a loop:
The text was updated successfully, but these errors were encountered: