-
Notifications
You must be signed in to change notification settings - Fork 175
/
main.go
144 lines (129 loc) · 3.37 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
144
// ex3.7 visualizes how many iterations it takes to find complex roots of a
// quartic equation using Newton's method, using different colors for
// different roots.
package main
import (
"image"
"image/color"
"image/png"
"math"
"math/cmplx"
"os"
)
type Func func(complex128) complex128
var colorPool = []color.RGBA{
{170, 57, 57, 255},
{170, 108, 57, 255},
{34, 102, 102, 255},
{45, 136, 45, 255},
}
var chosenColors = map[complex128]color.RGBA{}
func main() {
const (
xmin, ymin, xmax, ymax = -2, -2, +2, +2
width, height = 1024, 1024
)
img := image.NewRGBA(image.Rect(0, 0, width, height))
for py := 0; py < height; py++ {
y := float64(py)/height*(ymax-ymin) + ymin
for px := 0; px < width; px++ {
x := float64(px)/width*(xmax-xmin) + xmin
z := complex(x, y)
// Image point (px, py) represents complex value z.
img.Set(px, py, z4(z))
}
}
png.Encode(os.Stdout, img) // NOTE: ignoring errors
}
func mandelbrot(z complex128) color.Color {
const iterations = 200
const contrast = 15
var v complex128
for n := uint8(0); n < iterations; n++ {
v = v*v + z
if cmplx.Abs(v) > 2 {
switch {
case n > 50: // dark red
return color.RGBA{100, 0, 0, 255}
default:
// logarithmic blue gradient to show small differences on the
// periphery of the fractal.
logScale := math.Log(float64(n)) / math.Log(float64(iterations))
return color.RGBA{0, 0, 255 - uint8(logScale*255), 255}
}
}
}
return color.Black
}
// Some other interesting functions:
func acos(z complex128) color.Color {
v := cmplx.Acos(z)
blue := uint8(real(v)*128) + 127
red := uint8(imag(v)*128) + 127
return color.YCbCr{192, blue, red}
}
func sqrt(z complex128) color.Color {
v := cmplx.Sqrt(z)
blue := uint8(real(v)*128) + 127
red := uint8(imag(v)*128) + 127
return color.YCbCr{128, blue, red}
}
// f(x) = x^4 - 1
//
// z' = z - f(z)/f'(z)
// = z - (z^4 - 1) / (4 * z^3)
// = z - (z - 1/z^3) / 4
func z4(z complex128) color.Color {
f := func(z complex128) complex128 {
return z*z*z*z - 1
}
fPrime := func(z complex128) complex128 {
return (z - 1/(z*z*z)) / 4
}
return newton(z, f, fPrime)
}
// f(x) = x^4 + 2x^3 + 3x^2 + 4x - 5
//
// z' = z - f(z)/f'(z)
// = z - (z^4 + 2z^3 + 3z^2 + 4z - 5) / (4z^3 + 6z^2 + 6z + 4)
func quartic(z complex128) color.Color {
f := func(z complex128) complex128 {
return z*z*z*z + 2*z*z*z + 3*z*z + 4*z - 5
}
fPrime := func(z complex128) complex128 {
return (z*z*z*z + 2*z*z*z + 3*z*z + 4*z - 5) / (4*z*z*z + 6*z*z + 6*z + 4)
}
return newton(z, f, fPrime)
}
func newton(z complex128, f Func, fPrime Func) color.Color {
const iterations = 37
const contrast = 7
for i := uint8(0); i < iterations; i++ {
z -= fPrime(z)
if cmplx.Abs(f(z)) < 1e-6 {
root := complex(round(real(z), 4), round(imag(z), 4))
c, ok := chosenColors[root]
if !ok {
if len(colorPool) == 0 {
panic("no colors left")
}
c = colorPool[0]
colorPool = colorPool[1:]
chosenColors[root] = c
}
// Convert to YCbCr to make producing different shades easier.
y, cb, cr := color.RGBToYCbCr(c.R, c.G, c.B)
scale := math.Log(float64(i)) / math.Log(iterations)
y -= uint8(float64(y) * scale)
return color.YCbCr{y, cb, cr}
}
}
return color.Black
}
func round(f float64, digits int) float64 {
if math.Abs(f) < 0.5 {
return 0
}
pow := math.Pow10(digits)
return math.Trunc(f*pow+math.Copysign(0.5, f)) / pow
}