-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
332 lines (289 loc) · 10.5 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"fmt"
"image/color"
"log"
"math"
"github.com/braheezy/hobby-spline/pkg/bezier"
"golang.org/x/image/font"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
)
func main() {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Hobby's algorithm for aesthetic Bézier splines")
game := &Game{
points: []bezier.Point{
{X: 356, Y: 229},
{X: 523, Y: 287},
{X: 505, Y: 72},
{X: 109, Y: 224},
{X: 108, Y: 92},
{X: 232, Y: 307},
},
omega: 0.75,
showComb: true,
showNatural: true,
}
if err := ebiten.RunGame(game); err != nil {
log.Fatal(err)
}
}
type Game struct {
points []bezier.Point
splinePoints []bezier.Point
omega float64
showComb bool
showNatural bool
sliderDragging bool
draggingPoint *bezier.Point
dragOffsetX float32
dragOffsetY float32
}
func (g *Game) Update() error {
updateOmega := func(x int) {
g.omega = float64(x-sliderX) / float64(sliderWidth)
if g.omega < 0 {
g.omega = 0
} else if g.omega > 1 {
g.omega = 1
}
}
// Collect current touch states
touchIDs := ebiten.AppendTouchIDs(nil)
var x, y int
var inputJustPressed bool
var inputJustReleased bool
if len(touchIDs) > 0 {
// Use touch input
x, y = ebiten.TouchPosition(touchIDs[0])
inputJustPressed = len(inpututil.AppendJustPressedTouchIDs(touchIDs)) > 0
inputJustReleased = inpututil.IsTouchJustReleased(touchIDs[0])
} else {
// Use mouse input
x, y = ebiten.CursorPosition()
inputJustPressed = inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft)
inputJustReleased = inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft)
}
// Check mouse interactions with slider knob
if g.sliderDragging {
if inputJustReleased {
g.sliderDragging = false
} else {
updateOmega(x)
}
} else if inputJustPressed {
knobX := sliderX + int(g.omega*float64(sliderWidth))
// Check if mouse is within slider knob
if x >= knobX-sliderKnobDiameter/2 && x <= knobX+sliderKnobDiameter/2 && y >= sliderY && y <= sliderY+sliderHeight {
g.sliderDragging = true
} else if x >= combToggleX-toggleRadius && x <= combToggleX+toggleRadius && y >= combToggleY-toggleRadius && y <= combToggleY-toggleRadius+toggleDiameter {
// Check if mouse is within comb toggle
g.showComb = !g.showComb
}
// Check if mouse is within natural toggle
if x >= naturalToggleX-toggleRadius && x <= naturalToggleX+toggleRadius && y >= naturalToggleY-toggleRadius && y <= naturalToggleY-toggleRadius+toggleDiameter {
g.showNatural = !g.showNatural
}
}
// Handle point dragging logic for bezier curves
if g.draggingPoint != nil {
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
g.draggingPoint = nil
} else {
x, y := ebiten.CursorPosition()
g.draggingPoint.X = float64(x) - float64(g.dragOffsetX)
g.draggingPoint.Y = float64(y) - float64(g.dragOffsetY)
}
} else if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
x, y := ebiten.CursorPosition()
for i := range g.points {
px, py := g.points[i].X, g.points[i].Y
if (x-int(px))*(x-int(px))+(y-int(py))*(y-int(py)) <= 25*25 { // 25 pixels radius for easier clicking
g.draggingPoint = &g.points[i]
g.dragOffsetX = float32(x) - float32(px)
g.dragOffsetY = float32(y) - float32(py)
break
}
}
}
g.splinePoints, _ = bezier.CreateHobbySpline(g.points, g.omega)
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
// Set background
screen.Fill(backgroundColor)
strokeOp := &vector.StrokeOptions{Width: 1}
if g.showNatural {
// Calculate natural spline
naturalPoints, _ := bezier.NaturalCubicSpline(g.points)
if len(naturalPoints) != 0 {
for i := 0; i <= (len(naturalPoints)-2)/3; i++ {
pts := naturalPoints[i*3 : i*3+4]
strokeCurve(screen, &bezier.Bezier{Points: pts}, strokeOp, naturalCurveColor)
}
}
}
// Draw bezier curves
strokeOp = &vector.StrokeOptions{Width: 5}
if len(g.splinePoints) != 0 {
for i := 0; i <= (len(g.splinePoints)-2)/3; i++ {
pts := g.splinePoints[i*3 : i*3+4]
curve, err := bezier.NewBezier(false, pts...)
if err != nil {
log.Fatal(err)
}
if g.showComb {
drawComb(screen, curve)
}
strokeCurve(screen, curve, strokeOp, curveColor)
}
}
// Draw points that user can grab
for _, pt := range g.points {
vector.DrawFilledCircle(screen, float32(pt.X), float32(pt.Y), pointDiameter, pointColor, true)
}
// UI elements
// Draw the toolbar
vector.DrawFilledRect(screen, 0, float32(screenHeight-toolbarHeight), float32(screenWidth), float32(toolbarHeight), toolbarColor, true)
// Draw slider background
vector.DrawFilledRect(screen, float32(sliderX), float32(sliderY), float32(sliderWidth), float32(sliderHeight), sliderBgColor, true)
// Draw slider knob
knobX := sliderX + int(g.omega*float64(sliderWidth))
vector.DrawFilledCircle(screen, float32(knobX), float32(screenHeight-toolbarHeight/2), float32(sliderKnobDiameter/2), sliderKnobColor, true)
vector.StrokeCircle(screen, float32(knobX), float32(screenHeight-toolbarHeight/2), float32(sliderKnobDiameter/2), 1, outlineColor, true)
// Draw comb toggle
toggleColor := toggleOnColor
if !g.showComb {
toggleColor = toggleOffColor
}
vector.DrawFilledCircle(screen, float32(combToggleX), float32(combToggleY), float32(sliderHeight), toggleColor, true)
vector.StrokeCircle(screen, float32(combToggleX), float32(combToggleY), float32(sliderHeight), 1, outlineColor, true)
// Draw natural toggle
toggleColor = toggleOnColor
if !g.showNatural {
toggleColor = toggleOffColor
}
vector.DrawFilledCircle(screen, float32(naturalToggleX), float32(naturalToggleY), float32(sliderHeight), toggleColor, true)
vector.StrokeCircle(screen, float32(naturalToggleX), float32(naturalToggleY), float32(sliderHeight), 1, outlineColor, true)
textOp := &text.DrawOptions{}
textOp.ColorScale.ScaleWithColor(textColor)
// Draw omega value
omegaText := fmt.Sprintf("w = %.2f", g.omega)
omegaTextWidth := textWidth(omegaText, textFont)
textOp.GeoM.Translate(float64(sliderX-omegaTextWidth-padding), float64(sliderY-3))
text.Draw(screen, omegaText, text.NewGoXFace(textFont), textOp)
// Draw "Show Comb" label next to toggle
textOp = &text.DrawOptions{}
textOp.ColorScale.ScaleWithColor(textColor)
textOp.GeoM.Translate(float64(combToggleX+toggleDiameter), float64(combToggleY-sliderHeight/2-3))
text.Draw(screen, "Show Comb", text.NewGoXFace(textFont), textOp)
// Draw "Show Natural" label next to toggle
textOp = &text.DrawOptions{}
textOp.ColorScale.ScaleWithColor(textColor)
textOp.GeoM.Translate(float64(naturalToggleX+toggleDiameter), float64(naturalToggleY-sliderHeight/2-3))
text.Draw(screen, "Show Natural", text.NewGoXFace(textFont), textOp)
}
func textWidth(s string, face font.Face) int {
bounds, _ := font.BoundString(face, s)
return (bounds.Max.X - bounds.Min.X).Ceil()
}
var cachedColors map[int][]color.Color = make(map[int][]color.Color)
func getCombColors(teeth int) []color.Color {
if colors, found := cachedColors[teeth]; found {
return colors
}
// Calculate new colors and cache them
newColors := make([]color.Color, teeth)
for i := 0; i < teeth; i++ {
newColors[i] = getRainbowColor(float64(i), teeth)
}
cachedColors[teeth] = newColors
return newColors
}
// Function to interpolate a rainbow color based on position
func getRainbowColor(position float64, total int) color.Color {
if len(colors) == 0 {
return color.RGBA{0, 0, 0, 255} // return black if no colors are available
}
if total <= 1 {
// If total is 1 or less, return the first color to avoid division by zero
return colors[0]
}
// Ensure position is within [0, total-1]
if position < 0 {
position = 0
} else if position >= float64(total) {
position = float64(total - 1)
}
// Scale position to range within [0, len(colors) - 1]
scale := (position / float64(total-1)) * float64(len(colors)-1)
index := int(scale)
fraction := scale - float64(index)
if index >= len(colors)-1 {
return colors[len(colors)-1]
}
// Interpolate between colors[index] and colors[index+1]
c1, c2 := colors[index], colors[index+1]
r1, g1, b1, _ := c1.RGBA()
r2, g2, b2, _ := c2.RGBA()
r := interpolateColorComponent(r1, r2, fraction)
g := interpolateColorComponent(g1, g2, fraction)
b := interpolateColorComponent(b1, b2, fraction)
return color.RGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: 255}
}
func interpolateColorComponent(c1, c2 uint32, fraction float64) uint8 {
return uint8((float64(c1>>8)*(1-fraction) + float64(c2>>8)*fraction))
}
func strokeCurve(dst *ebiten.Image, curve *bezier.Bezier, strokeOp *vector.StrokeOptions, color color.Color) {
path := &vector.Path{}
path.MoveTo(float32(curve.Points[0].X), float32(curve.Points[0].Y))
if len(curve.Points) == 3 {
path.QuadTo(float32(curve.Points[1].X), float32(curve.Points[1].Y), float32(curve.Points[2].X), float32(curve.Points[2].Y))
} else if len(curve.Points) == 4 {
path.CubicTo(float32(curve.Points[1].X), float32(curve.Points[1].Y), float32(curve.Points[2].X), float32(curve.Points[2].Y), float32(curve.Points[3].X), float32(curve.Points[3].Y))
}
vs, is := path.AppendVerticesAndIndicesForStroke(nil, nil, strokeOp)
drawVerticesForUtil(dst, vs, is, color, true)
path.Close()
}
const PIXELS_PER_COMB_TOOTH = 5
func drawComb(dst *ebiten.Image, curve *bezier.Bezier) {
length := curve.Length()
teeth := math.Floor(length / PIXELS_PER_COMB_TOOTH)
colors := getCombColors(int(teeth))
step := 1 / teeth
for i := 0.0; i < teeth; i++ {
t := i * step
p := curve.Get(t)
n := curve.Normal(t)
kr := curve.Curvature(t)
p2 := bezier.Point{X: p.X + n.X*kr.K*-1500, Y: p.Y + n.Y*kr.K*-1500}
combColor := colors[int(i)]
// combColor := red
vector.StrokeLine(dst, float32(p.X), float32(p.Y), float32(p2.X), float32(p2.Y), 1, combColor, true)
}
}
func drawVerticesForUtil(dst *ebiten.Image, vs []ebiten.Vertex, is []uint16, clr color.Color, antialias bool) {
r, g, b, a := clr.RGBA()
for i := range vs {
vs[i].SrcX = 1
vs[i].SrcY = 1
vs[i].ColorR = float32(r) / 0xffff
vs[i].ColorG = float32(g) / 0xffff
vs[i].ColorB = float32(b) / 0xffff
vs[i].ColorA = float32(a) / 0xffff
}
op := &ebiten.DrawTrianglesOptions{}
op.ColorScaleMode = ebiten.ColorScaleModePremultipliedAlpha
op.Filter = ebiten.FilterNearest
op.FillRule = ebiten.NonZero
op.AntiAlias = antialias
dst.DrawTriangles(vs, is, whiteSubImage, op)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
s := ebiten.Monitor().DeviceScaleFactor()
return int(float64(outsideWidth) * s), int(float64(outsideHeight) * s)
}