diff --git a/example/example_julia.go b/example/example_julia.go new file mode 100644 index 0000000..c4b4c82 --- /dev/null +++ b/example/example_julia.go @@ -0,0 +1,25 @@ +package main + +import ( + "generativeart" + "math/rand" + "time" +) + +func julia1(z complex128) complex128 { + c := complex(-0.1, 0.651) + + z = z*z + c + + return z +} + +func main() { + rand.Seed(time.Now().Unix()) + c := generativeart.NewCanva(800, 800, 1.5, 1.5) + c.SetIterations(800) + c.SetColorSchema(generativeart.Viridis) + c.FillBackground() + c.Draw(generativeart.NewJulia(julia1, 40)) + c.ToPNG("julia.png") +} diff --git a/julia.go b/julia.go new file mode 100644 index 0000000..c80b6b9 --- /dev/null +++ b/julia.go @@ -0,0 +1,40 @@ +package generativeart + +import "math/cmplx" + +// GenFunc defines a func type used by julia set. +type GenFunc func(complex128) complex128 + +type julia struct { + fn GenFunc + maxz float64 +} + +func NewJulia(formula GenFunc, maxz float64) *julia { + return &julia{ + fn: formula, + maxz: maxz, + } +} + +// Generative draws a julia set. +func (j *julia)Generative(c *canva) { + n := len(c.opts.colorSchema) + if n > 255 { + n = 255 + } + + for i := 0; i <= c.width; i++ { + for k := 0; k <= c.height; k++ { + nit := 0 + z := complex(float64(i)/float64(c.width)*2.0*c.yaixs-c.yaixs, float64(k)/float64(c.height)*2.0*c.yaixs-c.yaixs) + + for cmplx.Abs(z) <= j.maxz && nit < c.opts.nIters { + z = j.fn(z) + nit += 1 + } + idx := uint8(nit*255/c.opts.nIters) % uint8(n) + c.img.Set(i, k, c.opts.colorSchema[idx]) + } + } +} \ No newline at end of file