pixclient/examples/markov/markov.go

79 lines
1.4 KiB
Go

package main
import (
"git.jcg.re/jcgruenhage/pixclient"
"math/rand"
"net"
"strconv"
)
func main() {
c := make(chan int)
for i := 0; i < 3; i++ {
go markov(strconv.Itoa(i), c)
}
print(<- c)
}
func markov(seed string, c chan int) {
client := pixclient.NewClient(net.IPv4(127, 0, 0, 1), 1235)
x, y := client.Size()
currentX := rand.Intn(x)
currentY := rand.Intn(y)
r, g, b := byte(255), byte(0), byte(0)
counter := 0
var direction int
for true {
oldDirection := direction
for ((currentX == x-1) && (direction == 1)) || ((currentX == 0) && (direction == 3)) || ((currentY == y-1) && (direction == 2)) || ((currentY == 0) && (direction == 0) || oldDirection == direction) {
direction = rand.Intn(4)
}
if direction == 0 {
currentY--
} else if direction == 1 {
currentX++
} else if direction == 2 {
currentY++
} else if direction == 3 {
currentX--
} else {
panic("Illegal Direction!")
}
if counter%50 == 0 {
iterateColor(&r, &g, &b)
}
client.WritePixel(currentX, currentY, r, g, b, 50)
counter++
}
}
func iterateColor(r, g, b *byte) {
if *r == 255 {
if *b != 0 {
*b--
} else if *g != 255 {
*g++
} else {
*r--
}
} else if *g == 255 {
if *r != 0 {
*r--
} else if *b != 255 {
*b++
} else {
*g--
}
} else if *b == 255 {
if *g != 0 {
*g--
} else if *r != 255 {
*r++
} else {
*b--
}
} else {
panic("Illegal state")
}
}