forked from felixpalmer/go_images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspirals.go
87 lines (77 loc) · 1.79 KB
/
spirals.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"image"
"image/color"
"image/png"
"log"
"math/rand"
"os"
"time"
)
func main() {
width, height := 2048, 1024
canvas := NewCanvas(image.Rect(0, 0, width, height))
canvas.DrawGradient()
// Draw a set of spirals randomly over the image
rand.Seed(time.Now().UTC().UnixNano())
for i := 0; i < 100; i++ {
x := float64(width) * rand.Float64()
y := float64(height) * rand.Float64()
color := color.RGBA{uint8(rand.Intn(255)),
uint8(rand.Intn(255)),
uint8(rand.Intn(255)),
255}
canvas.DrawSpiral(color, Vector{x, y})
}
outFilename := "spirals.png"
outFile, err := os.Create(outFilename)
if err != nil {
log.Fatal(err)
}
defer outFile.Close()
log.Print("Saving image to: ", outFilename)
png.Encode(outFile, canvas)
}
type Canvas struct {
image.RGBA
}
func NewCanvas(r image.Rectangle) *Canvas {
canvas := new(Canvas)
canvas.RGBA = *image.NewRGBA(r)
return canvas
}
func (c Canvas) DrawGradient() {
size := c.Bounds().Size()
for x := 0; x < size.X; x++ {
for y := 0; y < size.Y; y++ {
color := color.RGBA{
uint8(255 * x / size.X),
uint8(255 * y / size.Y),
55,
255}
c.Set(x, y, color)
}
}
}
func (c Canvas) DrawLine(color color.RGBA, from Vector, to Vector) {
delta := to.Sub(from)
length := delta.Length()
x_step, y_step := delta.X/length, delta.Y/length
limit := int(length + 0.5)
for i := 0; i < limit; i++ {
x := from.X + float64(i)*x_step
y := from.Y + float64(i)*y_step
c.Set(int(x), int(y), color)
}
}
func (c Canvas) DrawSpiral(color color.RGBA, from Vector) {
dir := Vector{0, 5}
last := from
for i := 0; i < 10000; i++ {
next := last.Add(dir)
c.DrawLine(color, last, next)
dir.Rotate(0.03)
dir.Scale(0.999)
last = next
}
}