add maze generation and some other files

This commit is contained in:
Yongwei Xing 2021-03-01 14:29:10 +08:00
parent e6decd968c
commit a34b353533
6 changed files with 71 additions and 4 deletions

View file

@ -11,9 +11,9 @@ func formula1(x, y float64) (float64, float64) {
}
func main() {
c := generativeart.NewCanva(1600, 1600, 4, 4)
c.FillBackgroud(generativeart.Tomato)
c := generativeart.NewCanva(1600, 1600, 2, 2)
c.FillBackgroud(generativeart.White)
g := generativeart.NewWave(formula1)
g.GenerativePolar(c, generativeart.Black)
g.GenerativePolar(c, generativeart.DarkSalmon)
c.ToPng("g1.png")
}

11
example/example_maze.go Normal file
View file

@ -0,0 +1,11 @@
package main
import "generativeart"
func main() {
c := generativeart.NewCanva(800, 800, 2,2)
c.FillBackgroud(generativeart.Azure)
g := generativeart.NewMaze()
g.Generative(c, generativeart.Tomato)
c.ToPng("maze.png")
}

6
go.mod
View file

@ -1,3 +1,9 @@
module generativeart
go 1.14
require (
github.com/fogleman/gg v1.3.0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb // indirect
)

7
go.sum Normal file
View file

@ -0,0 +1,7 @@
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb h1:fqpd0EBDzlHRCjiphRR5Zo/RSWWQlWv34418dnEixWk=
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

37
maze.go Normal file
View file

@ -0,0 +1,37 @@
package generativeart
import (
"image/color"
"math/rand"
"github.com/fogleman/gg"
)
type maze struct{}
// NewMaze returns a maze generator.
func NewMaze() *maze {
return &maze{}
}
// Generative draws a random maze image.
func (m *maze) Generative(c *canva, rgba color.RGBA) {
ctex := gg.NewContextForRGBA(c.img)
ctex.SetColor(rgba)
ctex.SetLineWidth(3.0)
step := 20
for x := 0; x < c.width; x += step {
for y := 0; y < c.height; y += step {
if rand.Float32() > 0.5 {
ctex.DrawLine(float64(x), float64(y), float64(x+step), float64(y+step))
//ctex.Stroke()
} else {
ctex.DrawLine(float64(x+step), float64(y), float64(x), float64(y+step))
ctex.SetLineWidth(3.0)
//ctex.Stroke()
}
ctex.Stroke()
}
}
}

View file

@ -3,6 +3,8 @@ package generativeart
import (
"image/color"
"math"
"github.com/fogleman/gg"
)
type Formula func(x, y float64) (float64, float64)
@ -18,6 +20,7 @@ func NewWave(fn Formula) *wave {
// GenerativePolar draws the image in polar coordinate system.
func (w *wave) GenerativePolar(c *canva, rgba color.RGBA) {
ctex := gg.NewContextForRGBA(c.img)
for x := -math.Pi; x <= math.Pi; x += 0.01 {
for y := -math.Pi; y <= math.Pi; y += 0.01 {
xi, yi := w.fn(x, y)
@ -25,7 +28,10 @@ func (w *wave) GenerativePolar(c *canva, rgba color.RGBA) {
if i < 0 || i > c.width-1 || j < 0 || j > c.height-1 {
continue
}
c.img.Set(i, j, rgba)
ctex.DrawCircle(float64(i), float64(j), 1.0)
ctex.SetColor(rgba)
ctex.Fill()
//c.img.Set(i, j, rgba)
}
}
}