add the dot line

This commit is contained in:
Yongwei Xing 2021-03-04 12:06:45 +08:00
parent 4cf80677c7
commit ee719fc631
5 changed files with 107 additions and 1 deletions

View file

@ -4,7 +4,7 @@
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/jdxyw/generativeart/master/LICENSE)
generativeart is a `Go` package to generate many kinds of generative art. The code is collecting some excellent generative art (implemented in `R` or `Processing`), and implement them in `Go` again. Currently, it supports the following type.
`generativeart` is a `Go` package to generate many kinds of generative art. The goal is to collect some excellent generative art (implemented in `R` or `Processing`), and rewrite them in `Go` again. I would paste the original link at the end of this README(If I remember. You can also submit a PR if you found I miss something.). Currently, it supports the following type.
- Maze
- Julia Set
@ -15,6 +15,7 @@ generativeart is a `Go` package to generate many kinds of generative art. The co
- Circle Line
- Circle Loop
- Silk Sky
- Dot Line
For these kinds of art, the package provides as many as parameters to control the appearance.
@ -115,6 +116,24 @@ func main() {
![](images/circleloop.png)
### Dot Line
```go
func main() {
rand.Seed(time.Now().Unix())
c := generativeart.NewCanva(2080, 2080)
c.SetBackground(color.RGBA{230, 230, 230, 255})
c.SetLineWidth(10)
c.SetIterations(4000)
c.SetColorSchema(generativeart.Plasma)
c.FillBackground()
c.Draw(generativeart.NewDotLine(100, 20, 50, false))
c.ToPNG("dotline.png")
}
```
![](images/dotline.png)
### Julia Set
```go
@ -226,3 +245,4 @@ Thanks for the following sites and repos, I got lots of ideas, inspiration, code
- https://github.com/cdr6934/Generative-Processing-Experiments
- https://github.com/pkd2512/inktober2017
- http://blog.dragonlab.de/2015/03/generative-art-week-1
- https://editor.p5js.org/kenekk1/sketches/Ly-5XYvKX

61
dotLine.go Normal file
View file

@ -0,0 +1,61 @@
package generativeart
import (
"github.com/fogleman/gg"
"math/rand"
)
// TODO: figure out what the meaning of these parameters.
type dotLine struct {
n int
ras float64
canv float64
randColor bool
}
func NewDotLine(n int, ras, canv float64, randColor bool) *dotLine {
return &dotLine{
n: n,
ras: ras,
canv: canv,
randColor: randColor,
}
}
func (d *dotLine) Generative(c *canva) {
ctex := gg.NewContextForRGBA(c.img)
ctex.SetLineWidth(c.opts.lineWidth)
var dir []int = []int{-1, 1}
for i := 0; i < c.opts.nIters; i++ {
oldx := rand.Intn(d.n - 1)
oldy := rand.Intn(d.n - 1)
n := rand.Intn(7)
if d.randColor {
ctex.SetColor(c.opts.colorSchema[rand.Intn(len(c.opts.colorSchema))])
} else {
ctex.SetRGBA255(RandomRangeInt(222, 255), RandomRangeInt(20, 222), 0, 255)
}
for j := 0; j < n; j++ {
newx := oldx + dir[rand.Intn(2)]
newy := oldy + dir[rand.Intn(2)]
if Distance(float64(newx), float64(newy), float64(d.n/2), float64(d.n/2)) > float64(d.n/2-10) {
newx = oldx
newy = oldy
}
if newx == oldx && rand.Intn(3) > 1 {
ctex.DrawEllipse(float64(oldx)*d.ras+d.canv, float64(oldy)*d.ras+d.canv, c.opts.lineWidth, c.opts.lineWidth)
ctex.Fill()
continue
}
ctex.DrawLine(float64(oldx)*d.ras+d.canv, float64(oldy)*d.ras+d.canv, float64(newx)*d.ras+d.canv, float64(newy)*d.ras+d.canv)
oldx = newx
oldy = newy
ctex.Stroke()
}
}
}

View file

@ -0,0 +1,20 @@
package main
import (
"github.com/jdxyw/generativeart"
"image/color"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().Unix())
c := generativeart.NewCanva(2080, 2080)
c.SetBackground(color.RGBA{230, 230, 230, 255})
c.SetLineWidth(10)
c.SetIterations(4000)
c.SetColorSchema(generativeart.Plasma)
c.FillBackground()
c.Draw(generativeart.NewDotLine(100, 20, 50, false))
c.ToPNG("dotline.png")
}

BIN
images/dotline.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 760 KiB

View file

@ -3,6 +3,7 @@ package generativeart
import (
"image/color"
"math"
"math/rand"
)
type HSV struct {
@ -113,3 +114,7 @@ func ConvertPolarToPixel(r, theta, xaixs, yaixs float64, h, w int) (int, int) {
func Distance(x1, y1, x2, y2 float64) float64 {
return math.Sqrt(math.Pow(x1-x2, 2.0) + math.Pow(y1-y2, 2.0))
}
func RandomRangeInt(min, max int) int {
return rand.Intn(max-min) + min
}