Created
May 24, 2016 13:50
-
-
Save fogleman/6631b9d6b5e3d2b3af02c625fa8476ca to your computer and use it in GitHub Desktop.
Voronoi / Poisson
This file contains 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 math import pi, sin, cos, hypot, floor | |
from pyhull.voronoi import VoronoiTess | |
from shapely.geometry import Polygon | |
import cairocffi as cairo | |
import colorsys | |
import random | |
R = 0.05 | |
FILL = 0x422B36 | |
STROKE = 0xECDCBF | |
def color(value): | |
r = ((value >> (8 * 2)) & 255) / 255.0 | |
g = ((value >> (8 * 1)) & 255) / 255.0 | |
b = ((value >> (8 * 0)) & 255) / 255.0 | |
return (r, g, b) | |
class Grid(object): | |
def __init__(self, r): | |
self.r = r | |
self.size = r / 2 ** 0.5 | |
self.cells = {} | |
def points(self): | |
return self.cells.values() | |
def normalize(self, x, y): | |
i = int(floor(x / self.size)) | |
j = int(floor(y / self.size)) | |
return (i, j) | |
def nearby(self, x, y): | |
result = [] | |
i, j = self.normalize(x, y) | |
for p in xrange(i - 2, i + 3): | |
for q in xrange(j - 2, j + 3): | |
if (p, q) in self.cells: | |
result.append(self.cells[(p, q)]) | |
return result | |
def nearest(self, x, y): | |
def func((bx, by)): | |
return hypot(x - bx, y - by) | |
return sorted(self.nearby(x, y), key=func) | |
def insert(self, x, y): | |
for bx, by in self.nearby(x, y): | |
if hypot(x - bx, y - by) < self.r: | |
return False | |
i, j = self.normalize(x, y) | |
self.cells[(i, j)] = (x, y) | |
return True | |
def poisson_disc(x1, y1, x2, y2, r, n): | |
x = x1 + (x2 - x1) / 2.0 | |
y = y1 + (y2 - y1) / 2.0 | |
active = [(x, y)] | |
grid = Grid(r) | |
grid.insert(x, y) | |
while active: | |
ax, ay = random.choice(active) | |
for i in xrange(n): | |
a = random.random() * 2 * pi | |
d = random.random() * r + r | |
x = ax + cos(a) * d | |
y = ay + sin(a) * d | |
if x < x1 or y < y1 or x > x2 or y > y2: | |
continue | |
if not grid.insert(x, y): | |
continue | |
active.append((x, y)) | |
break | |
else: | |
active.remove((ax, ay)) | |
return grid | |
def render(grid): | |
scale = 2048 | |
width = height = scale | |
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, width, height) | |
dc = cairo.Context(surface) | |
dc.set_line_cap(cairo.LINE_CAP_ROUND) | |
dc.set_line_join(cairo.LINE_JOIN_ROUND) | |
dc.scale(scale, scale) | |
dc.set_source_rgb(0, 0, 0) | |
dc.paint() | |
bounds = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) | |
tess = VoronoiTess(grid.points()) | |
polygons = [] | |
for region in tess.regions: | |
points = [tess.vertices[i] for i in region] | |
polygon = Polygon(points) | |
polygon = polygon.buffer(R * -0.15) | |
if not bounds.contains(polygon): | |
continue | |
polygons.append(polygon) | |
min_area = min(x.area for x in polygons) | |
max_area = max(x.area for x in polygons) | |
for polygon in polygons: | |
points = polygon.exterior.coords | |
dc.move_to(*points[0]) | |
for point in points[1:]: | |
dc.line_to(*point) | |
p = (polygon.area - min_area) / (max_area - min_area) | |
h, s, v = colorsys.rgb_to_hsv(*color(FILL)) | |
s += p * 0.6 - 0.3 | |
v += p * 0.4 - 0.2 | |
dc.set_source_rgb(*colorsys.hsv_to_rgb(h, s, v)) | |
dc.fill_preserve() | |
dc.set_source_rgb(*color(STROKE)) | |
dc.set_line_width(R * 0.1) | |
dc.stroke() | |
return surface | |
def main(): | |
grid = poisson_disc(0, 0, 1, 1, R, 32) | |
surface = render(grid) | |
surface.write_to_png('output.png') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment