Guest User

Untitled

a guest
Sep 15th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.28 KB | None | 0 0
  1. package main
  2.  
  3. import ("image"
  4.     "image/color"
  5.     "image/png"
  6.     "os"
  7.     "time"
  8.     "fmt"
  9.     "flag")
  10.  
  11. func main() {
  12.     var iterations = flag.Int("iterations", 0, "The number of iterations of the curve.")
  13.     flag.Parse()
  14.     drag := Dragon{0, 0, 0, 0, 0, 0, 0, []image.Point{}}
  15.     start := time.Now()
  16.     drag.generate(*iterations)
  17.     fmt.Println(time.Since(start), "to generate.")
  18.     render := time.Now()
  19.     drag.render()
  20.     fmt.Println(time.Since(render), "to render.")
  21.     fmt.Println("\n", time.Since(start), "total.")
  22. }
  23.  
  24. type Dragon struct {
  25.     Dir, X, Y, Maxx, Maxy, Minx, Miny int
  26.     Points []image.Point
  27. }
  28.  
  29. func (d *Dragon) right() {
  30.     if d.Dir == 3 {
  31.         d.Dir = 0
  32.     } else {
  33.         d.Dir += 1
  34.     }
  35. }
  36.  
  37. func (d *Dragon) left() {
  38.     if d.Dir == 0 {
  39.         d.Dir = 3
  40.     } else {
  41.         d.Dir -= 1
  42.     }
  43. }
  44.  
  45. func (d *Dragon) forward() {
  46.     switch d.Dir {
  47.     case 0:
  48.         d.Y += 4
  49.         d.Points = append(d.Points, image.Point{d.X, d.Y - 3}, image.Point{d.X, d.Y - 2}, image.Point{d.X, d.Y - 1}, image.Point{d.X, d.Y})
  50.     case 1:
  51.         d.X += 4
  52.         d.Points = append(d.Points, image.Point{d.X - 3, d.Y}, image.Point{d.X - 2, d.Y}, image.Point{d.X - 1, d.Y}, image.Point{d.X, d.Y})
  53.     case 2:
  54.         d.Y -= 4
  55.         d.Points = append(d.Points, image.Point{d.X, d.Y + 3}, image.Point{d.X, d.Y + 2}, image.Point{d.X, d.Y + 1}, image.Point{d.X, d.Y})
  56.     case 3:
  57.         d.X -= 4
  58.         d.Points = append(d.Points, image.Point{d.X + 3, d.Y}, image.Point{d.X + 2, d.Y}, image.Point{d.X + 1, d.Y}, image.Point{d.X, d.Y})
  59.     }
  60.     if d.X > d.Maxx {
  61.         d.Maxx = d.X
  62.     }
  63.     if d.X < d.Minx {
  64.         d.Minx = d.X
  65.     }
  66.     if d.Y > d.Maxy {
  67.         d.Maxy = d.Y
  68.     }
  69.     if d.Y < d.Miny {
  70.         d.Miny = d.Y
  71.     }
  72. }
  73.  
  74. func (d *Dragon) a (iterations int) {
  75.     if iterations == 0 {
  76.         return
  77.     }
  78.     d.a(iterations - 1)
  79.     d.right()
  80.     d.b(iterations - 1)
  81.     d.forward()
  82.     d.right()
  83. }
  84.  
  85. func (d *Dragon) b (iterations int) {
  86.     if iterations == 0 {
  87.         return
  88.     }
  89.     d.left()
  90.     d.forward()
  91.     d.a(iterations - 1)
  92.     d.left()
  93.     d.b(iterations - 1)
  94. }
  95.  
  96. func (d *Dragon) generate (iterations int) {
  97.     d.forward()
  98.     d.a(iterations)
  99. }
  100.  
  101. func (d *Dragon) render () {
  102.     white := color.NRGBA{255, 255, 255, 255}
  103.     image := image.NewGray(image.Rect(d.Minx, d.Miny, d.Maxx + 1, d.Maxy + 1))
  104.     for _, p := range d.Points {
  105.         image.Set(p.X, p.Y, white)
  106.     }
  107.     file, _ := os.Create("dragon.png")
  108.     defer file.Close()
  109.     png.Encode(file, image)
Add Comment
Please, Sign In to add comment