-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculator.go
39 lines (34 loc) · 1.02 KB
/
calculator.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
package mandelbrot
import (
"math/cmplx"
)
type algorithm func(complex128, complex128) complex128
// Calculator has methods for performing core mandelbrot operations.
// This class is thread-safe.
type Calculator struct {
maxIterations int
alg algorithm
}
// Score returns a value between 0 and 1 for the probability that p is
// in the Mandelbrot set.
func (c *Calculator) Score(p complex128) float64 {
z := p
var i int
for ; i < c.maxIterations; i++ {
// TODO(mhutchinson): potentially look for cycles and break earlier.
// The tightness of the cycle could be interesting data in the return type.
z = c.alg(z, p)
if cmplx.Abs(z) > 2 {
break
}
}
return float64(i) / float64(c.maxIterations)
}
// NewQuadraticCalculator returns a Calculator which classifies members for the classic mandelbrot set.
func NewQuadraticCalculator(maxIterations int) *Calculator {
var alg algorithm = func(z, p complex128) complex128 { return z*z + p }
return &Calculator{
maxIterations: maxIterations,
alg: alg,
}
}