[wip] trying to make amount of goids dynamic

This commit is contained in:
Nikolai Rodionov 2022-11-01 20:50:33 +01:00
parent 19d4fd0172
commit d73f02f0c2
4 changed files with 47 additions and 5 deletions

6
config.yaml Normal file
View File

@ -0,0 +1,6 @@
- step: 100
amount: 100
- step: 200
amount: 150
- step: 300
amount: 40

1
go.mod
View File

@ -7,4 +7,5 @@ require github.com/llgcode/draw2d v0.0.0-20210904075650-80aa0a2a901d
require (
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

3
go.sum
View File

@ -9,3 +9,6 @@ github.com/llgcode/ps v0.0.0-20150911083025-f1443b32eedb h1:61ndUreYSlWFeCY44JxD
github.com/llgcode/ps v0.0.0-20150911083025-f1443b32eedb/go.mod h1:1l8ky+Ew27CMX29uG+a2hNOKpeNYEQjjtiALiBlFQbY=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81 h1:00VmoueYNlNz/aHIilyyQz/MHSqGoWJzpFv/HW8xpzI=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

42
main.go
View File

@ -7,29 +7,53 @@ import (
"image"
"image/color"
"image/png"
"io/ioutil"
"math"
"math/rand"
"os"
"sort"
"github.com/llgcode/draw2d/draw2dimg"
"gopkg.in/yaml.v2"
)
// parameters
var windowWidth, windowHeight = 800, 600
var goidSize = 3
var goidColor = color.RGBA{200, 200, 100, 255} // gray, 50% transparency
var populationSize = 2
var populationSize = 10
var loops = 10000
var numNeighbours = 2
var numNeighbours = 10
var separationFactor = float64(goidSize * 5)
var coherenceFactor = 8
type steps struct {
Step int `yaml:"step"`
Amount int `yaml:"amount"`
}
func fixGoids(goids &[]*Goid, actual int, wished int) {
if actual < wished {
g := createRandomGoid()
goids = append(&goids, &g)
} else if wished < actual {
fmt.Println("it's not supported yet")
} else {
return
}
}
func main() {
fo, err := os.Create("/tmp/goids.txt")
if err != nil {
panic(err)
}
conf, err := ioutil.ReadFile("config.yaml")
if err != nil {
panic(err)
}
td := []steps{}
err = yaml.Unmarshal(conf, &td)
defer func() {
if err := fo.Close(); err != nil {
panic(err)
@ -37,19 +61,26 @@ func main() {
}()
clearScreen()
hideCursor()
var goids []*Goid
goids := make([]*Goid, 0)
for i := 0; i < populationSize; i++ {
g := createRandomGoid()
goids = append(goids, &g)
}
current_stage := 0
for i := 0; i < loops; i++ {
if i < td[current_stage].Step {
fmt.Printf("%d -- %d -- %d", i, current_stage, td[current_stage].Amount )
populationSize = td[current_stage].Amount
} else {
fixGoids(goids, populationSize, td[current_stage].Amount)
current_stage += 1
}
move(goids, fo)
frame := draw(goids)
printImage(frame.SubImage(frame.Rect))
// fo.Write()
}
showCursor()
}
@ -64,6 +95,7 @@ type Goid struct {
Color color.Color
}
func createRandomGoid() (g Goid) {
g = Goid{
X: rand.Intn(windowWidth),