Skip to content

Commit

Permalink
feat: add golang-demos
Browse files Browse the repository at this point in the history
  • Loading branch information
squillero committed Aug 1, 2024
1 parent 6ded450 commit 4ad2618
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
14 changes: 14 additions & 0 deletions 2024-25/golang-demos/tsp/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module demo

go 1.21

require github.com/hajimehoshi/ebiten/v2 v2.7.8

require (
github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895 // indirect
github.com/ebitengine/hideconsole v1.0.0 // indirect
github.com/ebitengine/purego v0.7.0 // indirect
github.com/jezek/xgb v1.1.1 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
)
16 changes: 16 additions & 0 deletions 2024-25/golang-demos/tsp/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895 h1:48bCqKTuD7Z0UovDfvpCn7wZ0GUZ+yosIteNDthn3FU=
github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895/go.mod h1:XZdLv05c5hOZm3fM2NlJ92FyEZjnslcMcNRrhxs8+8M=
github.com/ebitengine/hideconsole v1.0.0 h1:5J4U0kXF+pv/DhiXt5/lTz0eO5ogJ1iXb8Yj1yReDqE=
github.com/ebitengine/hideconsole v1.0.0/go.mod h1:hTTBTvVYWKBuxPr7peweneWdkUwEuHuB3C1R/ielR1A=
github.com/ebitengine/purego v0.7.0 h1:HPZpl61edMGCEW6XK2nsR6+7AnJ3unUxpTZBkkIXnMc=
github.com/ebitengine/purego v0.7.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/hajimehoshi/ebiten/v2 v2.7.8 h1:QrlvF2byCzMuDsbxFReJkOCbM3O2z1H/NKQaGcA8PKk=
github.com/hajimehoshi/ebiten/v2 v2.7.8/go.mod h1:Ulbq5xDmdx47P24EJ+Mb31Zps7vQq+guieG9mghQUaA=
github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4=
github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
45 changes: 45 additions & 0 deletions 2024-25/golang-demos/tsp/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright © 2024 Giovanni Squillero <[email protected]>
// https://github.com/squillero/computational-intelligence
// Free under certain conditions — see the license for details.

package main

import (
"math/rand"
)

type Dot struct {
x, y float32
}
type Polygon []Dot

// Game implements ebiten.Game interface.
type Game struct {
feed chan Polygon
cities []Dot
db []Polygon
}

func createFeed(c chan Polygon) {
for {
//time.Sleep(10 * time.Millisecond)
var p Polygon
for np := 3 + rand.Intn(10); np > 0; np -= 1 {
p = append(p, Dot{x: 1000 * rand.Float32(), y: 1000 * rand.Float32()})
}
c <- p
}
}

func main() {
var cities []Dot
for t := 0; t < 50; t += 1 {
cities = append(cities, Dot{x: 1000 * rand.Float32(), y: 1000 * rand.Float32()})
}

game := &Game{feed: make(chan Polygon), cities: cities}
// Specify the window size as you like. Here, a doubled size is specified.

go createFeed(game.feed)
startVisualizer(game)
}
5 changes: 5 additions & 0 deletions 2024-25/golang-demos/tsp/tsp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright © 2024 Giovanni Squillero <[email protected]>
// https://github.com/squillero/computational-intelligence
// Free under certain conditions — see the license for details.

package main
66 changes: 66 additions & 0 deletions 2024-25/golang-demos/tsp/visual.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright © 2024 Giovanni Squillero <[email protected]>
// https://github.com/squillero/computational-intelligence
// Free under certain conditions — see the license for details.

package main

import (
"image/color"
"log"

"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
)

// Update proceeds the game state.
// Update is called every tick (1/60 [s] by default).
func (g *Game) Update() error {
done := false
for !done {
select {
case p := <-g.feed:
g.db = append(g.db, p)
default:
done = true
}
}
return nil
}

// Draw draws the game screen.
// Draw is called every frame (typically 1/60[s] for 60Hz display).
func (g *Game) Draw(screen *ebiten.Image) {
var p, i int
for p = 0; p < len(g.db)-1; p += 1 {
y := uint8(1 + (p*128)/len(g.db))
for i = 0; i < len(g.db[p])-1; i += 1 {
vector.StrokeLine(screen, g.db[p][i].x, g.db[p][i].y, g.db[p][i+1].x, g.db[p][i+1].y, 1, color.Gray{Y: y}, false)
}
vector.StrokeLine(screen, g.db[p][i].x, g.db[p][i].y, g.db[p][0].x, g.db[p][0].y, 1, color.Gray{Y: y}, false)
}
if p < len(g.db) {
for i = 0; i < len(g.db[p])-1; i += 1 {
vector.StrokeLine(screen, g.db[p][i].x, g.db[p][i].y, g.db[p][i+1].x, g.db[p][i+1].y, 1, color.Gray{Y: 255}, false)
}
vector.StrokeLine(screen, g.db[p][i].x, g.db[p][i].y, g.db[p][0].x, g.db[p][0].y, 1, color.Gray{Y: 255}, false)
}
for _, p := range g.cities {
vector.DrawFilledCircle(screen, p.x, p.y, 5, color.RGBA{0xff, 0, 0, 0}, true)
}

}

// Layout takes the outside size (e.g., the window size) and returns the (logical) screen size.
// If you don't have to adjust the screen size with the outside size, just return a fixed size.
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return 1000, 1000
}

func startVisualizer(g *Game) {
ebiten.SetWindowSize(800, 800)
ebiten.SetWindowTitle("TSP")
// Call ebiten.RunGame to start your game loop.
if err := ebiten.RunGame(g); err != nil {
log.Fatal(err)
}
}

0 comments on commit 4ad2618

Please sign in to comment.