refactored, added options
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import sys, getopt
|
||||||
import os, glob, errno
|
import os, glob, errno
|
||||||
import math, random
|
import math, random
|
||||||
from PIL import Image, ImageDraw
|
from PIL import Image, ImageDraw
|
||||||
@@ -8,7 +9,89 @@ import svgwrite
|
|||||||
import datetime
|
import datetime
|
||||||
from sympy.geometry import Circle, Point
|
from sympy.geometry import Circle, Point
|
||||||
|
|
||||||
|
# __ ___ _
|
||||||
|
# / |/ /___ _(_)___
|
||||||
|
# / /|_/ / __ `/ / __ \
|
||||||
|
# / / / / /_/ / / / / /
|
||||||
|
# /_/ /_/\__,_/_/_/ /_/
|
||||||
|
def main(argv):
|
||||||
|
# Options
|
||||||
|
export_svg = False
|
||||||
|
export_bmp = False
|
||||||
|
|
||||||
|
try:
|
||||||
|
opts, args = getopt.getopt(argv,"n:",["number=", "svg", "bmp"])
|
||||||
|
print(opts)
|
||||||
|
except getopt.GetoptError as err:
|
||||||
|
print('mapgen.py [-n, --number][--svg][--bmp]')
|
||||||
|
print(str(err))
|
||||||
|
sys.exit(2)
|
||||||
|
for opt, arg in opts:
|
||||||
|
if opt == '-h':
|
||||||
|
print('mapgen.py [-n, --number]')
|
||||||
|
sys.exit()
|
||||||
|
elif opt in ("-n", "--number"):
|
||||||
|
number = int(arg)
|
||||||
|
elif opt in ("--svg"):
|
||||||
|
export_svg = True
|
||||||
|
elif opt in ("--bmp"):
|
||||||
|
export_bmp = True
|
||||||
|
|
||||||
|
if not export_svg and not export_bmp:
|
||||||
|
print('please explicitly provide --svg and/or --bmp option')
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# sys.exit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# create maps export folder
|
||||||
|
base = "maps"
|
||||||
|
now = datetime.datetime.now()
|
||||||
|
now = now.strftime("%Y-%m-%d_%X")
|
||||||
|
print(now)
|
||||||
|
directory = base + "/" + now
|
||||||
|
try:
|
||||||
|
os.makedirs(directory)
|
||||||
|
except OSError as exception:
|
||||||
|
if exception.errno != errno.EEXIST:
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
for i in range(0, number):
|
||||||
|
index = str(i) if i > 9 else '0'+str(i)
|
||||||
|
print(index)
|
||||||
|
generateMap(index, directory, export_svg, export_bmp)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# __ ___
|
||||||
|
# / |/ /___ _____
|
||||||
|
# / /|_/ / __ `/ __ \
|
||||||
|
# / / / / /_/ / /_/ /
|
||||||
|
# /_/ /_/\__,_/ .___/
|
||||||
|
# /_/
|
||||||
|
def generateMap(index, directory, svg, bmp):
|
||||||
|
nv = random.randint(5,15)
|
||||||
|
points = generatePolygon( ctrX=500, ctrY=500, aveRadius=300, irregularity=0.7, spikeyness=0.3, numVerts=nv )
|
||||||
|
# print(points)
|
||||||
|
lines = fractalize(points)
|
||||||
|
# print(lines)
|
||||||
|
|
||||||
|
if svg:
|
||||||
|
generateSVG(lines, directory, index)
|
||||||
|
|
||||||
|
if bmp:
|
||||||
|
generateBmp(lines, directory, index)
|
||||||
|
|
||||||
|
# ____ __
|
||||||
|
# / __ \____ / /_ ______ _____ ____
|
||||||
|
# / /_/ / __ \/ / / / / __ `/ __ \/ __ \
|
||||||
|
# / ____/ /_/ / / /_/ / /_/ / /_/ / / / /
|
||||||
|
# /_/ \____/_/\__, /\__, /\____/_/ /_/
|
||||||
|
# /____//____/
|
||||||
# http://stackoverflow.com/questions/8997099/algorithm-to-generate-random-2d-polygon
|
# http://stackoverflow.com/questions/8997099/algorithm-to-generate-random-2d-polygon
|
||||||
def generatePolygon( ctrX, ctrY, aveRadius, irregularity, spikeyness, numVerts ) :
|
def generatePolygon( ctrX, ctrY, aveRadius, irregularity, spikeyness, numVerts ) :
|
||||||
'''Start with the centre of the polygon at ctrX, ctrY,
|
'''Start with the centre of the polygon at ctrX, ctrY,
|
||||||
@@ -55,9 +138,7 @@ def generatePolygon( ctrX, ctrY, aveRadius, irregularity, spikeyness, numVerts )
|
|||||||
|
|
||||||
angle = angle + angleSteps[i]
|
angle = angle + angleSteps[i]
|
||||||
|
|
||||||
lines = fractalize(points)
|
return points
|
||||||
|
|
||||||
return lines
|
|
||||||
|
|
||||||
def clip(x, min, max) :
|
def clip(x, min, max) :
|
||||||
if( min > max ) : return x
|
if( min > max ) : return x
|
||||||
@@ -65,31 +146,40 @@ def clip(x, min, max) :
|
|||||||
elif( x > max ) : return max
|
elif( x > max ) : return max
|
||||||
else : return x
|
else : return x
|
||||||
|
|
||||||
|
# ______ __ ___
|
||||||
|
# / ____/________ ______/ /_____ _/ (_)___ ___
|
||||||
|
# / /_ / ___/ __ `/ ___/ __/ __ `/ / /_ / / _ \
|
||||||
|
# / __/ / / / /_/ / /__/ /_/ /_/ / / / / /_/ __/
|
||||||
|
# /_/ /_/ \__,_/\___/\__/\__,_/_/_/ /___/\___/
|
||||||
def fractalize(points) :
|
def fractalize(points) :
|
||||||
# print("Fractalize")
|
# print("Fractalize")
|
||||||
# print(points)
|
# print(points)
|
||||||
lines = []
|
lines = []
|
||||||
|
cote_fract = random.randint(1,10)
|
||||||
|
# loop through points 2 by 2 to obtain lines
|
||||||
|
# line can be fractalized or not
|
||||||
for p in range(0, len(points)):
|
for p in range(0, len(points)):
|
||||||
p1 = points[p]
|
p1 = points[p]
|
||||||
# print(p1)
|
|
||||||
# points.append(p1)
|
|
||||||
p2 = points[0] if p >= len(points)-1 else points[p+1];
|
p2 = points[0] if p >= len(points)-1 else points[p+1];
|
||||||
pts = [p1,p2]
|
line_pts = [p1,p2]
|
||||||
|
# print(line_pts)
|
||||||
|
|
||||||
# fractalize the line
|
# fractalize the line
|
||||||
if random.randint(1,10) > 3:
|
if random.randint(1,10) > cote_fract:
|
||||||
# number of fracatllization
|
# number of fracatllization
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
# loop throug points to divide then
|
# loop throug points to divide then
|
||||||
fpts = []
|
fpts = []
|
||||||
for v in range(0, len(pts)-1):
|
for v in range(0, len(line_pts)-1):
|
||||||
fp1 = pts[v]
|
fp1 = line_pts[v]
|
||||||
fpts.append(fp1)
|
fpts.append(fp1)
|
||||||
fp2 = pts[v+1];
|
fp2 = line_pts[v+1];
|
||||||
d = distance(fp1,fp2)
|
d = distance(fp1,fp2)
|
||||||
# print(d)
|
# print(d)
|
||||||
r = d*0.52
|
# r = d*0.52
|
||||||
|
fract_intesity = random.randint(501,800)* 0.001
|
||||||
|
r = d*fract_intesity
|
||||||
|
|
||||||
fps1,fps2 = circle_intersection((fp1[0],fp1[1],r), (fp2[0],fp2[1],r))
|
fps1,fps2 = circle_intersection((fp1[0],fp1[1],r), (fp2[0],fp2[1],r))
|
||||||
# print(fps1)
|
# print(fps1)
|
||||||
# print(ps2)
|
# print(ps2)
|
||||||
@@ -100,10 +190,10 @@ def fractalize(points) :
|
|||||||
|
|
||||||
# add the last point
|
# add the last point
|
||||||
fpts.append(fp2)
|
fpts.append(fp2)
|
||||||
pts = fpts
|
line_pts = fpts
|
||||||
|
|
||||||
# add the line, fractalized or not
|
# add the line, fractalized or not
|
||||||
lines.append(pts)
|
lines.append(line_pts)
|
||||||
|
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
@@ -147,34 +237,14 @@ def circle_intersection(circle1, circle2):
|
|||||||
|
|
||||||
return (xs1,ys1),(xs2,ys2)
|
return (xs1,ys1),(xs2,ys2)
|
||||||
|
|
||||||
# create maps export folder
|
# ______ ________
|
||||||
base = "maps"
|
# / ___/ | / / ____/
|
||||||
now = datetime.datetime.now()
|
# \__ \| | / / / __
|
||||||
now = now.strftime("%Y-%m-%d_%X")
|
# ___/ /| |/ / /_/ /
|
||||||
print(now)
|
# /____/ |___/\____/
|
||||||
directory = base + "/" + now
|
def generateSVG(lines, directory, index):
|
||||||
try:
|
|
||||||
os.makedirs(directory)
|
|
||||||
except OSError as exception:
|
|
||||||
if exception.errno != errno.EEXIST:
|
|
||||||
raise
|
|
||||||
|
|
||||||
|
svg = svgwrite.Drawing(filename = directory+"/map-"+index+".svg",size = ("1000px", "1000px"))
|
||||||
for i in range(0, 99):
|
|
||||||
print(i)
|
|
||||||
nv = random.randint(5,30)
|
|
||||||
lines = generatePolygon( ctrX=500, ctrY=500, aveRadius=300, irregularity=1, spikeyness=0.4, numVerts=nv )
|
|
||||||
# print(lines)
|
|
||||||
|
|
||||||
index = str(i) if i > 9 else '0'+str(i)
|
|
||||||
|
|
||||||
# ______ ________
|
|
||||||
# / ___/ | / / ____/
|
|
||||||
# \__ \| | / / / __
|
|
||||||
# ___/ /| |/ / /_/ /
|
|
||||||
# /____/ |___/\____/
|
|
||||||
|
|
||||||
svg = svgwrite.Drawing(filename = directory+"/map"+index+".svg",size = ("1000px", "1000px"))
|
|
||||||
|
|
||||||
# polygone (white background)
|
# polygone (white background)
|
||||||
polygone = []
|
polygone = []
|
||||||
@@ -214,26 +284,36 @@ for i in range(0, 99):
|
|||||||
|
|
||||||
svg.save()
|
svg.save()
|
||||||
|
|
||||||
|
# __ _ __
|
||||||
# __ _ __
|
# / /_ (_) /_____ ___ ____ _____
|
||||||
# / /_ (_) /_____ ___ ____ _____
|
# / __ \/ / __/ __ `__ \/ __ `/ __ \
|
||||||
# / __ \/ / __/ __ `__ \/ __ `/ __ \
|
# / /_/ / / /_/ / / / / / /_/ / /_/ /
|
||||||
# / /_/ / / /_/ / / / / / /_/ / /_/ /
|
# /_.___/_/\__/_/ /_/ /_/\__,_/ .___/
|
||||||
# /_.___/_/\__/_/ /_/ /_/\__,_/ .___/
|
# /_/
|
||||||
# /_/
|
def generateBmp(lines, directory, index):
|
||||||
# black = (0,0,0)
|
black = (0,0,0)
|
||||||
# white=(255,255,255)
|
white=(255,255,255)
|
||||||
# im = Image.new('RGB', (1000, 1000), white)
|
im = Image.new('RGB', (1000, 1000), white)
|
||||||
# imPxAccess = im.load()
|
imPxAccess = im.load()
|
||||||
# draw = ImageDraw.Draw(im)
|
draw = ImageDraw.Draw(im)
|
||||||
# tupVerts = list(map(tuple,verts))
|
# tupVerts = list(map(tuple,verts))
|
||||||
#
|
|
||||||
# # either use .polygon(), if you want to fill the area with a solid colour
|
polygone = []
|
||||||
# # draw.polygon( tupVerts, outline=black,fill=white )
|
for l in range(0, len(lines)):
|
||||||
#
|
for p in range(0, len(lines[l])):
|
||||||
# # or .line() if you want to control the line thickness, or use both methods together!
|
polygone.append(lines[l][p])
|
||||||
# draw.line( tupVerts+[tupVerts[0]], width=1, fill=black )
|
|
||||||
#
|
|
||||||
# # im.show()
|
# either use .polygon(), if you want to fill the area with a solid colour
|
||||||
# im.save(directory+'/map-'+str(index)+'.bmp')
|
# draw.polygon( polygone, outline=black,fill=white )
|
||||||
# # now you can save the image (im), or do whatever else you want with it.
|
|
||||||
|
# or .line() if you want to control the line thickness, or use both methods together!
|
||||||
|
draw.line( polygone+[polygone[0]], width=1, fill=black )
|
||||||
|
|
||||||
|
# im.show()
|
||||||
|
im.save(directory+'/map-'+str(index)+'.bmp')
|
||||||
|
# now you can save the image (im), or do whatever else you want with it.
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main(sys.argv[1:])
|
||||||
|
|||||||
Reference in New Issue
Block a user