Interpolation method for computation of cubic spline points within the range of a discrete set of known points.
```rust use cubic_spline::{Points, Point, SplineOpts, TryFrom};
fn main() { let source = vec![(10.0, 200.0), (256.0, 390.0), (512.0, 10.0), (778.0, 200.0)];
let opts = SplineOpts::new() .tension(0.5);
let mut points = Points::tryfrom(&source).expect("expect valid points but"); let result = points.calcspline(&opts).expect("cant construct spline points");
asserteq!(result.getref().len(), 49);
let innervec: &mut Vec
points.invert_vertically(400.0);
asserteq!(points.getref()[1].y, 10.0);
let calculatedpoints = points .calcspline(&opts.numofsegments(33)) .unwrap();
asserteq!(calculatedpoints.into_inner().len(), 133);
} ```
For information on how a curve can be constructed and which points to accept, see the appropriate structures.
If you already have some points you can implement From
trait for Point
struct and pass your points directly.
```rust use cubic_spline::{SplineOpts, Point, Points};
struct MyPoint { vertical: u8, horizontal: u8, color: String, }
impl<'a> From<&'a MyPoint> for Point { fn from(p: &'a MyPoint) -> Self { Point::new(&p.horizontal as f64, &p.vertical as f64) } }
fn main() {
let mypoints: Vec
asserteq!(spline.getref().len(), 17); }
```
It also compiled as wasm module. And you can use it in your js code but not completely. Now available only one function
```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 = '#ffcc00'
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() ```
| Name | Type | Default | Description |
| --------------------- | :-----------------: | :-----: | ------------------------------------------------------------------------------------------- |
| tension | f64
| 0.5
| Tension |
| numofsegments | u32
| 16
| Number of calculated points between known points |
| hiddenpointatstart | Option<(f64,f64)>
| None
| A point that will not be drawn, but the beginning of the graph will bend as if it is there. |
| hiddenpointatend | Option<(f64,f64)>
| None
| A point that will not be drawn, but the end of the graph will bend as if it is there. |
```rust use cubic_spline::{SplineOpts};
fn main() { let options = SplineOpts::new() .tension(0.6) .numofsegments(54) // .hiddenpointatstart((1.2, 3.1)) // .hiddenpointatend((397.9, 105.5)) ;
}
```
This module is MIT licensed.