add janus and update readme

This commit is contained in:
Yongwei Xing 2021-03-05 18:08:15 +08:00
parent 1a8c8fb011
commit 8b619457bf
9 changed files with 161 additions and 18 deletions

View file

@ -19,6 +19,7 @@
- Dot Line - Dot Line
- Swirl - Swirl
- Point Ribbon - Point Ribbon
- Janus
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.
@ -43,6 +44,7 @@ NewSpiralSquare(squareNum int, rectSide, decay float64, randColor bool)
NewSwirl(a, b, c, d, xaixs, yaixs float64) 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)
``` ```
## General Options ## General Options
@ -138,6 +140,23 @@ func main() {
} }
``` ```
### Junas
```go
func main() {
rand.Seed(time.Now().Unix())
c := generativeart.NewCanva(500, 500)
c.SetBackground(generativeart.Black)
c.FillBackground()
c.SetColorSchema(generativeart.DarkRed)
c.SetForeground(generativeart.LightPink)
c.Draw(generativeart.NewJanus(10, 0.2))
c.ToPNG("janus.png")
}
```
![](images/janus.png)
![](images/dotline.png) ![](images/dotline.png)
### Julia Set ### Julia Set
@ -194,23 +213,6 @@ func main() {
![](images/pointribbon.png) ![](images/pointribbon.png)
### Swirl
```go
func main() {
rand.Seed(time.Now().Unix())
c := generativeart.NewCanva(1600, 1600)
c.SetBackground(generativeart.Azure)
c.FillBackground()
c.SetForeground(color.RGBA{113, 3, 0, 180})
c.SetIterations(8000000)
c.Draw(generativeart.NewSwirl(0.970, -1.899, -1.381, -1.506, 2.0, 2.0))
c.ToPNG("swirl.png")
}
```
![](images/swirl.png)
### Circle Line ### Circle Line
```go ```go
@ -288,3 +290,4 @@ Thanks for the following sites and repos, I got lots of ideas, inspiration, code
- https://editor.p5js.org/kenekk1/sketches/Ly-5XYvKX - https://editor.p5js.org/kenekk1/sketches/Ly-5XYvKX
- 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

View file

@ -45,6 +45,19 @@ var (
{13, 13, 13, 255}, {13, 13, 13, 255},
} }
DarkRed = []color.RGBA{
{48, 19, 21, 255},
{71, 15, 16, 255},
{92, 10, 12, 255},
{115, 5, 4, 255},
{139, 0, 3, 255},
{163, 32, 1, 255},
{185, 66, 0, 255},
{208, 98, 1, 255},
{231, 133, 0, 255},
{254, 165, 0, 255},
}
Plasma = []color.RGBA{ Plasma = []color.RGBA{
color.RGBA{0x0c, 0x07, 0x86, 0xff}, color.RGBA{0x0c, 0x07, 0x86, 0xff},
color.RGBA{0x10, 0x07, 0x87, 0xff}, color.RGBA{0x10, 0x07, 0x87, 0xff},

18
example/example_janus.go Normal file
View file

@ -0,0 +1,18 @@
package main
import (
"github.com/jdxyw/generativeart"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().Unix())
c := generativeart.NewCanva(500, 500)
c.SetBackground(generativeart.Black)
c.FillBackground()
c.SetColorSchema(generativeart.DarkRed)
c.SetForeground(generativeart.LightPink)
c.Draw(generativeart.NewJanus(10, 0.2))
c.ToPNG("janus.png")
}

41
example/test.go Normal file
View file

@ -0,0 +1,41 @@
package main
import (
"github.com/fogleman/gg"
"github.com/jdxyw/generativeart"
"github.com/llgcode/draw2d/draw2dimg"
"image"
"math/rand"
"time"
)
func main() {
const S = 400
rand.Seed(time.Now().Unix())
dest := image.NewRGBA(image.Rect(0, 0, 500, 500))
gc := draw2dimg.NewGraphicContext(dest)
gc.SetStrokeColor(generativeart.Orange)
gc.BeginPath()
gc.MoveTo(100, 100)
gc.QuadCurveTo(150, 50, 200, 100)
//fmt.Println(gc.LastPoint())
//gc.QuadCurveTo(200, 100, 300, 200)
gc.QuadCurveTo(250, 250, 200, 400)
gc.QuadCurveTo(100, 250, 100, 100)
//gc.QuadCurveTo(200, 400, 100, 10)
//gc.Close()
gc.Stroke()
ctex := gg.NewContextForRGBA(dest)
ctex.SetColor(generativeart.White)
//ctex.DrawCircle(100, 100, 5)
//ctex.DrawCircle(200, 400, 5)
//ctex.DrawCircle(200, 100, 5)
//ctex.DrawCircle(300, 200, 5)
//ctex.DrawCircle(200, 400, 5)
ctex.Fill()
//gc.FillStroke()
draw2dimg.SaveToPngFile("test.png", dest)
}

2
go.mod
View file

@ -4,6 +4,6 @@ go 1.16
require ( require (
github.com/fogleman/gg v1.3.0 github.com/fogleman/gg v1.3.0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect github.com/llgcode/draw2d v0.0.0-20200930101115-bfaf5d914d1e
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb // indirect golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb // indirect
) )

8
go.sum
View file

@ -1,7 +1,15 @@
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8= 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/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/go-gl/gl v0.0.0-20180407155706-68e253793080/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk=
github.com/go-gl/glfw v0.0.0-20180426074136-46a8d530c326/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= 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= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/llgcode/draw2d v0.0.0-20200930101115-bfaf5d914d1e h1:YRRazju3DMGuZTSWEj0nE2SCRcK3DW/qdHQ4UQx7sgs=
github.com/llgcode/draw2d v0.0.0-20200930101115-bfaf5d914d1e/go.mod h1:mVa0dA29Db2S4LVqDYLlsePDzRJLDfdhVZiI15uY0FA=
github.com/llgcode/ps v0.0.0-20150911083025-f1443b32eedb h1:61ndUreYSlWFeCY44JxDDkngVoI7/1MVhEl98Nm0KOk=
github.com/llgcode/ps v0.0.0-20150911083025-f1443b32eedb/go.mod h1:1l8ky+Ew27CMX29uG+a2hNOKpeNYEQjjtiALiBlFQbY=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb h1:fqpd0EBDzlHRCjiphRR5Zo/RSWWQlWv34418dnEixWk= 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/image v0.0.0-20210220032944-ac19c3e999fb/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

BIN
images/janus.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

56
janus.go Normal file
View file

@ -0,0 +1,56 @@
package generativeart
import (
"github.com/fogleman/gg"
"math"
)
type janus struct {
n int
decay float64
}
// NewJanus returns a janus object
func NewJanus(n int, decay float64) *janus {
return &janus{
n: n,
decay: decay,
}
}
// Generative draws a janus image
// TODO not finished.
func (j *janus) Generative(c *canva) {
ctex := gg.NewContextForRGBA(c.img)
//ctex.SetColor(c.opts.foreground)
s := 220.0
r := 0.3
for i := 0; i < j.n; i++ {
//k := rand.Intn(len(c.opts.colorSchema))
k := i
ctex.Push()
ctex.Translate(float64(c.width/2), float64(c.height/2))
//theta += rand.Float64()*math.Pi/2
theta := RandomRangeFloat64(math.Pi/4, 3*math.Pi/4)
x1, y1 := math.Cos(theta)*r, math.Sin(theta)*r
x2, y2 := -x1, -y1
noise := RandomRangeFloat64(-math.Abs(y1), math.Abs(y1))
y1 += noise
y2 += noise
//r = r - r*j.decay
s = s * 0.836
ctex.Scale(s, s)
//r = r * 0.836
ctex.DrawArc(x1, y1, 1.0, math.Pi*3/2+theta, math.Pi*5/2+theta)
ctex.SetColor(c.opts.colorSchema[k])
ctex.Fill()
ctex.DrawArc(x2, y2, 1.0, math.Pi/2+theta, math.Pi*3/2+theta)
ctex.SetColor(c.opts.colorSchema[k])
ctex.Fill()
ctex.Pop()
}
}

View file

@ -118,3 +118,7 @@ func Distance(x1, y1, x2, y2 float64) float64 {
func RandomRangeInt(min, max int) int { func RandomRangeInt(min, max int) int {
return rand.Intn(max-min) + min return rand.Intn(max-min) + min
} }
func RandomRangeFloat64(min, max float64) float64 {
return min + rand.Float64()*(max-min)
}