Guest User

Untitled

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