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 - step: 10
amount: 100 amount: 100
neighbours: 10 neighbours: 1
- step: 100 - step: 100
amount: 100 amount: 100
neighbours: 50 neighbours: 50

64
main.go
View File

@ -12,6 +12,7 @@ import (
"math/rand" "math/rand"
"os" "os"
"sort" "sort"
"strconv"
"github.com/llgcode/draw2d/draw2dimg" "github.com/llgcode/draw2d/draw2dimg"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -23,30 +24,40 @@ var goidSize = 3
var goidColor = color.RGBA{200, 200, 100, 255} // gray, 50% transparency var goidColor = color.RGBA{200, 200, 100, 255} // gray, 50% transparency
var populationSize = 20 var populationSize = 20
var loops = 500 var loops = 500
var numNeighbours = 10 var numNeighbours = 5
var separationFactor = float64(goidSize * 5) var separationFactor = float64(goidSize * 5)
var coherenceFactor = 8 var coherenceFactor = 8
type steps struct { type steps struct {
Step int `yaml:"step"` Step int `yaml:"step"`
Amount int `yaml:"amount"` Amount int `yaml:"amount"`
NumNeighbours int `yaml:"neighbours"` 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) { func fixGoids(gs []*Goid, actual int, wished int) ([]*Goid, int) {
if actual < wished { if actual < wished {
g := createRandomGoid() g := createRandomGoid()
gs = append(gs, &g) gs = append(gs, &g)
return gs, actual + 1 return gs, actual + 1
} else if wished < actual { } else if wished < actual {
return gs[:len(gs)-1], actual -1 return gs[:len(gs)-1], actual - 1
} else { } else {
return gs, actual return gs, actual
} }
} }
func main() { func main() {
fo, err := os.Create("/tmp/goids.txt") fo, err := os.Create("/tmp/goids.yaml")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -78,11 +89,48 @@ func main() {
if i > td[current_stage].Step { if i > td[current_stage].Step {
current_stage += 1 current_stage += 1
} }
move(goids, fo) move(goids, fo, i)
frame := draw(goids) frame := draw(goids)
printImage(frame.SubImage(frame.Rect)) printImage(frame.SubImage(frame.Rect))
} }
showCursor() 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 // 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 // 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 { for i, goid := range goids {
neighbours := goid.nearestNeighbours(goids) neighbours := goid.nearestNeighbours(goids)
separate(goid, neighbours) separate(goid, neighbours)
align(goid, neighbours) align(goid, neighbours)
cohere(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)) file.Write([]byte(position))
stayInWindow(goid) stayInWindow(goid)
} }