Yatoom / foronoi

An implementation of Fortune's algorithm for Voronoi diagrams in Python.
MIT License
51 stars 11 forks source link

Incorrect voronoi polygons #1

Closed RenaudLN closed 3 years ago

RenaudLN commented 4 years ago

Hi, I was trying out this Polygon-bounded Voronoi which is pretty cool but I run into issues from time to time as the algorithm does not seem to find Voronoi polygons for some of the points in the data (points that are within the bounding polygon).

Below is a reproducible example:

import numpy as np
from voronoi import Voronoi, Polygon

# These points were initially taken at random with np.random.rand
pts = [
    (0.17094288, 0.96789158),
    (0.55449063, 0.72159817),
    (0.94036409, 0.82856818),
]
# A square bounding box
polygon = Polygon([
    (0., 0.),
    (0., 1.),
    (1., 1.),
    (1., 0.),
])
# Initialise the voronoi object
v = Voronoi(polygon)
# Create the diagram
v.create_diagram(points=pts, vis_steps=False, verbose=False, vis_result=False, vis_tree=False)

# Print 
for i, pt in enumerate(v.points):
    print(i, pt.get_coordinates())
# Notice that in the output, the first two polygons are the same (with permutation):
# 0 [(0.685, 1.0), (0.962, 0.0), (0.0, 0.0), (0.0, 0.28), (0.462, 1.0)]
# 1 [(0.0, 0.28), (0.462, 1.0), (0.685, 1.0), (0.962, 0.0), (0.0, 0.0)]
# 2 [(0.962, 0.0), (0.685, 1.0), (1.0, 1.0), (1.0, 0.0)]

And if I try to plot them I get the following: newplot(2)

Yatoom commented 4 years ago

I haven't yet had the time to look into it, but I plan to do it soon! Thanks for the reproducible example, cases like these are definitely useful for debugging problems! Dankjewel!

Yatoom commented 3 years ago

Hi @RenaudLN, thank you for reporting this issue! It should now be fixed.

fixed-issue

The first set of coordinates is now describing the corner piece on the top left. The other polygons are still the same.

0 [(0.462, 1.0), (0.0, 0.28), (0.0, 1.0)] 
1 [(0.0, 0.28), (0.462, 1.0), (0.685, 1.0), (0.962, 0.0), (0.0, 0.0)]
2 [(0.962, 0.0), (0.685, 1.0), (1.0, 1.0), (1.0, 0.0)]