the ugliest code ever written

This commit is contained in:
Nikolai Rodionov 2022-11-14 22:34:25 +01:00
parent 17d2680dd0
commit 1a11deb7bc
2 changed files with 57 additions and 9 deletions

View File

@ -1,6 +1,6 @@
- step: 10
amount: 100
neighbours: 10
neighbours: 1
- step: 100
amount: 100
neighbours: 50

60
main.go
View File

@ -12,6 +12,7 @@ import (
"math/rand"
"os"
"sort"
"strconv"
"github.com/llgcode/draw2d/draw2dimg"
"gopkg.in/yaml.v2"
@ -23,7 +24,7 @@ var goidSize = 3
var goidColor = color.RGBA{200, 200, 100, 255} // gray, 50% transparency
var populationSize = 20
var loops = 500
var numNeighbours = 10
var numNeighbours = 5
var separationFactor = float64(goidSize * 5)
var coherenceFactor = 8
@ -33,20 +34,30 @@ type steps struct {
NumNeighbours int `yaml:"neighbours"`
}
type Coor struct {
X int `yaml:"x"`
Y int `yaml:"y"`
}
type OutPut struct {
Goid int `yaml:"goid"`
Loop int `yaml:"loop"`
Data Coor `yaml:"data"`
}
func fixGoids(gs []*Goid, actual int, wished int) ([]*Goid, int) {
if actual < wished {
g := createRandomGoid()
gs = append(gs, &g)
return gs, actual + 1
} else if wished < actual {
return gs[:len(gs)-1], actual -1
return gs[:len(gs)-1], actual - 1
} else {
return gs, actual
}
}
func main() {
fo, err := os.Create("/tmp/goids.txt")
fo, err := os.Create("/tmp/goids.yaml")
if err != nil {
panic(err)
}
@ -78,11 +89,48 @@ func main() {
if i > td[current_stage].Step {
current_stage += 1
}
move(goids, fo)
move(goids, fo, i)
frame := draw(goids)
printImage(frame.SubImage(frame.Rect))
}
showCursor()
out, err := ioutil.ReadFile("/tmp/goids.yaml")
if err != nil {
panic(err)
}
output := []OutPut{}
err = yaml.Unmarshal(out, &output)
var total int = 0
for _, goid := range output {
if total < goid.Goid {
total = goid.Goid
}
}
fmt.Println(total)
for i := 0; i <= total; i++{
var x []string
var y []string
fmt.Println("GOID: ", i)
for z := 0; z <= loops; z++ {
check := false
for _, g := range output {
if g.Goid == i && g.Loop == z{
check = true
x = append(x, strconv.Itoa(g.Data.X))
y = append(y, strconv.Itoa(g.Data.Y))
break
}
}
if !check {
x = append(x, "-")
y = append(y, "-")
}
}
fmt.Printf("%v\n", x)
fmt.Printf("%v\n", y)
}
}
// Goid represents a drawn goid
@ -128,13 +176,13 @@ func (g *Goid) distance(n Goid) float64 {
}
// move the goids with the 3 classic boid rules
func move(goids []*Goid, file *os.File) {
func move(goids []*Goid, file *os.File, loop int) {
for i, goid := range goids {
neighbours := goid.nearestNeighbours(goids)
separate(goid, neighbours)
align(goid, neighbours)
cohere(goid, neighbours)
position := fmt.Sprintf("- coor: %d\n data:\n x: %d\n y: %d\n", i, goid.X, goid.Y)
position := fmt.Sprintf("- goid: %d\n loop: %d\n data:\n x: %d\n y: %d\n", i, loop, goid.X, goid.Y)
file.Write([]byte(position))
stayInWindow(goid)
}