/
swirl.go
99 lines (83 loc) · 1.88 KB
/
swirl.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package arts
import (
"github.com/fogleman/gg"
"github.com/jdxyw/generativeart"
"github.com/jdxyw/generativeart/common"
"math"
)
type swirl struct {
// These are some math parameters.
// http://paulbourke.net/fractals/peterdejong/
a, b, c, d, xaixs, yaixs float64
}
func NewSwirl(a, b, c, d, xaixs, yaixs float64) *swirl {
return &swirl{
a: a,
b: b,
c: c,
d: d,
xaixs: xaixs,
yaixs: yaixs,
}
}
// Generative draws a swirl image.
func (s *swirl) Generative(c *generativeart.Canva) {
ctex := gg.NewContextForRGBA(c.Img())
ctex.SetLineWidth(c.Opts().LineWidth())
start := point{
x: 1.0,
y: 1.0,
}
cl := c.Opts().Foreground()
for i := 0; i < c.Opts().NIters(); i++ {
next := s.swirlTransform(start)
x, y := common.ConvertCartesianToPixel(next.x, next.y, s.xaixs, s.yaixs, c.Height(), c.Width())
c.Img().Set(x, y, cl)
start = next
}
s.removeNoisy(c)
s.removeNoisy(c)
}
func (s *swirl) swirlTransform(p point) point {
return point{
x: math.Sin(s.a*p.y) - math.Cos(s.b*p.x),
y: math.Sin(s.c*p.x) - math.Cos(s.d*p.y),
}
}
func (s *swirl) removeNoisy(c *generativeart.Canva) {
for i := 1; i < c.Width()-1; i++ {
for j := 1; j < c.Height()-1; j++ {
if c.Img().At(i, j) == c.Opts().Background() {
continue
}
var n int
if c.Img().At(i+1, j) == c.Opts().Background() {
n += 1
}
if c.Img().At(i+1, j+1) == c.Opts().Background() {
n += 1
}
if c.Img().At(i, j+1) == c.Opts().Background() {
n += 1
}
if c.Img().At(i-1, j) == c.Opts().Background() {
n += 1
}
if c.Img().At(i-1, j+1) == c.Opts().Background() {
n += 1
}
if c.Img().At(i-1, j-1) == c.Opts().Background() {
n += 1
}
if c.Img().At(i+1, j-1) == c.Opts().Background() {
n += 1
}
if c.Img().At(i, j-1) == c.Opts().Background() {
n += 1
}
if n > 5 {
c.Img().Set(i, j, c.Opts().Background())
}
}
}
}