diff --git a/mapgen.py b/mapgen.py index 8c11cbd..419e84f 100755 --- a/mapgen.py +++ b/mapgen.py @@ -1,6 +1,7 @@ #!/usr/bin/python # -*- coding: utf-8 -*- +import sys, getopt import os, glob, errno import math, random from PIL import Image, ImageDraw @@ -8,7 +9,89 @@ import svgwrite import datetime 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 def generatePolygon( ctrX, ctrY, aveRadius, irregularity, spikeyness, numVerts ) : '''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] - lines = fractalize(points) - - return lines + return points def clip(x, min, max) : if( min > max ) : return x @@ -65,31 +146,40 @@ def clip(x, min, max) : elif( x > max ) : return max else : return x - +# ______ __ ___ +# / ____/________ ______/ /_____ _/ (_)___ ___ +# / /_ / ___/ __ `/ ___/ __/ __ `/ / /_ / / _ \ +# / __/ / / / /_/ / /__/ /_/ /_/ / / / / /_/ __/ +# /_/ /_/ \__,_/\___/\__/\__,_/_/_/ /___/\___/ def fractalize(points) : # print("Fractalize") # print(points) 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)): p1 = points[p] - # print(p1) - # points.append(p1) 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 - if random.randint(1,10) > 3: + if random.randint(1,10) > cote_fract: # number of fracatllization for i in range(10): # loop throug points to divide then fpts = [] - for v in range(0, len(pts)-1): - fp1 = pts[v] + for v in range(0, len(line_pts)-1): + fp1 = line_pts[v] fpts.append(fp1) - fp2 = pts[v+1]; + fp2 = line_pts[v+1]; d = distance(fp1,fp2) # 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)) # print(fps1) # print(ps2) @@ -100,10 +190,10 @@ def fractalize(points) : # add the last point fpts.append(fp2) - pts = fpts + line_pts = fpts # add the line, fractalized or not - lines.append(pts) + lines.append(line_pts) return lines @@ -147,34 +237,14 @@ def circle_intersection(circle1, circle2): 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 -try: - os.makedirs(directory) -except OSError as exception: - if exception.errno != errno.EEXIST: - raise +# ______ ________ +# / ___/ | / / ____/ +# \__ \| | / / / __ +# ___/ /| |/ / /_/ / +# /____/ |___/\____/ +def generateSVG(lines, directory, index): - -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")) + svg = svgwrite.Drawing(filename = directory+"/map-"+index+".svg",size = ("1000px", "1000px")) # polygone (white background) polygone = [] @@ -214,26 +284,36 @@ for i in range(0, 99): svg.save() - - # __ _ __ - # / /_ (_) /_____ ___ ____ _____ - # / __ \/ / __/ __ `__ \/ __ `/ __ \ - # / /_/ / / /_/ / / / / / /_/ / /_/ / - # /_.___/_/\__/_/ /_/ /_/\__,_/ .___/ - # /_/ - # black = (0,0,0) - # white=(255,255,255) - # im = Image.new('RGB', (1000, 1000), white) - # imPxAccess = im.load() - # draw = ImageDraw.Draw(im) +# __ _ __ +# / /_ (_) /_____ ___ ____ _____ +# / __ \/ / __/ __ `__ \/ __ `/ __ \ +# / /_/ / / /_/ / / / / / /_/ / /_/ / +# /_.___/_/\__/_/ /_/ /_/\__,_/ .___/ +# /_/ +def generateBmp(lines, directory, index): + black = (0,0,0) + white=(255,255,255) + im = Image.new('RGB', (1000, 1000), white) + imPxAccess = im.load() + draw = ImageDraw.Draw(im) # tupVerts = list(map(tuple,verts)) - # - # # either use .polygon(), if you want to fill the area with a solid colour - # # draw.polygon( tupVerts, outline=black,fill=white ) - # - # # or .line() if you want to control the line thickness, or use both methods together! - # draw.line( tupVerts+[tupVerts[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. + + polygone = [] + for l in range(0, len(lines)): + for p in range(0, len(lines[l])): + polygone.append(lines[l][p]) + + + # either use .polygon(), if you want to fill the area with a solid colour + # draw.polygon( polygone, outline=black,fill=white ) + + # 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:])