-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
bezier.go
43 lines (36 loc) 路 912 Bytes
/
bezier.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
// Copyright 2021 Changkun Ou <changkun.de>. All rights reserved.
// Use of this source code is governed by a GPLv3 license that
// can be found in the LICENSE file.
package geometry
import (
"changkun.de/x/polyred/geometry/primitive"
"changkun.de/x/polyred/math"
)
type BezierCurve struct {
controlPoints []primitive.Vertex
}
func NewBezierCurve(cp ...*primitive.Vertex) *BezierCurve {
bc := &BezierCurve{
controlPoints: make([]primitive.Vertex, len(cp)),
}
for i := range cp {
bc.controlPoints[i] = *cp[i]
}
return bc
}
func (bc *BezierCurve) At(t float64) math.Vector {
n := len(bc.controlPoints)
tc := make([]math.Vector, n)
for i := range bc.controlPoints {
tc[i] = bc.controlPoints[i].Pos
}
// The de Casteljau algorithm.
for j := 0; j < n; j++ {
for i := 0; i < n-j-1; i++ {
b01 := math.LerpV(tc[i], tc[i+1], t)
tc[i].X = b01.X
tc[i].Y = b01.Y
}
}
return tc[0]
}