This repository has been archived by the owner on Sep 23, 2023. It is now read-only.
/
newton.go
198 lines (171 loc) · 5.01 KB
/
newton.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"image"
"image/color"
"image/png"
"log"
"math/cmplx"
"net/http"
"strconv"
)
const epsilon = 1e-5
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var x float64 = 0
var y float64 = 0
var zoom float64 = 1
qx := r.URL.Query().Get("x")
if qx != "" {
// int x
fx, err := strconv.ParseFloat(qx, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Bad Request"))
}
x = fx
}
qy := r.URL.Query().Get("y")
if qy != "" {
// int y
fy, err := strconv.ParseFloat(qy, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Bad Request"))
}
y = fy
}
qzoom := r.URL.Query().Get("zoom")
if qzoom != "" {
// int y
fzoom, err := strconv.ParseFloat(qzoom, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Bad Request"))
}
zoom = fzoom
}
fractal(w, x, y, zoom)
})
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}
func fractal(w http.ResponseWriter, ox float64, oy float64, zoom float64) {
const (
xmin, ymin, xmax, ymax = -2, -2, +2, +2
width, height = 1024, 1024
iterations = 200
)
var newXmin float64 = xmin / zoom
var newXmax float64 = xmax / zoom
var newYmin float64 = ymin / zoom
var newYmax float64 = ymax / zoom
img := image.NewRGBA(image.Rect(0, 0, width, height))
for py := 0; py < height; py++ {
y := float64(py)/height*(newYmax-newYmin) + newYmin + oy
for px := 0; px < width; px++ {
x := float64(px)/width*(newXmax-newXmin) + newXmin + ox
z := complex(x, y)
img.Set(px, py, newton(z, iterations))
}
}
// new image
nimg := image.NewRGBA(image.Rect(0, 0, width, height))
for py := 0; py < height; py++ {
for px := 0; px < width; px++ {
var r int
var g int
var b int
var a int
if px == 1023 {
// 終端に到達しているなら、色の平均を求めない
if py == 1023 {
nimg.Set(px, py, img.RGBAAt(px, py))
continue
}
// x終端に到達している場合は、直下のピクセルと比べて色の平均を取得する
r += int(img.RGBAAt(px, py).R)
r += int(img.RGBAAt(px, py+1).R)
r /= 2
g += int(img.RGBAAt(px, py).G)
g += int(img.RGBAAt(px, py+1).G)
g /= 2
b += int(img.RGBAAt(px, py).B)
b += int(img.RGBAAt(px, py+1).B)
b /= 2
a += int(img.RGBAAt(px, py).A)
a += int(img.RGBAAt(px, py+1).A)
a /= 2
nimg.Set(px, py, color.RGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: uint8(a)})
continue
}
// x終端に到達している場合は、右のピクセルと比べて色の平均を取得する
if py == 1023 {
r += int(img.RGBAAt(px, py).R)
r += int(img.RGBAAt(px+1, py).R)
r /= 2
g += int(img.RGBAAt(px, py).G)
g += int(img.RGBAAt(px+1, py).G)
g /= 2
b += int(img.RGBAAt(px, py).B)
b += int(img.RGBAAt(px+1, py).B)
b /= 2
a += int(img.RGBAAt(px, py).A)
a += int(img.RGBAAt(px+1, py).A)
a /= 2
nimg.Set(px, py, color.RGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: uint8(a)})
continue
}
// それ以外は、右、右下、直下の3pxと自分自身の4pxで色の平均を割り出す
r += int(img.RGBAAt(px, py).R)
r += int(img.RGBAAt(px+1, py).R)
r += int(img.RGBAAt(px, py+1).R)
r += int(img.RGBAAt(px+1, py+1).R)
r /= 4
g += int(img.RGBAAt(px, py).G)
g += int(img.RGBAAt(px+1, py).G)
g += int(img.RGBAAt(px, py+1).G)
g += int(img.RGBAAt(px+1, py+1).G)
g /= 4
b += int(img.RGBAAt(px, py).B)
b += int(img.RGBAAt(px+1, py).B)
b += int(img.RGBAAt(px, py+1).B)
b += int(img.RGBAAt(px+1, py+1).B)
b /= 4
a += int(img.RGBAAt(px, py).A)
a += int(img.RGBAAt(px+1, py).A)
a += int(img.RGBAAt(px, py+1).A)
a += int(img.RGBAAt(px+1, py+1).A)
a /= 4
nimg.Set(px, py, color.RGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: uint8(a)})
}
}
png.Encode(w, nimg)
}
func newton(z complex128, iterations uint8) color.Color {
const contrast = 15
for n := uint8(0); n < iterations; n++ {
// ニュートン法のアルゴリズムは右記を参照. https://algorithm.joho.info/mathematics/newton-method-program/
// 漸化式 αn - f(αn) / df(αn)
z = z - f(z)/df(z)
// z^4 - 1 = 0のとりうる解は、z = ±1 または z = ±iなので
// それぞれの解とzの差が0近似値となったものを正解値とする
if cmplx.Abs(1-z) < epsilon {
return color.RGBA{R: contrast * n, G: 0, B: 0, A: 255}
} else if cmplx.Abs(-1-z) < epsilon {
return color.RGBA{R: 0, G: 0, B: contrast * n, A: 255}
} else if cmplx.Abs(1i-z) < epsilon {
return color.RGBA{R: 0, G: contrast * n, B: 0, A: 255}
} else if cmplx.Abs(-1i-z) < epsilon {
return color.RGBA{R: 0, G: contrast * n, B: contrast * n, A: 255}
}
}
return color.Black
}
// z^4 - 1 = 0を求める関数
func f(x complex128) complex128 {
return x*x*x*x - 1
}
// 導関数
// https://ja.wolframalpha.com/input/?i=z%5E4+-1%E3%81%AE%E5%B0%8E%E9%96%A2%E6%95%B0
func df(x complex128) complex128 {
return 4 * x * x * x
}