add random shapes and update the readme

This commit is contained in:
jdxyw 2021-03-06 15:32:29 +08:00
parent 8b619457bf
commit a6b4097f6a
6 changed files with 113 additions and 18 deletions

View file

@ -20,6 +20,7 @@
- Swirl - Swirl
- Point Ribbon - Point Ribbon
- Janus - Janus
- Random Shapes
For these kinds of art, the package provides as many as parameters to control the appearance. For these kinds of art, the package provides as many as parameters to control the appearance.
@ -45,6 +46,7 @@ NewSwirl(a, b, c, d, xaixs, yaixs float64)
NewDotLine(n int, ras, canv float64, randColor bool) NewDotLine(n int, ras, canv float64, randColor bool)
NewPointRibbon(r float64) NewPointRibbon(r float64)
NewJanus(n int, decay float64) NewJanus(n int, decay float64)
NewRandomShape(shapeNum int)
``` ```
## General Options ## General Options
@ -86,6 +88,28 @@ func main() {
![](images/silksmoke.png) ![](images/silksmoke.png)
### Random Shapes
```go
func main() {
rand.Seed(time.Now().Unix())
c := generativeart.NewCanva(500, 500)
c.SetBackground(generativeart.White)
c.FillBackground()
c.SetColorSchema([]color.RGBA{
{0xCF, 0x2B, 0x34, 0xFF},
{0xF0, 0x8F, 0x46, 0xFF},
{0xF0, 0xC1, 0x29, 0xFF},
{0x19, 0x6E, 0x94, 0xFF},
{0x35, 0x3A, 0x57, 0xFF},
})
c.Draw(generativeart.NewRandomShape(150))
c.ToPNG("randomshape.png")
}
```
![](images/randomshape.png)
### Spiral Square ### Spiral Square
```go ```go
@ -140,6 +164,8 @@ func main() {
} }
``` ```
![](images/dotline.png)
### Junas ### Junas
```go ```go
@ -157,7 +183,6 @@ func main() {
![](images/janus.png) ![](images/janus.png)
![](images/dotline.png)
### Julia Set ### Julia Set
@ -213,23 +238,6 @@ func main() {
![](images/pointribbon.png) ![](images/pointribbon.png)
### Circle Line
```go
func main() {
rand.Seed(time.Now().Unix())
c := generativeart.NewCanva(600, 600)
c.SetBackground(generativeart.Tan)
c.SetLineWidth(1.0)
c.SetLineColor(generativeart.LightPink)
c.FillBackground()
c.Draw(generativeart.NewCircleLine(0.02, 600, 1.5, 2, 2))
c.ToPNG("circleline.png")
}
```
![](images/circleline.png)
### Maze ### Maze
```go ```go
@ -291,3 +299,4 @@ Thanks for the following sites and repos, I got lots of ideas, inspiration, code
- http://paulbourke.net/fractals/peterdejong/ - http://paulbourke.net/fractals/peterdejong/
- https://editor.p5js.org/kenekk1/sketches/O44Dln5oo - https://editor.p5js.org/kenekk1/sketches/O44Dln5oo
- https://openprocessing.org/sketch/1071233 - https://openprocessing.org/sketch/1071233
- https://twitter.com/okazz_

View file

@ -0,0 +1,24 @@
package main
import (
"github.com/jdxyw/generativeart"
"image/color"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().Unix())
c := generativeart.NewCanva(500, 500)
c.SetBackground(generativeart.White)
c.FillBackground()
c.SetColorSchema([]color.RGBA{
{0xCF, 0x2B, 0x34, 0xFF},
{0xF0, 0x8F, 0x46, 0xFF},
{0xF0, 0xC1, 0x29, 0xFF},
{0x19, 0x6E, 0x94, 0xFF},
{0x35, 0x3A, 0x57, 0xFF},
})
c.Draw(generativeart.NewRandomShape(150))
c.ToPNG("randomshape.png")
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 268 KiB

BIN
images/randomshape.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

58
randomshape.go Normal file
View file

@ -0,0 +1,58 @@
package generativeart
import (
"github.com/fogleman/gg"
"math"
"math/rand"
)
type randomShape struct {
shapeNum int
}
func NewRandomShape(shapeNum int) *randomShape {
return &randomShape{
shapeNum: shapeNum,
}
}
// Generative draws a random shape image.
func (r *randomShape) Generative(c *canva) {
ctex := gg.NewContextForRGBA(c.img)
ctex.Translate(float64(c.width/2), float64(c.height/2))
ctex.Rotate(RandomRangeFloat64(-1, 1) * math.Pi * 0.25)
ctex.Translate(-float64(c.width/2), -float64(c.height/2))
for i := 0; i < r.shapeNum; i++ {
x := RandomGaussian(0.5, 0.2) * float64(c.width)
y := RandomGaussian(0.5, 0.2) * float64(c.height)
w := RandomRangeFloat64(0, float64(c.width)/3)*RandomRangeFloat64(0, rand.Float64()) + 5.0
h := w + RandomRangeFloat64(-1, 1)*3.0
rnd := rand.Intn(4)
theta := math.Pi * 2.0 * float64(rand.Intn(4)) / 4
ctex.Push()
ctex.Translate(x, y)
ctex.Rotate(theta)
ctex.SetColor(c.opts.colorSchema[rand.Intn(len(c.opts.colorSchema))])
switch rnd {
case 0:
ctex.DrawCircle(0, 0, w/2)
case 1:
ctex.DrawRectangle(0, 0, w/2, w/2)
case 2:
if rand.Float64() < 0.5 {
ctex.DrawEllipse(0, 0, w/2, h/2)
} else {
ctex.DrawRectangle(0, 0, w, h)
}
case 3:
ctex.DrawRectangle(0, 0, w*2, RandomRangeFloat64(2, 10))
}
ctex.Fill()
ctex.Pop()
}
}

View file

@ -122,3 +122,7 @@ func RandomRangeInt(min, max int) int {
func RandomRangeFloat64(min, max float64) float64 { func RandomRangeFloat64(min, max float64) float64 {
return min + rand.Float64()*(max-min) return min + rand.Float64()*(max-min)
} }
func RandomGaussian(mean, std float64) float64 {
return rand.NormFloat64()*std + mean
}