Guest User

Untitled

a guest
Sep 15th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.86 KB | None | 0 0
  1. package main
  2.  
  3. import "image"
  4. import "image/color"
  5. import "image/png"
  6. import "os"
  7. import "strings"
  8.  
  9. func main() {
  10.     renderDragon(dragonString(20, "Fa"))
  11. }
  12.  
  13. func dragonString(iterations int, dragon string) string {
  14.     var outstr = strings.Split(dragon, "")
  15.     for i := 0; i < iterations; i++ {
  16.         var tmpout = []string{""}
  17.         for _, s := range outstr {
  18.             switch s {
  19.             case "a":
  20.                 tmpout = append(tmpout, "a", "R", "b", "F", "R")
  21.             case "b":
  22.                 tmpout = append(tmpout, "L", "F", "a", "L", "b")
  23.             default:
  24.                 tmpout = append(tmpout, string(s))
  25.             }
  26.         }
  27.         outstr = tmpout
  28.     }
  29.     return strings.Join(outstr, "")
  30. }
  31.  
  32. type Point struct {
  33.     x int
  34.     y int
  35. }
  36.  
  37. func renderDragon(dragon string) {
  38.     var x, y int = 0, 0
  39.     var dir int = 0
  40.     var points []Point
  41.     for _ , s := range dragon {
  42.         switch s {
  43.         case 76:
  44.             if dir == 0 {
  45.                 dir = 3
  46.             } else {
  47.                 dir -= 1
  48.             }
  49.         case 82:
  50.             if dir == 3 {
  51.                 dir = 0
  52.             } else {
  53.                 dir += 1
  54.             }
  55.         case 70:
  56.             switch dir {
  57.             case 0:
  58.                 y += 4
  59.                 points = append(points, Point{x, y - 3},Point{x, y - 2},Point{x, y - 1},Point{x, y})
  60.             case 1:
  61.                 x += 4
  62.                 points = append(points, Point{x - 3, y},Point{x - 2, y},Point{x - 1, y},Point{x, y})
  63.             case 2:
  64.                 y -= 4
  65.                 points = append(points, Point{x, y + 3},Point{x, y + 2},Point{x, y + 1},Point{x, y})
  66.             case 3:
  67.                 x -= 4
  68.                 points = append(points, Point{x + 3, y},Point{x + 2, y},Point{x + 1, y},Point{x, y})
  69.             }
  70.         }
  71.     }
  72.     var minx, miny, maxx, maxy int
  73.     for _, i := range points {
  74.         if i.x < minx {
  75.             minx = i.x
  76.         }
  77.         if i.y < miny {
  78.             miny = i.y
  79.         }
  80.         if i.x > maxx {
  81.             maxx = i.x
  82.         }
  83.         if i.y > maxy {
  84.             maxy = i.y
  85.         }
  86.     }
  87.     image := image.NewRGBA(image.Rect(minx, miny, maxx + 1, maxy + 1))
  88.     white := color.NRGBA{254, 254, 254, 254}
  89.     for _, i := range points {
  90.         image.Set(i.x, i.y, white)
  91.    
  92.     }
  93.     png.Encode(os.Stdout, image)
  94. }
Add Comment
Please, Sign In to add comment