diff --git a/README.md b/README.md index 4dfe148..6f9584b 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,37 @@ func main() { ![](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 ```go @@ -195,20 +226,6 @@ func main() { ![](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 ```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 - https://editor.p5js.org/kenekk1/sketches/Ly-5XYvKX - http://paulbourke.net/fractals/peterdejong/ +- https://editor.p5js.org/kenekk1/sketches/O44Dln5oo diff --git a/color.go b/color.go index a0db994..082057a 100644 --- a/color.go +++ b/color.go @@ -22,6 +22,7 @@ var ( Orange = color.RGBA{0xFF, 0xA5, 0x00, 0xFF} Black = color.RGBA{0x00, 0x00, 0x00, 0xFF} White = color.RGBA{0xFF, 0xFF, 0xFF, 0xFF} + LightGray = color.RGBA{200, 200, 200, 255} Outdoors = []color.RGBA{ {67, 110, 165, 255}, {47, 76, 114, 255}, diff --git a/example/example_pointribbon.go b/example/example_pointribbon.go new file mode 100644 index 0000000..3964ec6 --- /dev/null +++ b/example/example_pointribbon.go @@ -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") +} diff --git a/images/pointribbon.png b/images/pointribbon.png new file mode 100644 index 0000000..eab4013 Binary files /dev/null and b/images/pointribbon.png differ diff --git a/pointRibbon.go b/pointRibbon.go new file mode 100644 index 0000000..116ba60 --- /dev/null +++ b/pointRibbon.go @@ -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 + } +} diff --git a/utils_test.go b/utils_test.go index d8018f6..395e106 100644 --- a/utils_test.go +++ b/utils_test.go @@ -17,13 +17,13 @@ func TestDistance(t *testing.T) { args args want float64 }{ - {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: "testcase3", args: args{x1:1, y1:1, x2:0, y2:0}, want: 1.414213562}, + {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: "testcase3", args: args{x1: 1, y1: 1, x2: 0, y2: 0}, want: 1.414213562}, } for _, tt := range tests { 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) } })