add point ribbon

This commit is contained in:
Yongwei Xing 2021-03-04 19:54:21 +08:00
parent 802f3c5a17
commit f529917c67
6 changed files with 91 additions and 18 deletions

View file

@ -161,6 +161,37 @@ func main() {
![](images/julia.png) ![](images/julia.png)
### Silk Sky
```go
func main() {
rand.Seed(time.Now().Unix())
c := generativeart.NewCanva(600, 600)
c.SetAlpha(10)
c.Draw(generativeart.NewSilkSky(15, 5))
c.ToPNG("silksky.png")
}
```
![](images/silksky.png)
### Point Ribbon
```go
func main() {
rand.Seed(time.Now().Unix())
c := generativeart.NewCanva(500, 500)
c.SetBackground(generativeart.Lavender)
c.SetLineWidth(2)
c.SetIterations(150000)
c.FillBackground()
c.Draw(generativeart.NewPointRibbon(50))
c.ToPNG("pointribbon.png")
}
```
![](images/pointribbon.png)
### Swirl ### Swirl
```go ```go
@ -195,20 +226,6 @@ func main() {
![](images/circleline.png) ![](images/circleline.png)
### Silk Sky
```go
func main() {
rand.Seed(time.Now().Unix())
c := generativeart.NewCanva(600, 600)
c.SetAlpha(10)
c.Draw(generativeart.NewSilkSky(15, 5))
c.ToPNG("silksky.png")
}
```
![](images/silksky.png)
### Maze ### Maze
```go ```go
@ -268,3 +285,4 @@ Thanks for the following sites and repos, I got lots of ideas, inspiration, code
- http://blog.dragonlab.de/2015/03/generative-art-week-1 - http://blog.dragonlab.de/2015/03/generative-art-week-1
- 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

View file

@ -22,6 +22,7 @@ var (
Orange = color.RGBA{0xFF, 0xA5, 0x00, 0xFF} Orange = color.RGBA{0xFF, 0xA5, 0x00, 0xFF}
Black = color.RGBA{0x00, 0x00, 0x00, 0xFF} Black = color.RGBA{0x00, 0x00, 0x00, 0xFF}
White = color.RGBA{0xFF, 0xFF, 0xFF, 0xFF} White = color.RGBA{0xFF, 0xFF, 0xFF, 0xFF}
LightGray = color.RGBA{200, 200, 200, 255}
Outdoors = []color.RGBA{ Outdoors = []color.RGBA{
{67, 110, 165, 255}, {67, 110, 165, 255},
{47, 76, 114, 255}, {47, 76, 114, 255},

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.Lavender)
c.SetLineWidth(2)
c.SetIterations(150000)
c.FillBackground()
c.Draw(generativeart.NewPointRibbon(50))
c.ToPNG("pointribbon.png")
}

BIN
images/pointribbon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 KiB

36
pointRibbon.go Normal file
View file

@ -0,0 +1,36 @@
package generativeart
import (
"github.com/fogleman/gg"
"math"
)
type pointRibbon struct {
r float64
}
// NewPointRibbon returns a pointRibbon object.
func NewPointRibbon(r float64) *pointRibbon {
return &pointRibbon{
r: r,
}
}
// Generative draws a point ribbon image.
// TODO: make the point as parameters.
func (s *pointRibbon) Generative(c *canva) {
ctex := gg.NewContextForRGBA(c.img)
ctex.SetLineWidth(c.opts.lineWidth)
var t float64
var dt = 0.0001
for i := 0; i < c.opts.nIters; i++ {
delta := 2.0*s.r*math.Cos(4.0*dt*t) + s.r*math.Cos(t)
ctex.SetRGBA255(int(delta), int(2*s.r*math.Sin(t)-s.r*math.Cos(3*dt*t)), 100, 10)
ctex.DrawPoint(2*s.r*math.Sin(2*t*dt)+s.r*math.Cos(t*dt)+float64(c.width/2),
2*s.r*math.Sin(t*dt)-s.r*math.Sin(5*t)+float64(c.height/2), 1.0)
ctex.Stroke()
t += 0.01
dt += 0.1
}
}

View file

@ -17,13 +17,13 @@ func TestDistance(t *testing.T) {
args args args args
want float64 want float64
}{ }{
{name: "testcase1", args: args{x1:0, y1:0, x2:0, y2:0}, want: 0}, {name: "testcase1", args: args{x1: 0, y1: 0, x2: 0, y2: 0}, want: 0},
{name: "testcase2", args: args{x1:0, y1:3, x2:4, y2:0}, want: 5}, {name: "testcase2", args: args{x1: 0, y1: 3, x2: 4, y2: 0}, want: 5},
{name: "testcase3", args: args{x1:1, y1:1, x2:0, y2:0}, want: 1.414213562}, {name: "testcase3", args: args{x1: 1, y1: 1, x2: 0, y2: 0}, want: 1.414213562},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if got := Distance(tt.args.x1, tt.args.y1, tt.args.x2, tt.args.y2); math.Abs(got-tt.want)>0.00001 { if got := Distance(tt.args.x1, tt.args.y1, tt.args.x2, tt.args.y2); math.Abs(got-tt.want) > 0.00001 {
t.Errorf("Distance() = %v, want %v", got, tt.want) t.Errorf("Distance() = %v, want %v", got, tt.want)
} }
}) })