/
run.go
83 lines (75 loc) · 1.73 KB
/
run.go
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
package physarum
import (
"fmt"
"image/png"
"math/rand"
"time"
)
const (
width = 1024
height = 1024
particles = 1 << 22
iterations = 400
blurRadius = 1
blurPasses = 2
zoomFactor = 1
)
func one(model *Model, iterations int) {
now := time.Now().UTC().UnixNano() / 1000
path := fmt.Sprintf("out%d.png", now)
fmt.Println()
fmt.Println(path)
fmt.Println(len(model.Particles), "particles")
PrintConfigs(model.Configs, model.AttractionTable)
SummarizeConfigs(model.Configs)
for i := 0; i < iterations; i++ {
model.Step()
}
palette := RandomPalette()
im := Image(model.W, model.H, model.Data(), palette, 0, 0, 1/2.2)
SavePNG(path, im, png.DefaultCompression)
}
func frames(model *Model, rate int) {
palette := RandomPalette()
saveImage := func(path string, w, h int, grids [][]float32, ch chan bool) {
max := particles / float32(width*height) * 20
im := Image(w, h, grids, palette, 0, max, 1/2.2)
SavePNG(path, im, png.BestSpeed)
if ch != nil {
ch <- true
}
}
ch := make(chan bool, 1)
ch <- true
for i := 0; ; i++ {
fmt.Println(i)
model.Step()
if i%rate == 0 {
<-ch
path := fmt.Sprintf("frame%08d.png", i/rate)
go saveImage(path, model.W, model.H, model.Data(), ch)
}
}
}
func Run() {
if false {
n := 2 + rand.Intn(4)
configs := RandomConfigs(n)
table := RandomAttractionTable(n)
model := NewModel(
width, height, particles, blurRadius, blurPasses, zoomFactor,
configs, table)
frames(model, 3)
}
for {
n := 2 + rand.Intn(4)
configs := RandomConfigs(n)
table := RandomAttractionTable(n)
model := NewModel(
width, height, particles, blurRadius, blurPasses, zoomFactor,
configs, table)
start := time.Now()
one(model, iterations)
fmt.Println(time.Since(start))
}
}