Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import "image"
- import "image/color"
- import "image/png"
- import "os"
- import "strings"
- func main() {
- renderDragon(dragonString(20, "Fa"))
- }
- func dragonString(iterations int, dragon string) string {
- var outstr = strings.Split(dragon, "")
- for i := 0; i < iterations; i++ {
- var tmpout = []string{""}
- for _, s := range outstr {
- switch s {
- case "a":
- tmpout = append(tmpout, "a", "R", "b", "F", "R")
- case "b":
- tmpout = append(tmpout, "L", "F", "a", "L", "b")
- default:
- tmpout = append(tmpout, string(s))
- }
- }
- outstr = tmpout
- }
- return strings.Join(outstr, "")
- }
- type Point struct {
- x int
- y int
- }
- func renderDragon(dragon string) {
- var x, y int = 0, 0
- var dir int = 0
- var points []Point
- for _ , s := range dragon {
- switch s {
- case 76:
- if dir == 0 {
- dir = 3
- } else {
- dir -= 1
- }
- case 82:
- if dir == 3 {
- dir = 0
- } else {
- dir += 1
- }
- case 70:
- switch dir {
- case 0:
- y += 4
- points = append(points, Point{x, y - 3},Point{x, y - 2},Point{x, y - 1},Point{x, y})
- case 1:
- x += 4
- points = append(points, Point{x - 3, y},Point{x - 2, y},Point{x - 1, y},Point{x, y})
- case 2:
- y -= 4
- points = append(points, Point{x, y + 3},Point{x, y + 2},Point{x, y + 1},Point{x, y})
- case 3:
- x -= 4
- points = append(points, Point{x + 3, y},Point{x + 2, y},Point{x + 1, y},Point{x, y})
- }
- }
- }
- var minx, miny, maxx, maxy int
- for _, i := range points {
- if i.x < minx {
- minx = i.x
- }
- if i.y < miny {
- miny = i.y
- }
- if i.x > maxx {
- maxx = i.x
- }
- if i.y > maxy {
- maxy = i.y
- }
- }
- image := image.NewRGBA(image.Rect(minx, miny, maxx + 1, maxy + 1))
- white := color.NRGBA{254, 254, 254, 254}
- for _, i := range points {
- image.Set(i.x, i.y, white)
- }
- png.Encode(os.Stdout, image)
- }
Add Comment
Please, Sign In to add comment