Created
January 2, 2021 17:40
-
-
Save nst/cf2cc2d206b19061a3476bab67d9058b to your computer and use it in GitHub Desktop.
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
import cairo | |
import random | |
import sys | |
# Nicolas Seriot | |
# 2020-12 | |
# http://seriot.ch/temp/maze.png | |
def draw(img, c, r): | |
img.move_to(c,r) | |
img.line_to(c+1,r+1) | |
img.stroke() | |
ROWS = 240 | |
COLS = 320 | |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, COLS*2-1, ROWS*2-1) | |
img = cairo.Context(surface) | |
img.set_antialias(cairo.ANTIALIAS_NONE) | |
img.set_source_rgb(0,0,0) | |
img.paint() | |
img.set_line_width(1) | |
img.set_source_rgb(1,1,1) | |
for c in range(COLS): | |
for r in range(ROWS): | |
draw(img, c*2, r*2) | |
m = [[0 for col in range(COLS)] for row in range(ROWS)] | |
c,r = 0,0 | |
q = [] | |
q.append((c,r)) | |
m[c][r] = 1 | |
headings = [(0,-1), (0,1), (-1,0), (1,0)] | |
while q: | |
deltas = [(dc,dr) for (dc,dr) in headings | |
if c+dc >= 0 and c+dc < COLS | |
and r+dr >= 0 and r+dr < ROWS | |
and m[r+dr][c+dc] == 0] | |
if len(deltas) == 0: | |
c,r = q.pop() | |
continue | |
dc,dr = random.choice(deltas) | |
draw(img,c*2+dc,r*2+dr) | |
c,r = c+dc,r+dr | |
q.append((c,r)) | |
m[r][c] = 1 | |
surface.write_to_png("maze.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment