Created
May 16, 2017 18:58
-
-
Save TApicella/d81067fc7d7df6d7f08f05d87eca8a39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
import random as r | |
import math | |
def distance(point1, point2, manhattan_dist=False): | |
if manhattan_dist: | |
return abs(point2[0]-point1[0]) + abs(point2[1]-point1[1]) | |
else: | |
return math.sqrt( (point2[0] - point1[0])**2 + (point2[1] - point1[1])**2 ) | |
pointspace = (400, 400) | |
numpoints = 20 | |
allpoints = [] | |
allcolors = [] | |
for p in range(numpoints): | |
print("Generating point number", p) | |
randx = r.randint(0, pointspace[0]) | |
randy = r.randint(0, pointspace[0]) | |
while (randx, randy) in allpoints: | |
randx = r.randint(0, pointspace[0]) | |
randy = r.randint(0, pointspace[0]) | |
allpoints.append((randx, randy)) | |
randr = r.randint(50, 200) | |
randg = r.randint(50, 200) | |
randb = r.randint(50, 200) | |
while (randr, randg, randb) in allcolors: | |
randr = r.randint(50, 200) | |
randg = r.randint(50, 200) | |
randb = r.randint(50, 200) | |
allcolors.append((randr, randg, randb)) | |
allpixels_e = {} | |
allpixels_m = {} | |
for i in range(pointspace[0]): | |
for j in range(pointspace[1]): | |
distances_e = [] | |
distances_m = [] | |
print("Testing point ", (i, j)) | |
if (i, j) in allpoints: | |
allpixels_e[(i, j)] = (250, 250, 250) | |
allpixels_m[(i, j)] = (250, 250, 250) | |
else: | |
for p in allpoints: | |
distances_e.append(distance((i, j), p)) | |
distances_m.append(distance((i, j), p, True)) | |
mindex_e = distances_e.index(min(distances_e)) | |
mindex_m = distances_m.index(min(distances_m)) | |
allpixels_e[(i, j)] = allcolors[mindex_e] | |
allpixels_m[(i, j)] = allcolors[mindex_m] | |
im = Image.new('RGB', (pointspace[0], pointspace[1])) | |
imagedata = []; | |
for x in range(im.width): | |
for y in range(im.height): | |
color = allpixels_e[(x, y)] | |
imagedata.append(color) | |
im.putdata(imagedata) | |
im.save("voronoi_%s.jpg" % ("euc")) | |
im = Image.new('RGB', (pointspace[0], pointspace[1])) | |
imagedata = []; | |
for x in range(im.width): | |
for y in range(im.height): | |
color = allpixels_m[(x, y)] | |
imagedata.append(color) | |
im.putdata(imagedata) | |
im.save("voronoi_%s.jpg" % ("man")) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment