-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.pony
220 lines (187 loc) · 5.26 KB
/
main.pony
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use "collections"
use "itertools"
use "random"
use "time"
primitive Clockwise
fun string(): String iso^ =>
"cw".clone()
primitive CounterClockwise
fun string(): String iso^ =>
"ccw".clone()
primitive Straight
fun string(): String iso^ =>
"s".clone()
type Step is (Clockwise | CounterClockwise | Straight)
primitive Left
fun rotate(twist: Step): Direction =>
match twist
| Clockwise => Up
| CounterClockwise => Down
| Straight => Left
end
fun delta(): (ISize, ISize) =>
(-1, 0)
fun string(): String iso^ =>
"left".clone()
primitive Right
fun rotate(twist: Step): Direction =>
match twist
| Clockwise => Down
| CounterClockwise => Up
| Straight => Right
end
fun delta(): (ISize, ISize) =>
(1, 0)
fun string(): String iso^ =>
"right".clone()
primitive Up
fun rotate(twist: Step): Direction =>
match twist
| Clockwise => Right
| CounterClockwise => Left
| Straight => Up
end
fun delta(): (ISize, ISize) =>
(0, -1)
fun string(): String iso^ =>
"up".clone()
primitive Down
fun rotate(twist: Step): Direction =>
match twist
| Clockwise => Left
| CounterClockwise => Right
| Straight => Down
end
fun delta(): (ISize, ISize) =>
(0, 1)
fun string(): String iso^ =>
"up".clone()
type Direction is (Left | Right | Up | Down)
primitive DrawStep
fun apply(old_dir: Direction, new_dir: Direction, x: F64, y: F64,
dx: F64, dy: F64, offsets: Array[F64]): PathCommands
=>
let pcs = PathCommands
match (old_dir, new_dir)
| (Up, Up) =>
for o in offsets.values() do
pcs
.>command(PathMove.abs(x + o, y + dx))
.>command(PathLine.abs(x + o, y))
end
| (Down, Down) =>
for o in offsets.values() do
pcs
.>command(PathMove.abs((x + dx) - o, y))
.>command(PathLine.abs((x + dx) - o, y + dy))
end
| (Left, Left) =>
for o in offsets.values() do
pcs
.>command(PathMove.abs(x + dx, (y + dx) - o))
.>command(PathLine.abs(x, (y + dx) - o))
end
| (Right, Right) =>
for o in offsets.values() do
pcs
.>command(PathMove.abs(x, y + o))
.>command(PathLine.abs(x + dx, y + o))
end
| (Up, Right) =>
for o in offsets.values() do
pcs
.>command(PathMove.abs(x + o, y + dx))
.>command(PathArc.abs(dx - o, dy - o, 0, false, true, x + dx, y + o))
end
| (Right, Up) =>
for o in offsets.values() do
pcs
.>command(PathMove.abs(x, y + o))
.>command(PathArc.abs(o, o, 0, false, false, x + o, y))
end
| (Down, Right) =>
for o in offsets.values() do
pcs
.>command(PathMove.abs((x + dx) - o, y))
.>command(PathArc.abs(o, o, 0, false, false, x + dx, y + o))
end
| (Right, Down) =>
for o in offsets.values() do
pcs
.>command(PathMove.abs(x, y + o))
.>command(PathArc.abs(dx - o, dy - o, 0, false, true, (x + dx) - o, y + dy))
end
| (Up, Left) =>
for o in offsets.values() do
pcs
.>command(PathMove.abs(x + o, y + dx))
.>command(PathArc.abs(o, o, 0, false, false, x, (y + dx) - o))
end
| (Left, Up) =>
for o in offsets.values() do
pcs
.>command(PathMove.abs(x + dx, (y + dx) - o))
.>command(PathArc.abs(dx - o, dy - o, 0, false, true, x + o, y))
end
| (Down, Left) =>
for o in offsets.values() do
pcs
.>command(PathMove.abs((x + dx) - o, y))
.>command(PathArc.abs(dx - o, dx - o, 0, false, true, x, (y + dx) - o))
end
| (Left, Down) =>
for o in offsets.values() do
pcs
.>command(PathMove.abs(x + dx, (y + dx) - o))
.>command(PathArc.abs(o, o, 0, false, false, (x + dx) - o, y + dy))
end
end
pcs
primitive DrawSteps
fun apply(steps: Array[Step], sx: F64, sy: F64, factor: F64,
offsets: Array[F64]): PathCommands
=>
var last_dir: Direction = Up
let pcs = PathCommands
var cx = sx
var cy = sy
let scaled_offsets = Iter[F64](offsets.values())
.map[F64]({(x) => x * factor})
.collect(Array[F64])
// pcs.command(PathMove.abs(cx * factor, cy * factor))
for step in steps.values() do
let dir = last_dir.rotate(step)
let delta = dir.delta()
pcs.commands(DrawStep(last_dir, dir, cx * factor, cy * factor,
factor, factor, scaled_offsets))
cx = cx + delta._1.f64()
cy = cy + delta._2.f64()
last_dir = dir
end
pcs
primitive LaySteps
fun apply(num_steps: USize, cw: F64, ccw: F64,
random: Random): Array[Step]
=>
let steps = Array[Step]
for _ in Range(0, num_steps) do
let r = random.real()
steps.push(if r < cw then
Clockwise
elseif r < ccw then
CounterClockwise
else
Straight
end)
end
steps
actor Main
new create(env: Env) =>
let random = MT(Time.micros())
let svg = SVG.svg()
for i in Range[F64](0, 3) do
let steps = LaySteps(100, 0.4, 0.6, random)
let ds = DrawSteps(steps, 15 + i, 15 + i, 20, [0.4; 0.5; 0.6])
svg.c(SVG.path(ds.optimize(OptimizeConnectCommands)))
end
env.out.print(svg.render())