Interpolation methods for computation of cubic spline points within the range of a discrete set of known points.
```rust use cubic_spline::{CalcPoints, SplineOpts, SplineResult, SrcPoints};
let points = vec![(10.0, 200.0), (256.0, 390.0), (512.0, 10.0), (778.0, 200.0)];
let opts = SplineOpts { numofsegments: 16, ..Default::default() };
let pts: SrcPoints<(f64, f64)> = SrcPoints::new(&points); let mut result = SplineResult::<(f64, f64)>::new(); pts.calc(&opts, &mut result);
assert_eq!(result.get().len(), 51);
// // Same as: // use cubicspline::{Spline}; let splinepoints = Spline::from_tuples(&points, &opts);
asserteq!(splinepoints.len(), 51); ```
For now source and resulting points may be Vec<f64> - (vec![x,y,x,y,...])
or Vec<(f64, f64)> - (vec![(x,y),(x,y), ...])
.
For this types of points there are two helper functions Spline::from_flatten_points
and Spline::from_tuples
If you allready have some points to avoid unnecessary copying, creating new Vec
etc. you can implement GetPoint
trait. And if you need some particular result implement PushPoint
.
```rust use cubic_spline::{CalcPoints,SrcPoints,SplineResult,PushPoint,GetPoint,SplineOpts};
struct MyPoint {
pub top: f32,
pub left: f32,
pub label: Option
struct MyResult
impl<'a> GetPoint for MySrcPoint
impl PushPoint for MyResult
impl<'a> CalcPoints for MySrcPoint
let points: Vec
```
See here for implementation example
```js import { getCurvePoints } from 'cubic-spline-rs'
const NUMOFSEGMENTS = 22
const points = [10.0, 200.0, 256.0, 390.0, 512.0, 10.0, 778.0, 200.0]
const curvePoints = getCurvePoints( points, { numofsegments: NUMOFSEGMENTS, // *optional // tension: 0.5, // *optional } )
```
If you want to draw result points to canvas code like this ```js const ctx = getMyCanvas2DContext()
ctx.beginPath() ctx.lineWidth = 3 ctx.strokeStyle = COLORS.stroke
ctx.moveTo(curvePoints[0], curvePoints[1]) const length = curvePoints.length - 1 for (let i = 2; i < length; i += 2) { ctx.lineTo(curvePoints[i], curvePoints[i + 1]) }
ctx.stroke() ctx.closePath() ``` See example here.
| Name | Type | Default | Description |
|--------------------------|:------:|:-------:|-----------------------------------------------------------------------|
| tension | f64
| 0.5
| Tension |
| numofsegments | u32
| 16
| Number of calculated points between known points |
rust
use cubic_spline::{SplineOpts};
let opts = SplineOpts {
tension: 0.6,
..Default.default()
}
This module is MIT licensed.