forked from jdxyw/generativeart
/
contourline.go
50 lines (40 loc) · 1.14 KB
/
contourline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package arts
import (
"math"
"math/rand"
"github.com/andrewwatson/generativeart"
"github.com/andrewwatson/generativeart/common"
"github.com/fogleman/gg"
)
type contourLine struct {
lineNum int
}
// NewContourLine returns a contourLine object.
func NewContourLine(lineNum int) *contourLine {
return &contourLine{
lineNum: lineNum,
}
}
// Generative draws a contour line image.
func (cl *contourLine) Generative(c *generativeart.Canva) string {
ctex := gg.NewContextForRGBA(c.Img())
noise := common.NewPerlinNoise()
for i := 0; i < cl.lineNum; i++ {
cls := c.Opts().ColorSchema()[rand.Intn(len(c.Opts().ColorSchema()))]
x := rand.Float64() * float64(c.Width())
y := rand.Float64() * float64(c.Height())
for j := 0; j < 1500; j++ {
theta := noise.Noise2D(x/800.0, y/800.0) * math.Pi * 2 * 800
x += math.Cos(theta) * 0.4
y += math.Sin(theta) * 0.4
ctex.SetColor(cls)
ctex.DrawEllipse(x, y, 2, 2)
ctex.Fill()
if x > float64(c.Width()) || x < 0 || y > float64(c.Height()) || y < 0 || rand.Float64() < 0.001 {
x = rand.Float64() * float64(c.Width())
y = rand.Float64() * float64(c.Height())
}
}
}
return ""
}