Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
33
Task/Voronoi-diagram/Python/voronoi-diagram-1.py
Normal file
33
Task/Voronoi-diagram/Python/voronoi-diagram-1.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
from PIL import Image
|
||||
import random
|
||||
import math
|
||||
|
||||
def generate_voronoi_diagram(width, height, num_cells):
|
||||
image = Image.new("RGB", (width, height))
|
||||
putpixel = image.putpixel
|
||||
imgx, imgy = image.size
|
||||
nx = []
|
||||
ny = []
|
||||
nr = []
|
||||
ng = []
|
||||
nb = []
|
||||
for i in range(num_cells):
|
||||
nx.append(random.randrange(imgx))
|
||||
ny.append(random.randrange(imgy))
|
||||
nr.append(random.randrange(256))
|
||||
ng.append(random.randrange(256))
|
||||
nb.append(random.randrange(256))
|
||||
for y in range(imgy):
|
||||
for x in range(imgx):
|
||||
dmin = math.hypot(imgx-1, imgy-1)
|
||||
j = -1
|
||||
for i in range(num_cells):
|
||||
d = math.hypot(nx[i]-x, ny[i]-y)
|
||||
if d < dmin:
|
||||
dmin = d
|
||||
j = i
|
||||
putpixel((x, y), (nr[j], ng[j], nb[j]))
|
||||
image.save("VoronoiDiagram.png", "PNG")
|
||||
image.show()
|
||||
|
||||
generate_voronoi_diagram(500, 500, 25)
|
||||
23
Task/Voronoi-diagram/Python/voronoi-diagram-2.py
Normal file
23
Task/Voronoi-diagram/Python/voronoi-diagram-2.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import numpy as np
|
||||
from PIL import Image
|
||||
from scipy.spatial import KDTree
|
||||
|
||||
def generate_voronoi_diagram(X, Y, num_cells):
|
||||
# Random colors and points
|
||||
colors = np.random.randint((256, 256, 256), size=(num_cells, 3), dtype=np.uint8)
|
||||
points = np.random.randint((Y, X), size=(num_cells, 2))
|
||||
|
||||
# Construct a list of all possible (y,x) coordinates
|
||||
idx = np.indices((Y, X))
|
||||
coords = np.moveaxis(idx, 0, -1).reshape((-1, 2))
|
||||
|
||||
# Find the closest point to each coordinate
|
||||
_d, labels = KDTree(points).query(coords)
|
||||
labels = labels.reshape((Y, X))
|
||||
|
||||
# Export an RGB image
|
||||
rgb = colors[labels]
|
||||
img = Image.fromarray(rgb, mode='RGB')
|
||||
img.save('VoronoiDiagram.png', 'PNG')
|
||||
img.show()
|
||||
return rgb
|
||||
Loading…
Add table
Add a link
Reference in a new issue