Created
December 28, 2018 05:18
-
-
Save jtanx/844e60ddf43bb865b47786b373b58a01 to your computer and use it in GitHub Desktop.
Flag Drawing V1
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
path-start | |
stroke-width 1 | |
stroke #ffffff | |
fill #ffffff | |
path m 449.96406,299.9134 c -105.26305,-44.48626 -58.60174,-181.58185 42.06956,-174.6907 -20.36609,10.46694 -23.31775,29.99772 -11.68704,48.09021 13.02444,20.2558 -1.19897,52.84856 -18.80577,60.7674 -28.93485,13.02443 -34.72791,47.74999 -11.57675,65.83309 z | |
path-end | |
path-start | |
stroke-width 1 | |
stroke #de2910 | |
fill #de2910 | |
path m 444.27188,200.91974 -5.91976,9.29378 -2.14454,-10.8142 -10.67812,-2.75928 9.62461,-5.3895 -0.67104,-10.99955 8.08542,7.48945 10.25578,-4.04271 -4.61053,10.00942 7.00143,8.50541 z | |
path-end | |
path-start | |
stroke-width 1 | |
stroke #ffffff | |
fill #ffffff | |
path "m 450.56002,298.75902 c -12.73114,-6.53451 -22.9963,-20.15491 -27.46839,-36.43134 -5.11498,-18.66969 -2.17269,-38.74247 8.08308,-55.03768 l -2.20789,-1.39371 c -10.64057,16.92871 -13.69313,37.74293 -8.38575,57.11886 4.72784,17.22201 15.21355,31.09815 28.78703,38.06438 z | |
path-end |
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 turtle | |
from os import path | |
def drawRect(t, w, h, x, y, fill): | |
mat = ((1,0),(1,1), \ | |
(0,1),(0,0)) | |
t.up() | |
t.goto(x,y) | |
t.pencolor(fill) | |
t.fillcolor(fill) | |
t.down() | |
t.begin_fill() | |
for i,j in mat: | |
t.goto(x + i*w, y + j*h) | |
t.end_fill() | |
t.up() | |
def drawPath(t, pts, width, outline, fill): | |
t.up() | |
t.goto(*pts[0]) | |
t.pencolor(outline) | |
t.width(width) | |
t.down() | |
if fill: | |
t.fillcolor(fill) | |
t.begin_fill() | |
for point in pts: | |
t.goto(*point) | |
if fill: | |
t.end_fill() | |
t.up() | |
def svgPathToCoords(path): | |
start = False | |
isRelative = True | |
path = path.split() | |
pts = [] | |
for e in path: | |
if e == 'm' or e == 'c': | |
start = True | |
isRelative = True | |
elif e == 'M' or e == 'C': | |
start = True | |
isRelative = False | |
elif e == 'z' or e == 'Z': | |
start = False | |
if pts: | |
pts.append(pts[0]) #Close path w/straight line | |
elif start == True and "," in e: | |
x,y = e.split(",")[:2] | |
x = float(x) | |
y = float(y) | |
if isRelative and pts: | |
pts.append((pts[-1][0]+x,pts[-1][1]+y)) | |
else: | |
pts.append((x,y)) | |
#else: print ("Unknown parameter:", e) | |
return pts | |
def lines(f): | |
""" Takes file f and returns contents as a list of lines """ | |
lines = [] | |
if path.exists(f): | |
fp = open(f) | |
lines = [line.strip() for line in fp] | |
fp.close() | |
return lines | |
def parsePaths(f): | |
paths = [] | |
start = False | |
path = [None, None, None, None] | |
for line in lines(f): | |
if line == "path-start": | |
start = True | |
elif line == "path-end": | |
start = False | |
paths.append(tuple(path)) | |
path = [None,None,None,None] | |
else: | |
split = line.find(" ") | |
attrib = (line[:split], line[split+1:]) | |
if attrib[0] == "stroke-width": | |
path[1] = float(attrib[1]) | |
elif attrib[0] == "stroke": | |
path[2] = attrib[1] | |
elif attrib[0] == "fill": | |
if attrib[1] == "none": | |
path[3] = None | |
else: | |
path[3] = attrib[1] | |
elif attrib[0] == "path": | |
path[0] = svgPathToCoords(attrib[1]) | |
return paths | |
""" Wales data """ | |
rectData = [(900,600,0,0,"#de2910")] | |
def drawHK(w): | |
t = turtle.Turtle() | |
t.screen.setup(900,600) | |
t.screen.setworldcoordinates(0,600,900,0) | |
for rect in rectData: | |
print(rect) | |
drawRect(t, *rect) | |
for path in parsePaths("Hong Kong.txt"): | |
drawPath(t, *path) | |
t.screen.mainloop() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment