|
| 1 | +package mandelbrot |
| 2 | + |
| 3 | +import ( |
| 4 | + "image/color" |
| 5 | + "math" |
| 6 | + "math/big" |
| 7 | + "math/cmplx" |
| 8 | +) |
| 9 | + |
| 10 | +func mandelbrot64(z complex128) color.Color { |
| 11 | + const iterations = 200 |
| 12 | + const contrast = 15 |
| 13 | + var v complex64 |
| 14 | + for n := uint8(0); n < iterations; n++ { |
| 15 | + v = v*v + complex64(z) |
| 16 | + if cmplx.Abs(complex128(v)) > 2 { |
| 17 | + if n > 50 { |
| 18 | + return color.RGBA{100, 0, 0, 255} |
| 19 | + } |
| 20 | + scale := math.Log(float64(n)) / math.Log(float64(iterations)) |
| 21 | + return color.RGBA{0, 0, 255 - uint8(scale*255), 255} |
| 22 | + } |
| 23 | + } |
| 24 | + return color.Black |
| 25 | +} |
| 26 | + |
| 27 | +func mandelbrot128(z complex128) color.Color { |
| 28 | + const iterations = 200 |
| 29 | + const contrast = 15 |
| 30 | + var v complex128 |
| 31 | + for n := uint8(0); n < iterations; n++ { |
| 32 | + v = v*v + z |
| 33 | + if cmplx.Abs(v) > 2 { |
| 34 | + if n > 50 { |
| 35 | + return color.RGBA{100, 0, 0, 255} |
| 36 | + } |
| 37 | + scale := math.Log(float64(n)) / math.Log(float64(iterations)) |
| 38 | + return color.RGBA{0, 0, 255 - uint8(scale*255), 255} |
| 39 | + } |
| 40 | + } |
| 41 | + return color.Black |
| 42 | +} |
| 43 | + |
| 44 | +func mandelbrotBigFloat(z complex128) color.Color { |
| 45 | + const iterations = 200 |
| 46 | + const contrast = 15 |
| 47 | + zR := (&big.Float{}).SetFloat64(real(z)) |
| 48 | + zI := (&big.Float{}).SetFloat64(imag(z)) |
| 49 | + var vR, vI = &big.Float{}, &big.Float{} |
| 50 | + for i := uint8(0); i < iterations; i++ { |
| 51 | + vR2, vI2 := &big.Float{}, &big.Float{} |
| 52 | + vR2.Mul(vR, vR).Sub(vR2, (&big.Float{}).Mul(vI, vI)).Add(vR2, zR) |
| 53 | + vI2.Mul(vR, vI).Mul(vI2, big.NewFloat(2)).Add(vI2, zI) |
| 54 | + vR, vI = vR2, vI2 |
| 55 | + squareSum := &big.Float{} |
| 56 | + squareSum.Mul(vR, vR).Add(squareSum, (&big.Float{}).Mul(vI, vI)) |
| 57 | + if squareSum.Cmp(big.NewFloat(4)) == 1 { |
| 58 | + if i > 50 { |
| 59 | + return color.RGBA{100, 0, 0, 255} |
| 60 | + } |
| 61 | + scale := math.Log(float64(i)) / math.Log(float64(iterations)) |
| 62 | + return color.RGBA{0, 0, 255 - uint8(scale*255), 255} |
| 63 | + } |
| 64 | + } |
| 65 | + return color.Black |
| 66 | +} |
| 67 | + |
| 68 | +func mandelbrotBigRat(z complex128) color.Color { |
| 69 | + const iterations = 200 |
| 70 | + const contrast = 15 |
| 71 | + zR := (&big.Rat{}).SetFloat64(real(z)) |
| 72 | + zI := (&big.Rat{}).SetFloat64(imag(z)) |
| 73 | + var vR, vI = &big.Rat{}, &big.Rat{} |
| 74 | + for i := uint8(0); i < iterations; i++ { |
| 75 | + vR2, vI2 := &big.Rat{}, &big.Rat{} |
| 76 | + vR2.Mul(vR, vR).Sub(vR2, (&big.Rat{}).Mul(vI, vI)).Add(vR2, zR) |
| 77 | + vI2.Mul(vR, vI).Mul(vI2, big.NewRat(2, 1)).Add(vI2, zI) |
| 78 | + vR, vI = vR2, vI2 |
| 79 | + squareSum := &big.Rat{} |
| 80 | + squareSum.Mul(vR, vR).Add(squareSum, (&big.Rat{}).Mul(vI, vI)) |
| 81 | + if squareSum.Cmp(big.NewRat(4, 1)) == 1 { |
| 82 | + if i > 50 { |
| 83 | + return color.RGBA{100, 0, 0, 255} |
| 84 | + } |
| 85 | + scale := math.Log(float64(i)) / math.Log(float64(iterations)) |
| 86 | + return color.RGBA{0, 0, 255 - uint8(scale*255), 255} |
| 87 | + } |
| 88 | + } |
| 89 | + return color.Black |
| 90 | +} |
0 commit comments