From 664f27d42187705237d863df91a8799202fc2262 Mon Sep 17 00:00:00 2001 From: Yongwei Xing Date: Tue, 2 Mar 2021 16:29:47 +0800 Subject: [PATCH] add rand circles --- example/example_randcircle.go | 26 +++++++ julia.go | 6 +- randcircle.go | 130 ++++++++++++++++++++++++++++++++++ utils.go | 4 ++ 4 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 example/example_randcircle.go create mode 100644 randcircle.go diff --git a/example/example_randcircle.go b/example/example_randcircle.go new file mode 100644 index 0000000..847dcb0 --- /dev/null +++ b/example/example_randcircle.go @@ -0,0 +1,26 @@ +package main + +import ( + "generativeart" + "image/color" + "math/rand" + "time" +) + +func main() { + rand.Seed(time.Now().Unix()) + c := generativeart.NewCanva(500, 500, 2, 2) + c.SetBackground(generativeart.MistyRose) + c.SetLineWidth(1.0) + c.SetLineColor(color.RGBA{ + R: 122, + G: 122, + B: 122, + A: 30, + }) + c.SetColorSchema(generativeart.Plasma) + c.SetIterations(4) + c.FillBackground() + c.Draw(generativeart.NewRandCicle(30, 80, 0.2, 2, 10, 30, true)) + c.ToPNG("randcircle.png") +} diff --git a/julia.go b/julia.go index c80b6b9..433dafb 100644 --- a/julia.go +++ b/julia.go @@ -12,13 +12,13 @@ type julia struct { func NewJulia(formula GenFunc, maxz float64) *julia { return &julia{ - fn: formula, + fn: formula, maxz: maxz, } } // Generative draws a julia set. -func (j *julia)Generative(c *canva) { +func (j *julia) Generative(c *canva) { n := len(c.opts.colorSchema) if n > 255 { n = 255 @@ -37,4 +37,4 @@ func (j *julia)Generative(c *canva) { c.img.Set(i, k, c.opts.colorSchema[idx]) } } -} \ No newline at end of file +} diff --git a/randcircle.go b/randcircle.go new file mode 100644 index 0000000..2bf9351 --- /dev/null +++ b/randcircle.go @@ -0,0 +1,130 @@ +package generativeart + +import ( + "github.com/fogleman/gg" + "math" + "math/rand" +) + +type circle struct { + x, y float64 + radius float64 + dx, dy float64 +} + +type randCircle struct { + maxCircle int + maxStepsPerCircle int + minSteps float64 + maxSteps float64 + minRadius float64 + maxRadius float64 + isRandColor bool +} + +func NewRandCicle(mc, msp int, minStep, maxStep, minr, maxr float64, isRandColor bool) *randCircle { + return &randCircle{ + maxCircle: mc, + maxStepsPerCircle: msp, + minSteps: minStep, + maxSteps: maxStep, + minRadius: minr, + maxRadius: maxr, + isRandColor: isRandColor, + } +} + +func (r *randCircle) newCircleSlice(cn, w, h int) []circle { + var circles []circle + + for i := 0; i < cn; i++ { + x := rand.Intn(w) + 1 + y := rand.Intn(h) + 1 + radius := float64(rand.Intn(int(r.minRadius))) + r.maxRadius - r.minRadius + angle := rand.Float64() * math.Pi * 2.0 + step := r.minSteps + rand.Float64()*(r.maxSteps-r.minSteps) + circles = append(circles, circle{ + x: float64(x), + y: float64(y), + radius: radius, + dx: step * math.Cos(angle), + dy: step * math.Sin(angle), + }) + } + + return circles +} + +func (r *randCircle) circleSliceUpdate(cs []circle, w, h int) []circle { + var circles []circle + + for _, c := range cs { + c.x += c.dx + c.y += c.dy + + if c.x <= 0 { + c.x = 0 + c.dx *= -1 + } + + if c.y <= 0 { + c.y = 0 + c.dy *= -1 + } + + if c.x > float64(w) { + c.x = float64(w) + c.dx *= -1 + } + + if c.y > float64(h) { + c.y = float64(h) + c.dy *= -1 + } + + circles = append(circles, c) + } + + return circles +} + +// Generative draws a random circles image. +func (r *randCircle) Generative(c *canva) { + ctex := gg.NewContextForRGBA(c.img) + + + for j := 0; j