Voroni diagrams

Quick demo of the Voroni class in geometry.


In [1]:
import os, sys
sys.path.insert(0, os.path.abspath(".."))

In [2]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import open_cp.geometry as geometry
import matplotlib.patches

In [3]:
points = np.asarray([[1,1], [3,2], [1,3], [2,4]])
voroni = geometry.Voroni(points)

In [4]:
voroni.points


Out[4]:
array([[ 1.,  1.],
       [ 3.,  2.],
       [ 1.,  3.],
       [ 2.,  4.]])

In [5]:
voroni.vertices


Out[5]:
array([[ 1.75      ,  2.        ],
       [ 2.16666667,  2.83333333]])

In [6]:
fig, ax = plt.subplots(figsize=(8,8))
ax.scatter(voroni.points[:,0], voroni.points[:,1], color="blue")
for index, (x, y) in enumerate(voroni.points):
    ax.text(x+0.02,y+0.02,index)
ax.scatter(voroni.vertices[:,0], voroni.vertices[:,1], marker="+", color="black")
for index, (x, y) in enumerate(voroni.vertices):
    ax.text(x+0.02,y+0.02,index)

for poly in voroni.polygons(inf_dist=5):
    p = matplotlib.patches.Polygon(poly, fc="red", ec="black", alpha=0.5)
    ax.add_patch(p)
    
ax.set_aspect(1)
ax.set(xlim=[-5,8], ylim=[-3,7])


Out[6]:
[(-3, 7), (-5, 8)]

In [7]:
voroni.polygon_for(0, inf_dist=5)


Out[7]:
[(3.9860679774997898, -2.4721359549995796),
 (-3.25, 2.0),
 array([ 1.75,  2.  ])]

In [8]:
voroni.regions


Out[8]:
[[-1, 1, 0], [1, -1, 0], [-1, 0], [-1, 1], []]

In [9]:
voroni.ridge_vertices


Out[9]:
[[0, 1], [-1, 0], [-1, 1], [-1, 0], [-1, 1]]

In [10]:
voroni.ridge_points


Out[10]:
array([[2, 1],
       [2, 0],
       [2, 3],
       [1, 0],
       [1, 3]], dtype=int32)

In [11]:
voroni.point_region


Out[11]:
array([2, 1, 0, 3])

In [12]:
points = np.asarray([[1,1], [3,2], [1,3], [2,4], [4,4], [5,0]])
voroni = geometry.Voroni(points)

fig, ax = plt.subplots(figsize=(8,8))
ax.scatter(voroni.points[:,0], voroni.points[:,1], color="blue")
for index, (x, y) in enumerate(voroni.points):
    ax.text(x+0.02,y+0.02,index)
ax.scatter(voroni.vertices[:,0], voroni.vertices[:,1], marker="+", color="black")
for index, (x, y) in enumerate(voroni.vertices):
    ax.text(x+0.02,y+0.02,index)

for poly in voroni.polygons(inf_dist=5):
    p = matplotlib.patches.Polygon(poly, fc="red", ec="black", alpha=0.5)
    ax.add_patch(p)
    
ax.set_aspect(1)
ax.set(xlim=[-4,11], ylim=[-6,9])


Out[12]:
[(-6, 9), (-4, 11)]

In [13]:
points = np.asarray([[0,0], [1,0], [0,1], [1,1]])
voroni = geometry.Voroni(points)

fig, ax = plt.subplots(figsize=(8,8))
ax.scatter(voroni.points[:,0], voroni.points[:,1], color="blue")
for index, (x, y) in enumerate(voroni.points):
    ax.text(x+0.02,y+0.02,index)
ax.scatter(voroni.vertices[:,0], voroni.vertices[:,1], marker="+", color="black")
for index, (x, y) in enumerate(voroni.vertices):
    ax.text(x+0.02,y+0.02,index)

for poly in voroni.polygons(inf_dist=5):
    p = matplotlib.patches.Polygon(poly, fc="red", ec="black", alpha=0.5)
    ax.add_patch(p)
    
ax.set_aspect(1)
ax.set(xlim=[-6,7], ylim=[-6,7])


Out[13]:
[(-6, 7), (-6, 7)]

Estimating the required distance to infinity

Currently, if we make the inf_dist too small, then the "boundary polygons" may not be large enough to contain the points which formed them (for example).


In [14]:
points = np.asarray([[1,1], [3,2], [1,3], [2,4], [4,4], [5,0]])
voroni = geometry.Voroni(points)

fig, ax = plt.subplots(figsize=(8,8))
ax.scatter(voroni.points[:,0], voroni.points[:,1], color="blue")
for index, (x, y) in enumerate(voroni.points):
    ax.text(x+0.02,y+0.02,index)
ax.scatter(voroni.vertices[:,0], voroni.vertices[:,1], marker="+", color="black")
for index, (x, y) in enumerate(voroni.vertices):
    ax.text(x+0.02,y+0.02,index)

for i in range(6):
    poly = voroni.polygon_for_by_distance(i, 1)
    p = matplotlib.patches.Polygon(poly, fc="red", ec="black", alpha=0.5)
    ax.add_patch(p)
    
ax.set_aspect(1)
ax.set(xlim=[-4,11], ylim=[-6,9])
None



In [ ]: