Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
53
Task/Langtons-ant/Go/langtons-ant.go
Normal file
53
Task/Langtons-ant/Go/langtons-ant.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"image/png"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
up = iota
|
||||
rt
|
||||
dn
|
||||
lt
|
||||
)
|
||||
|
||||
func main() {
|
||||
bounds := image.Rect(0, 0, 100, 100)
|
||||
im := image.NewGray(bounds)
|
||||
gBlack := color.Gray{0}
|
||||
gWhite := color.Gray{255}
|
||||
draw.Draw(im, bounds, image.NewUniform(gWhite), image.ZP, draw.Src)
|
||||
pos := image.Point{50, 50}
|
||||
dir := up
|
||||
for pos.In(bounds) {
|
||||
switch im.At(pos.X, pos.Y).(color.Gray).Y {
|
||||
case gBlack.Y:
|
||||
im.SetGray(pos.X, pos.Y, gWhite)
|
||||
dir--
|
||||
case gWhite.Y:
|
||||
im.SetGray(pos.X, pos.Y, gBlack)
|
||||
dir++
|
||||
}
|
||||
if dir&1 == 1 {
|
||||
pos.X += 1 - dir&2
|
||||
} else {
|
||||
pos.Y -= 1 - dir&2
|
||||
}
|
||||
}
|
||||
f, err := os.Create("ant.png")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
if err = png.Encode(f, im); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
if err = f.Close(); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue