-
Notifications
You must be signed in to change notification settings - Fork 13
/
mosaic_sun.go
160 lines (134 loc) · 2.82 KB
/
mosaic_sun.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"github.com/buchanae/ink/color"
. "github.com/buchanae/ink/dd"
"github.com/buchanae/ink/gfx"
"github.com/buchanae/ink/math"
"github.com/buchanae/ink/rand"
)
const (
Gap = 0.001
Space = 0.0045
Count = 25
Start = 0.01
Width = (0.5 - Start) / Count
MinChord = 0.005
MaxChord = 0.008
JumpChance = 0.2
LightenChance = 0.4
LightenAmt = 0.2
TweakChance = 0.3
TweakAmt = -0.001
)
func Ink(doc gfx.Doc) {
rand.SeedNow()
A := color.HexString("#ebc334")
B := color.HexString("#0c79e8")
gfx.Fill{
Shape: Circle{
XY: XY{.5, .5},
Radius: Start - Gap,
Segments: 5,
},
Color: A,
}.Draw(doc)
for i := float32(0); i < Count; i++ {
rings := Rings{
Offset: rand.Range(0, 3),
Inner: Start + i*Width,
Outer: Start + (i+1)*Width - Space,
Gap: Gap,
}
var min float32 = MinChord
var max float32 = MaxChord
min += i * 0.001
max += i * 0.003
max = math.Min(max, 0.05)
chords := GenChords(rings.Inner, min, max)
// TODO interpcolor isn't based on visual interpolation
// going form orange to blue goes through green
col := color.Interpolate(A, B, i/Count)
for _, in := range chords {
rx := rings
rx.From = in.From
rx.To = in.To
rx.Gap += rand.Range(0, 0.002)
rx.Color = col
if rand.Bool(JumpChance) {
rx.Color = color.Interpolate(A, B,
rand.Range(0, Count)/Count,
)
}
if rand.Bool(LightenChance) {
amt := rand.Range(-LightenAmt, LightenAmt)
rx.Color = rx.Color.Lighten(amt)
}
rx.Draw(doc)
}
}
}
type Rings struct {
Inner, Outer float32
From, To float32
Offset float32
Gap float32
Color color.RGBA
}
func (r Rings) Draw(doc gfx.Layer) {
center := XY{.5, .5}
inner := Circle{
XY: center,
Radius: r.Inner,
}
outer := Circle{
XY: center,
Radius: r.Outer,
}
from := r.Offset + r.From
to := r.Offset + r.To
innerGap := ChordAngle(r.Inner, r.Gap)
outerGap := ChordAngle(r.Outer, r.Gap)
quad := Quad{
inner.XYFromAngle(from + innerGap),
inner.XYFromAngle(to - innerGap),
outer.XYFromAngle(to - outerGap),
outer.XYFromAngle(from + outerGap),
}
if rand.Bool(TweakChance) {
quad = rand.TweakQuad(quad, TweakAmt)
}
fill := gfx.Fill{quad, r.Color}
fill.Draw(doc)
}
type Chord struct {
From, To float32
}
func GenChords(radius, min, max float32) []Chord {
var out []Chord
p := float32(0)
for {
length := rand.Range(min, max)
// Protect against NaN
// from Asin in ChordAngle
if length >= radius {
length = radius
}
ang := ChordAngle(radius, length)
next := p + ang
if next >= math.Pi*2 {
out = append(out, Chord{
From: p,
})
break
}
out = append(out, Chord{
From: p,
To: next,
})
p = next
}
return out
}
func ChordAngle(radius, length float32) float32 {
return 2 * math.Asin(length/(2*radius))
}