-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (149 loc) · 5.05 KB
/
main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"encoding/binary"
"fmt"
"image"
"image/color"
"math"
"math/bits"
"time"
spatial "git.sequentialread.com/forest/modular-spatial-index"
)
const dim = 512
const rainbowCount = float64(20)
const saturationFluctuationCount = float64(8)
var frames = 0
func main() {
spatialIndex, err := spatial.NewSpatialIndex2D(bits.UintSize)
if err != nil {
panic(err)
}
run_opengl_app(func() *image.RGBA {
seconds := float64(time.Now().UnixNano()) / float64(int64(time.Second))
rectX := int(float64(dim) * (float64(0.4) + math.Sin(seconds*float64(1.3))*float64(0.3)))
rectY := int(float64(dim) * (float64(0.5) + math.Cos(seconds*float64(0.3))*float64(0.2)))
rectSize := 1 + int(float64(25)*(float64(1)+math.Sin(seconds*float64(0.843))))
rectMaxX := rectX + rectSize
rectMaxY := rectY + rectSize
inputMin, inputMax := spatialIndex.GetValidInputRange()
_, outputMaxBytes := spatialIndex.GetOutputRange()
curveLength := int(binary.BigEndian.Uint64(outputMaxBytes))
//log.Printf("inputMin: %d, inputMax: %d, curveLength: %d", inputMin, inputMax, curveLength)
remappedRectXMin := int(lerp(float64(inputMin), float64(inputMax), float64(rectX)/float64(dim)))
remappedRectYMin := int(lerp(float64(inputMin), float64(inputMax), float64(rectY)/float64(dim)))
remappedRectXMax := int(lerp(float64(inputMin), float64(inputMax), float64(rectX+rectSize)/float64(dim)))
remappedRectSize := remappedRectXMax - remappedRectXMin
byteRanges, err := spatialIndex.RectangleToIndexedRanges(remappedRectXMin, remappedRectYMin, remappedRectSize, remappedRectSize, 1)
if err != nil {
panic(err)
}
ranges := make([][]int, len(byteRanges))
// log.Println("------------")
for i, byteRange := range byteRanges {
ranges[i] = []int{
int(binary.BigEndian.Uint64(byteRange.Start)),
int(binary.BigEndian.Uint64(byteRange.End)),
}
// log.Printf("Start: %x\n", byteRange.Start)
// log.Printf(" End: %x\n", byteRange.End)
// log.Printf(" Max: %x\n", outputMaxBytes)
}
// log.Println("------------")
// outBytes, _ := json.MarshalIndent(ranges, "", " ")
// log.Println("outBytes: ", string(outBytes))
rgba := image.NewRGBA(image.Rectangle{Min: image.Point{0, 0}, Max: image.Point{dim, dim}})
queriedArea := 0
for x := 0; x < dim; x++ {
for y := 0; y < dim; y++ {
onVertical := (x == rectMaxX || x == rectX) && y >= rectY && y <= rectMaxY
onHorizontal := (y == rectMaxY || y == rectY) && x >= rectX && x <= rectMaxX
if onVertical || onHorizontal {
rgba.Set(x, y, color.White)
continue
}
remappedX := int(lerp(float64(inputMin), float64(inputMax), float64(x)/float64(dim)))
remappedY := int(lerp(float64(inputMin), float64(inputMax), float64(y)/float64(dim)))
if y > dim-20 {
found := false
xOnCurveNumberLine := int(lerp(float64(0), float64(curveLength), float64(x)/float64(dim)))
for _, curveRange := range ranges {
if xOnCurveNumberLine >= curveRange[0] && xOnCurveNumberLine <= curveRange[1] {
found = true
}
}
if found {
rgba.Set(x, y, color.White)
} else {
rgba.Set(x, y, color.Black)
}
continue
}
curvePointBytes, err := spatialIndex.GetIndexedPoint(remappedX, remappedY)
curvePoint := int(binary.BigEndian.Uint64(curvePointBytes))
if err != nil {
panic(err)
}
// if x*2 == y && !logged {
// log.Printf("[%d,%d]: %d %d", x, y, curvePoint, int((float64(curvePoint)/float64(myCurve.N*myCurve.N))*1000))
// }
curveFloat := (float64(curvePoint) / float64(math.MaxInt64))
//sat := (float64(2) + math.Sin(curveFloat*math.Pi*2*saturationFluctuationCount)) * float64(0.3333333)
sat := 0.2
// if curvePoint >= curvePoints[0] && curvePoint <= curvePoints[len(curvePoints)-1] {
// sat = 1
// }
for _, rng := range ranges {
if curvePoint >= rng[0] && curvePoint <= rng[1] {
sat = 1
queriedArea++
}
}
hue := int(curveFloat*rainbowCount*float64(3600)) % 3600
rainbow := hsvColor(float64(hue)*0.1, sat, sat)
// uvColor := color.RGBA{
// uint8((float32(x) / float32(width)) * float32(255)),
// uint8((float32(y) / float32(width)) * float32(255)),
// 255,
// 255,
// }
rgba.Set(x, y, rainbow)
}
}
if frames%10 == 0 {
fmt.Printf("range count: %d, queriedArea: %d%%\n", len(ranges), int((float64(queriedArea)/float64(rectSize*rectSize))*float64(100)))
}
frames++
return rgba
})
}
func lerp(a, b, lerp float64) float64 {
return a*(float64(1)-lerp) + b*lerp
}
func hsvColor(H, S, V float64) color.RGBA {
Hp := H / 60.0
C := V * S
X := C * (1.0 - math.Abs(math.Mod(Hp, 2.0)-1.0))
m := V - C
r, g, b := 0.0, 0.0, 0.0
switch {
case 0.0 <= Hp && Hp < 1.0:
r = C
g = X
case 1.0 <= Hp && Hp < 2.0:
r = X
g = C
case 2.0 <= Hp && Hp < 3.0:
g = C
b = X
case 3.0 <= Hp && Hp < 4.0:
g = X
b = C
case 4.0 <= Hp && Hp < 5.0:
r = X
b = C
case 5.0 <= Hp && Hp < 6.0:
r = C
b = X
}
return color.RGBA{uint8(int((m + r) * float64(255))), uint8(int((m + g) * float64(255))), uint8(int((m + b) * float64(255))), 0xff}
}