Set a field size in the config file (#5)

This commit is contained in:
Nikolai Rodionov 2022-12-28 15:33:23 +01:00
parent c0e37c3183
commit 55001259e4
3 changed files with 70 additions and 27 deletions

View File

@ -1,3 +1,6 @@
screen_size:
width: 20
height: 1000
last_step: 100 last_step: 100
init_amount: 10 init_amount: 10
steps: steps:

33
goids.txt Normal file
View File

@ -0,0 +1,33 @@
GOID: 0
[3 5 7 9 11 13 15 17 19 21 1 3 5 7 9 11 13 15 17 19 21 1 3 5 7 9 11 13 15 17 19 21 1 3 5 7 9 11 13 15 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 - 7 7 7 7 7 7 7 7 7 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 -]
[889 891 893 895 897 899 901 903 905 907 909 911 913 915 917 919 921 923 925 927 929 931 933 935 937 939 941 943 945 947 949 951 953 955 957 959 961 963 965 967 969 969 968 968 968 968 968 968 968 968 968 968 968 968 968 968 968 968 968 968 - 137 137 137 137 137 137 137 137 137 137 145 154 162 168 173 177 181 184 187 189 191 193 195 197 199 201 203 205 207 209 211 213 215 217 219 221 223 225 227 -]
GOID: 1
[2 3 4 5 6 7 8 9 - - - - - - - - - - - - - 14 15 16 17 18 19 20 21 0 1 2 3 4 5 6 7 8 9 10 - 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 - - - 6 6 6 6 6 6 6 6 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 0 1 2 3 4 5 6 7 8 9 10 11 12 13 -]
[320 322 324 326 328 330 332 334 - - - - - - - - - - - - - 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 - 954 954 954 954 954 954 954 954 954 954 954 954 954 954 954 954 954 954 - - - 802 802 802 802 802 802 802 802 802 769 722 671 621 575 533 495 462 433 407 384 363 344 327 312 298 285 273 262 252 243 235 228 221 214 207 200 193 186 -]
GOID: 2
[18 20 22 0 2 4 6 - - - - - - - - - - - - - - - 14 15 16 17 18 19 20 21 0 1 2 3 4 5 6 7 8 - - - 2 3 8 12 16 19 22 2 11 19 26 1 8 15 22 5 - - - - - - - - - - - - - 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 0 1 2 3 4 5 6 7 8 9 10 11 12 13 -]
[301 302 303 304 305 306 307 - - - - - - - - - - - - - - - 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 - - - 57 54 51 50 52 52 56 67 79 90 100 109 117 124 131 138 - - - - - - - - - - - - - 266 261 256 251 247 243 240 237 234 232 230 228 226 224 222 220 218 216 214 212 210 208 206 204 202 200 198 196 194 -]
GOID: 3
[3 4 5 6 7 8 - - - - - - - - - - - - - - - - - 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 - - - - - 21 3 8 10 14 20 24 2 8 14 20 26 0 6 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]
[91 93 95 97 99 101 - - - - - - - - - - - - - - - - - 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 - - - - - 4 9 18 21 32 52 66 79 91 102 112 121 129 136 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]
GOID: 4
[13 15 17 19 21 - - - - - - - - - - - - - - - - - - - 10 11 12 13 14 15 16 17 18 19 20 21 0 - - - - - - - 8 11 14 17 21 3 8 13 18 23 2 7 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]
[447 449 451 453 455 - - - - - - - - - - - - - - - - - - - 200 201 202 203 204 205 206 207 208 209 210 211 212 - - - - - - - 103 98 95 92 97 103 110 117 124 131 138 145 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]
GOID: 5
[17 19 21 1 - - - - - - - - - - - - - - - - - - - - - 9 10 11 12 13 14 15 16 17 18 19 - - - - - - - - - 12 12 12 12 12 12 12 12 12 12 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]
[467 468 469 470 - - - - - - - - - - - - - - - - - - - - - 540 542 544 546 548 550 552 554 556 558 560 - - - - - - - - - 850 859 868 876 884 891 898 905 912 919 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]
GOID: 6
[8 9 10 - - - - - - - - - - - - - - - - - - - - - - - 11 11 11 11 11 11 11 11 11 - - - - - - - - - - - 6 8 10 13 17 21 3 7 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]
[948 949 950 - - - - - - - - - - - - - - - - - - - - - - - 510 510 510 510 510 510 510 510 510 - - - - - - - - - - - 335 312 291 274 260 247 235 224 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]
GOID: 7
[11 12 - - - - - - - - - - - - - - - - - - - - - - - - - 8 10 12 14 16 18 20 - - - - - - - - - - - - - 13 15 15 14 13 12 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]
[15 15 - - - - - - - - - - - - - - - - - - - - - - - - - 830 832 834 836 838 840 842 - - - - - - - - - - - - - 19 22 22 21 20 19 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]
GOID: 8
[7 - - - - - - - - - - - - - - - - - - - - - - - - - - - 4 5 6 7 8 - - - - - - - - - - - - - - - 10 10 10 10 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]
[833 - - - - - - - - - - - - - - - - - - - - - - - - - - - 746 746 746 746 746 - - - - - - - - - - - - - - - 650 654 655 656 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]
GOID: 9
[- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 2 2 2 - - - - - - - - - - - - - - - - - 11 11 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]
[- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 718 718 718 - - - - - - - - - - - - - - - - - 588 594 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]
GOID: 10
[- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 18 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]
[- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 465 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]

61
main.go
View File

@ -19,7 +19,6 @@ import (
) )
// parameters // parameters
var windowWidth, windowHeight = 1000, 700
var goidSize = 3 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 = 0 var populationSize = 0
@ -29,10 +28,17 @@ var separationFactor = float64(goidSize * 5)
var coherenceFactor = 8 var coherenceFactor = 8
type configuration struct { type configuration struct {
Steps []steps `yaml:"steps"` Steps []steps `yaml:"steps"`
LastStep int `yaml:"last_step"` ScreenSize ScreenSize `yaml:"screen_size"`
InitAmount int `yaml:"init_amount"` LastStep int `yaml:"last_step"`
InitAmount int `yaml:"init_amount"`
} }
type ScreenSize struct {
Width int `yaml:"width"`
Height int `yaml:"height"`
}
type steps struct { type steps struct {
Step int `yaml:"step"` Step int `yaml:"step"`
Amount int `yaml:"amount"` Amount int `yaml:"amount"`
@ -43,15 +49,16 @@ type Coor struct {
X int `yaml:"x"` X int `yaml:"x"`
Y int `yaml:"y"` Y int `yaml:"y"`
} }
type OutPut struct { type OutPut struct {
Goid int `yaml:"goid"` Goid int `yaml:"goid"`
Loop int `yaml:"loop"` Loop int `yaml:"loop"`
Data Coor `yaml:"data"` Data Coor `yaml:"data"`
} }
func fixGoids(gs []*Goid, actual int, wished int) ([]*Goid, int) { func fixGoids(gs []*Goid, actual int, wished int, field ScreenSize) ([]*Goid, int) {
if actual < wished { if actual < wished {
g := createRandomGoid() g := createRandomGoid(field)
gs = append(gs, &g) gs = append(gs, &g)
return gs, actual + 1 return gs, actual + 1
} else if wished < actual { } else if wished < actual {
@ -83,13 +90,13 @@ func main() {
clearScreen() clearScreen()
hideCursor() hideCursor()
var goids []*Goid var goids []*Goid
for i := 0; i < populationSize; i++ { for i := 0; i < populationSize; i++ {
g := createRandomGoid() g := createRandomGoid(config.ScreenSize)
goids = append(goids, &g) goids = append(goids, &g)
} }
current_stage := 0 current_stage := 0
for i := 0; i < loops; i++ { for i := 0; i < loops; i++ {
goids, populationSize = fixGoids(goids, populationSize, steps[current_stage].Amount) goids, populationSize = fixGoids(goids, populationSize, steps[current_stage].Amount, config.ScreenSize)
if steps[current_stage].NumNeighbours >= populationSize { if steps[current_stage].NumNeighbours >= populationSize {
numNeighbours = populationSize numNeighbours = populationSize
} else { } else {
@ -98,8 +105,8 @@ func main() {
if i == steps[current_stage+1].Step { if i == steps[current_stage+1].Step {
current_stage += 1 current_stage += 1
} }
move(goids, fo, i) move(goids, fo, i, config.ScreenSize)
frame := draw(goids) frame := draw(goids, config.ScreenSize)
printImage(frame.SubImage(frame.Rect)) printImage(frame.SubImage(frame.Rect))
} }
showCursor() showCursor()
@ -156,10 +163,10 @@ type Goid struct {
Color color.Color Color color.Color
} }
func createRandomGoid() (g Goid) { func createRandomGoid(field ScreenSize) (g Goid) {
g = Goid{ g = Goid{
X: rand.Intn(windowWidth), X: rand.Intn(field.Width),
Y: rand.Intn(windowHeight), Y: rand.Intn(field.Height),
Vx: rand.Intn(goidSize), Vx: rand.Intn(goidSize),
Vy: rand.Intn(goidSize), Vy: rand.Intn(goidSize),
R: goidSize, R: goidSize,
@ -189,7 +196,7 @@ 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, loop int) { func move(goids []*Goid, file *os.File, loop int, field ScreenSize) {
for i, goid := range goids { for i, goid := range goids {
neighbours := goid.nearestNeighbours(goids) neighbours := goid.nearestNeighbours(goids)
separate(goid, neighbours) separate(goid, neighbours)
@ -197,21 +204,21 @@ func move(goids []*Goid, file *os.File, loop int) {
cohere(goid, neighbours) cohere(goid, neighbours)
position := fmt.Sprintf("- goid: %d\n loop: %d\n data:\n x: %d\n y: %d\n", i, loop, 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, field)
} }
} }
// if goid goes out of the window frame it comes back on the other side // if goid goes out of the window frame it comes back on the other side
func stayInWindow(goid *Goid) { func stayInWindow(goid *Goid, field ScreenSize) {
if goid.X < 0 { if goid.X < 0 {
goid.X = windowWidth - goid.X goid.X = field.Width - goid.X
} else if goid.X > windowWidth { } else if goid.X > field.Width {
goid.X = windowWidth - goid.X goid.X = field.Width - goid.X
} }
if goid.Y < 0 { if goid.Y < 0 {
goid.Y = windowHeight - goid.Y goid.Y = field.Height - goid.Y
} else if goid.Y > windowHeight { } else if goid.Y > field.Height {
goid.Y = windowHeight - goid.Y goid.Y = field.Height - goid.Y
} }
} }
@ -259,8 +266,8 @@ func cohere(g *Goid, neighbours []Goid) {
} }
// draw the goids // draw the goids
func draw(goids []*Goid) *image.RGBA { func draw(goids []*Goid, field ScreenSize) *image.RGBA {
dest := image.NewRGBA(image.Rect(0, 0, windowWidth, windowHeight)) dest := image.NewRGBA(image.Rect(0, 0, field.Width, field.Height))
gc := draw2dimg.NewGraphicContext(dest) gc := draw2dimg.NewGraphicContext(dest)
for _, goid := range goids { for _, goid := range goids {
gc.SetFillColor(goid.Color) gc.SetFillColor(goid.Color)