Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iteratively creating voronoi with global variable #24

Open
madorjan2 opened this issue Aug 15, 2022 · 0 comments
Open

Iteratively creating voronoi with global variable #24

madorjan2 opened this issue Aug 15, 2022 · 0 comments

Comments

@madorjan2
Copy link

madorjan2 commented Aug 15, 2022

Hey,

Great library, saved me a lot of headache trying to implement Lloyd's algorithm.
While implementing it, I ran into a quite severe issue though. While I iterate through the relaxation, I create a new Voronoi object every step, and if I use a global variable as the bounding region parameter, I inherit points from the edge from my previous iteration. So if I use a global variable at initialization, after the 1st iteration I get an increased number of vertices in my Voronoi sites.

As an example, see the code below:

from foronoi import Voronoi, Polygon, Visualizer, VoronoiObserver
import numpy as np

DIM_X = 10
DIM_Y = 10
N_of_P = 5
BBOX = Polygon([(0,0), (DIM_X,0), (0,DIM_Y), (DIM_X, DIM_Y)])
ITERATION = 10

def initialize_voronoi(n):
    rng = np.random.default_rng()
    numbers = rng.choice(DIM_X*DIM_Y, size=n, replace=False)
    points = []
    for number in numbers:
        y = number//DIM_X
        x = number%DIM_X
        points.append((x,y))
    return points

def create_foronoi(points):
    v = Voronoi(BBOX)
    v.create_diagram(points=points)
    return v

for i in range(ITERATION):
    points = initialize_voronoi(N_of_P)
    vor = create_foronoi(points)
    acc = 0
    for site in vor.sites:
        acc += len(site.vertices())
    print(f"{i}.: {acc}")
    Visualizer(vor, canvas_offset=1)\
        .plot_sites(show_labels=True)\
        .plot_edges(show_labels=True)\
        .plot_vertices()\
        .plot_border_to_site()\
        .show()

If at line 21 you change Voronoi(BBOX) to Voronoi(Polygon([(0,0), (DIM_X,0), (0,DIM_Y), (DIM_X, DIM_Y)])) it works perfectly.

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

No branches or pull requests

1 participant