-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_utils.py
150 lines (115 loc) · 3.04 KB
/
flag_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""
This is a set of tools and weird hacks to get
./flag.py to work and closely resemble ./flag.ps.
Here, we define functions that match some of the
PostScript operators (moveto, rectfill, setcolor, etc.).
ISSUES:
- ps bg color white?
- 'lw' in PathPatch are wrong units?
- subclassing 'matplotlib.transforms.Affine2D'
lead to some weird problems.
"""
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
import matplotlib.transforms
import math
class Writer:
def __init__(self):
self.width = 720
self.height = 480
dpi = 100 #?
self.dpi = dpi
self.fig = plt.figure(figsize=(self.width/dpi,
self.height/dpi),
dpi=dpi)
self.ax = self.fig.add_axes((0,0,1,1))
self.reset()
def reset(self):
self.x = 0
self.y = 0
#path data:
self.vertices = []
self.codes = []
#store
self.rotation_rad = 0
self.tx = 0
self.ty = 0
#global holds state of drawer
wr = Writer()
#def tr(pt):
# return wr.transform(pt)
def moveto(x,y):
"""
"""
t = matplotlib.transforms.Affine2D()
t.rotate(wr.rotation_rad)
t.translate(wr.tx, wr.ty)
xx,yy = t.transform_point((x,y))
wr.x = xx
wr.y = yy
wr.vertices.append((wr.x, wr.y))
wr.codes.append(Path.MOVETO)
def rlineto(x,y):
#ugly? rlineto shouldn't
# include translation but should include rotation?
# zero-out translation entries:
#M = wr.transform.get_matrix().copy()
#M[:2,2] = 0
#wr0 = MyTransform(M)
#dx,dy = wr0((x,y))
tr = matplotlib.transforms.Affine2D()
tr.rotate(wr.rotation_rad)
dx,dy = tr.transform_point((x,y))
#relative:
wr.x += dx
wr.y += dy
wr.vertices.append((wr.x, wr.y))
wr.codes.append(Path.LINETO)
def setlinewidth(lw):
wr.linewidth = lw #default?
def setrgbcolor(r,g,b):
wr.rgb = (r,g,b) #default?
def translate(dx,dy):
wr.tx += dx
wr.ty += dy
def rotate(theta):
wr.rotation_rad += math.radians(theta)
def _add_patch(patch):
wr.ax.add_patch(patch)
wr.reset()
def fill():
path = Path(wr.vertices, wr.codes)
_add_patch(
patches.PathPatch(
path,
facecolor = wr.rgb,
edgecolor = 'none',
lw = 1,
))
def rectfill(x,y,width,height):
matplotlib.patches.Rectangle
_add_patch(
patches.Rectangle(
(x,y),
width,
height,
facecolor = wr.rgb,
edgecolor = 'none',
lw = 1,
))
def stroke():
"""
"""
path = Path(wr.vertices, wr.codes)
_add_patch(patches.PathPatch(
path,
facecolor = 'none',
edgecolor = wr.rgb,
lw = wr.linewidth,
))
def write_png(path, globals_):
wr.ax.set_xbound((0, globals_['width']))
wr.ax.set_ybound((0, globals_['height']))
wr.ax.figure.savefig(path,dpi=wr.dpi)
print("WROTE:", path)