generativeart/arts/squaregrid.go

61 lines
1.3 KiB
Go
Raw Normal View History

package arts
2021-03-02 03:23:29 +00:00
import (
"github.com/fogleman/gg"
"github.com/jdxyw/generativeart"
2021-03-02 03:23:29 +00:00
"math/rand"
)
2021-03-04 02:46:41 +00:00
type girdSquares struct {
step, rectSize int
decay float64
}
2021-03-02 03:23:29 +00:00
// NewGirdSquares returns a grid squares generator.
2021-03-04 02:46:41 +00:00
func NewGirdSquares(step, rectSize int, decay float64) *girdSquares {
return &girdSquares{
step: step,
rectSize: rectSize,
decay: decay,
}
2021-03-02 03:23:29 +00:00
}
// Generative draws a grid squares image.
func (g *girdSquares) Generative(c *generativeart.Canva) {
ctex := gg.NewContextForRGBA(c.Img())
2021-03-02 03:23:29 +00:00
for x := 0; x < c.Width(); x += g.step {
for y := 0; y < c.Height(); y += g.step {
cl := c.Opts().ColorSchema()[rand.Intn(len(c.Opts().ColorSchema()))]
2021-03-02 03:23:29 +00:00
x0 := float64(x)
y0 := float64(y)
2021-03-04 02:46:41 +00:00
s := float64(g.rectSize)
2021-03-02 03:23:29 +00:00
theta := rand.Intn(360) + 1
for i := 0; i < c.Opts().NIters(); i++ {
2021-03-02 03:23:29 +00:00
ctex.Push()
2021-03-04 02:46:41 +00:00
ctex.Translate(x0+float64(g.step/2), y0+float64(g.step/2))
2021-03-02 03:23:29 +00:00
ctex.Rotate(gg.Radians(float64(theta * i)))
ctex.Scale(s, s)
ctex.LineTo(-0.5, 0.5)
ctex.LineTo(0.5, 0.5)
ctex.LineTo(0.5, -0.5)
ctex.LineTo(-0.5, -0.5)
ctex.LineTo(-0.5, 0.5)
ctex.SetLineWidth(c.Opts().LineWidth())
ctex.SetColor(c.Opts().LineColor())
2021-03-02 03:23:29 +00:00
ctex.StrokePreserve()
ctex.SetRGBA255(int(cl.R), int(cl.G), int(cl.B), c.Opts().Alpha())
2021-03-02 03:23:29 +00:00
ctex.Fill()
ctex.Pop()
2021-03-04 02:46:41 +00:00
s = s - g.decay*float64(g.rectSize)
2021-03-02 03:23:29 +00:00
}
}
}
}