andreesteve / voronoice

A nice and fast way to construct 2D Voronoi Diagrams
MIT License
34 stars 9 forks source link

Does voronoi.vertices() contain duplicates on boundary ? #13

Closed rsaccon closed 2 years ago

rsaccon commented 2 years ago

if I look at the voronoi.vertices() of a random voronoi diagram with bounding box from (-3000,-2000) to (3000, 2000) and three sites, I see this:

sites.len(): 3
--------------
0  -  x:553.4147615318169 y:-25.93358939022241
1  -  x:3000 y:2000
2  -  x:-3000 y:2000
3  -  x:-3000 y:-2000
4  -  x:3000 y:-2000
5  -  x:-3042.2091405193432 y:-10029.851902653116 outside !!!
6  -  x:11417.50649746393 y:142.9281150447297 outside !!!
7  -  x:-4578.460474700879 y:9096.246159148237 outside !!!
8  -  x:-586.3170464617128 y:2000
9  -  x:-156.10726253494698 y:-2000
10  -  x:3000 y:12.09393867700863
11  -  x:-586.3170464617131 y:2000
12  -  x:-156.10726253494715 y:-2000
13  -  x:3000 y:12.093938677008644

Now this seem to be a lot of vertices. Let me try to understand this: 0) is somewhere in the middle, 1) 2) 3) 4) are the corners of the bounding box and 5) 6) 7) are outside the bounding box. This looks all fine to me. 8) to 13) are on the boundary but they contain contain duplicates: 8) 11) are quasi duplicates, 9) 12) are quasi duplicates, 10) 13) are quasi duplicates.

Did I mess up something to end up with this kind of data ? Or is the user supposed to dedup this duplicate-vertices-on-boundary by himself, if needed ? Or are these duplicates there for a reason ?

andreesteve commented 2 years ago

Your analysis is correct. There are duplicated or near-duplicated points in the bounding box as outcome of the closing/clipping process.

This probably needs to be done after all clipping/closing is done. I believe this only happens on the edges of the bounding box, so perhaps one way to dedup is to sort the points on each one of the edges and merge if their distances is less than some epsilon multiplier.

Ideally this would be an improvement added to the library. so consumers do not need to do this every time. Certainly I would welcome a PR to address this, if you are interested!

rsaccon commented 2 years ago

Ok, thanks, I will try to approach the dedup as you suggested.

andreesteve commented 2 years ago

Lastest changes to master include an improvement to deduplicate all clipped voronoi edge vertices.

rsaccon commented 2 years ago

Great, thanks, will update and see wether I can get rid of my dedup hack.