-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (111 loc) · 2.78 KB
/
main.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"flag"
"fmt"
"image"
"image/color"
"image/gif"
"io"
"math"
"os"
"time"
)
type imgData struct {
size int
scale float64
cx, cy float64
}
var (
palette = make([]color.Color, 0)
nthreadsPtr, sizePtr, nframesPtr, periodPtr *uint
nthreads int
)
const (
maxIterations = 256
mod = 0.75
)
func main() {
nthreadsPtr = flag.Uint("t", 1, "Número de hilos a usar")
sizePtr = flag.Uint("s", 256, "Tamaño del GIF")
nframesPtr = flag.Uint("n", 256, "Número de frames del GIF")
periodPtr = flag.Uint("p", 10, "Periodo de oscilación de la fase")
flag.Parse()
palette = append(palette, color.Black)
for i := 1; i <= 0xFF; i++ {
palette = append(palette, color.RGBA{0, uint8(i), 0, 0xFF})
}
nthreads = int(*nthreadsPtr)
fractal(os.Stdout)
}
func fractal(out io.Writer) {
size := int(*sizePtr)
nframes := int(*nframesPtr)
period := int(*periodPtr)
delay := period * 100 / nframes
phase := math.Pi/2 + 0.3
dphase := float64(2.0*math.Pi) / float64(nframes)
scale := 1.0 / (float64(size) / 2)
anim := gif.GIF{LoopCount: nframes}
start := time.Now()
for i := 0; i < nframes; i++ {
rect := image.Rect(0, 0, size, size)
img := image.NewPaletted(rect, palette)
cx := mod * math.Cos(phase)
cy := mod * math.Sin(phase)
var data imgData
data.cx = cx
data.cy = cy
data.size = size
data.scale = scale
var ch = make(chan bool, nthreads)
for j := 0; j < nthreads; j++ {
ch <- true
}
x := 0
for range ch {
go renderColumn(x, img, data, ch)
x++
if x >= size {
break
}
}
phase += dphase
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim)
fmt.Fprintln(os.Stderr, time.Since(start).Seconds())
}
func renderColumn(x int, img *image.Paletted, data imgData, ch chan<- bool) {
for y := 0; y < data.size; y++ {
px := float64(x-data.size/2) * data.scale
py := float64(y-data.size/2) * data.scale
iterations := computeIterations(px, py, data.cx, data.cy, maxIterations)
index := uint8(iterations)
if maxIterations != len(palette) {
index = uint8((float64(iterations) / float64(maxIterations)) * float64((len(palette))))
index = max(0, index-1)
}
img.SetColorIndex(x, y, index)
}
ch <- true
}
func computeIterations(x, y, cx, cy float64, maxIteration int) int {
zx := x
zy := y
iterations := 0
for modSquared(zx, zy) <= 4.0 && iterations < maxIteration-1 {
zx, zy = computeNext(zx, zy, cx, cy)
iterations++
}
return iterations
}
func computeNext(currX, currY, cx, cy float64) (x, y float64) {
// z_n = z_n-1^2 + c
x = currX*currX - currY*currY + cx
y = 2.0*currX*currY + cy
return x, y
}
func modSquared(x, y float64) float64 {
return x*x + y*y
}