-
-
Save Bug38/4144e9f80d60c9bd2d42b6858d70d775 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 matplotlib.pyplot as plt | |
import random, math | |
sizex = 10 | |
sizey = 30 | |
verticalweight = 4 | |
maxlenght = 8 | |
random.seed() | |
xunit = math.sqrt(3) | |
lines = [] | |
listofPoints = [] | |
points = {} | |
grid = [] | |
def drawPoint(p,c,size=1): | |
x,y = c | |
p.scatter(x * xunit, y*2 if x%2 else 1+y*2, c='k', s=size) | |
def drawLines(p): | |
for l in lines: | |
if len(l) == 1: | |
drawPoint(p, l[0]) | |
else: | |
tmp = [] | |
for t in l: | |
x,y = t | |
tmp.append([x * xunit, y*2 if x%2 else 1+y*2]) | |
xs, ys = zip(*tmp) | |
ls = p.plot(xs, ys) | |
p.setp(ls, linewidth=1, color='k') | |
def getAdjPoints(c): | |
x,y = c | |
tmplist = [[x+1,y], [x-1,y]] | |
for _ in range(verticalweight): | |
tmplist.append([x,y-1]) | |
tmplist.append([x,y+1]) | |
if x%2: | |
tmplist.append([x+1,y-1]) | |
tmplist.append([x-1,y-1]) | |
else: | |
tmplist.append([x-1,y+1]) | |
tmplist.append([x+1,y+1]) | |
retlist = [] | |
for tmp in tmplist: | |
if tmp in listofPoints: | |
retlist.append(tmp) | |
return retlist | |
# Grid generation | |
for x in range(sizex): | |
tmp = [] | |
for y in range(sizey): | |
p=[x,y] | |
tmp.append(p) | |
listofPoints.append(p) | |
grid.append(tmp) | |
# Lines generation | |
while (len(listofPoints)): | |
line = [] | |
linelen = random.randrange(maxlenght-2,maxlenght) | |
c = listofPoints.pop(random.randrange(len(listofPoints))) | |
line.append(c) | |
for i in range(linelen - 1): | |
nextc = getAdjPoints(c) | |
if len(nextc): | |
c = nextc[random.randrange(len(nextc))] | |
listofPoints.remove(c) | |
line.append(c) | |
else: | |
break | |
lines.append(line) | |
# Pattern drawing | |
drawLines(plt) | |
x = plt.gca() | |
x.axes.xaxis.set_visible(False) | |
x.axes.yaxis.set_visible(False) | |
x.figure.set_size_inches(sizex/5,sizey/5) | |
# plt.savefig("pattern.svg", format="svg") | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment