Documentation ¶
Overview ¶
Package splines calculates cubic spline for drawing lines through points.
The spline calculations are based on a [mathworld](https://mathworld.wolfram.com/CubicSpline.html) article.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Polynomial ¶
type Polynomial struct {
A, B, C, D float64
}
Polynomial describes a fourth order polynomial curve. The polynomial has the form of "a + b*t + c*t^2 + d*t^3".
func SolveSpline ¶
func SolveSpline(ys []float64) ([]Polynomial, error)
SolveSpline returns a slice with 'n-1' [Polynomial]s for n values of 'y'.
Example ¶
package main import ( "fmt" splines "gitlab.com/Achilleshiel/gosplines" ) func main() { xs := []float64{0, 2, 4, 6, 8} ys := []float64{0, 1, 0, -1, 0} xPolynomial, _ := splines.SolveSpline(xs) yPolynomial, _ := splines.SolveSpline(ys) x, y := xPolynomial[0].Calculate(0.6), yPolynomial[0].Calculate(0.6) fmt.Printf("first polynomial with t=0.6: x=%0.2f, y=%0.2f\n", x, y) x, y = xPolynomial[2].Calculate(0.1), yPolynomial[2].Calculate(0.1) fmt.Printf("third polynomial with t=0.1: x=%0.2f, y=%0.2f\n", x, y) }
Output: first polynomial with t=0.6: x=1.20, y=0.79 third polynomial with t=0.1: x=4.20, y=-0.15
func SolveSplineWithConstraint ¶
func SolveSplineWithConstraint(ys []float64, start, end float64) ([]Polynomial, error)
SolveSplineWithConstraint returns a slice with n-1 Polynomials for n values of y. start and end values determine the velocity of the start and end point.
Example ¶
package main import ( "fmt" "math" splines "gitlab.com/Achilleshiel/gosplines" ) func main() { xs := []float64{0, 1} ys := []float64{0, 1} xPolynomial, _ := splines.SolveSpline(xs) yPolynomial, _ := splines.SolveSpline(ys) x, y := xPolynomial[0].Calculate(0.5), yPolynomial[0].Calculate(0.5) fmt.Printf("polynomial without constraints for t=0.5: x=%0.2f, y=%0.2f\n", x, y) xPolynomial, _ = splines.SolveSplineWithConstraint(xs, math.Cos(math.Pi/2), math.Cos(math.Pi)) yPolynomial, _ = splines.SolveSplineWithConstraint(ys, math.Sin(math.Pi/2), math.Sin(math.Pi)) x, y = xPolynomial[0].Calculate(0.5), yPolynomial[0].Calculate(0.5) fmt.Printf("polynomial with constraints for t=0.5: x=%0.2f, y=%0.2f\n", x, y) }
Output: polynomial without constraints for t=0.5: x=0.50, y=0.50 polynomial with constraints for t=0.5: x=0.38, y=0.62
func (Polynomial) Calculate ¶
func (c Polynomial) Calculate(t float64) float64
Calculate the polynomial for a given value t.
Click to show internal directories.
Click to hide internal directories.