ch3/ch3-03 #200
Replies: 5 comments
-
3.5 import ( func main() {
} func mandelbrot(z complex128) color.Color {
} |
Beta Was this translation helpful? Give feedback.
-
3.6 import ( func main() {
} func mandelbrot(z complex128) color.Color {
} |
Beta Was this translation helpful? Give feedback.
-
3.7 import ( func main() {
} func newtonFractal(z complex128, roots []complex128, maxIter int, tol float64) (int, int) { |
Beta Was this translation helpful? Give feedback.
-
3.9package main import ( var ( func main() {
} func handler(w http.ResponseWriter, r *http.Request) { func pngplot(w io.Writer, cx, cy, zoom float64) {
} func mandelbrot(z complex128) color.Color {
} |
Beta Was this translation helpful? Give feedback.
-
3.5package main
import (
"image"
"image/color"
"image/png"
"math/cmplx"
"os"
)
func mandelbrot(c complex128, maxIter int) int {
z := complex(0, 0)
for n := 0; n < maxIter; n++ {
if cmplx.Abs(z) > 2 {
return n
}
z = z*z + c
}
return maxIter
}
func getColor(iter, maxIter int) color.Color {
if iter == maxIter {
return color.Black
}
r := uint8(255 - iter*7%255)
g := uint8(iter * 9 % 255)
b := uint8(255 - iter*5%255)
return color.RGBA{R: r, G: g, B: b, A: 255}
}
func main() {
const (
width, height = 1024, 1024
xmin, ymin = -2.8, -2.8
xmax, ymax = 2.0, 2.0
maxIter = 200
)
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
c := complex(x, y)
iter := mandelbrot(c, maxIter)
col := getColor(iter, maxIter)
img.Set(px, py, col)
}
}
file, err := os.Create("mandelbrot.png")
if err != nil {
panic(err)
}
defer file.Close()
png.Encode(file, img)
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
ch3/ch3-03
中文版
https://gopl-zh.github.io/ch3/ch3-03.html
Beta Was this translation helpful? Give feedback.
All reactions