splines

package module
v0.0.0-...-443bd1d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2024 License: EUPL-1.2 Imports: 4 Imported by: 1

README

Go Splines

A library that allows you to generate cubic splines.

Get Go Splines

go get gitlab.com/Achilleshiel/gosplines

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL