npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cubic-spline-rs

v0.9.4

Published

Function that calculates curve points for cubic spline

Downloads

6

Readme

cubic_spline

Crates.io npm

Interpolation method for computation of cubic spline points within the range of a discrete set of known points.

Online documentation Demo

example

Example

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::try_from(&source).expect("expect valid points but");
  let result = points.calc_spline(&opts).expect("cant construct spline points");
  
  assert_eq!(result.get_ref().len(), 49);
  
  let inner_vec: &mut Vec<Point> = points.get_mut();
  inner_vec.push(Point::new(7.7, 1.3));
  inner_vec[1].x += 0.79;
  inner_vec.last_mut().iter_mut().for_each(|mut p| {p.tension = Some(0.7);});
  
  points.invert_vertically(400.0);
  
  assert_eq!(points.get_ref()[1].y, 10.0);
  
  let calculated_points = points
    .calc_spline(&opts.num_of_segments(33))
    .unwrap();
  
  assert_eq!(calculated_points.into_inner().len(), 133);

}

For information on how a curve can be constructed and which points to accept, see the appropriate structures.

Custom points

If you already have some points you can implement From trait for Point struct and pass your points directly.

Example

use cubic_spline::{SplineOpts, Point, Points};

#[derive(Default)]
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 my_points: Vec<MyPoint> = vec![MyPoint::default(),MyPoint::default()];
  let spline = Points::from(&my_points)
    .calc_spline(&SplineOpts::default())
    .unwrap();
  
  assert_eq!(spline.get_ref().len(), 17);
}

Use in Javascript

It also compiled as wasm module. And you can use it in your js code but not completely. Now available only one function

import { getCurvePoints } from 'cubic-spline-rs'

const NUM_OF_SEGMENTS = 22

const points = [10.0, 200.0, 256.0, 390.0, 512.0, 10.0, 778.0, 200.0]

const curvePoints = getCurvePoints( points, {
  num_of_segments: NUM_OF_SEGMENTS, // *optional
  // tension: 0.5, // *optional
  // ...  
} )

If you want to draw result points to canvas - code like this:

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()

See example here.

Options

| Name | Type | Default | Description | | --------------------- | :-----------------: | :-----: | ------------------------------------------------------------------------------------------- | | tension | f64 | 0.5 | Tension | | num_of_segments | u32 | 16 | Number of calculated points between known points | | hidden_point_at_start | Option<(f64,f64)> | None | A point that will not be drawn, but the beginning of the graph will bend as if it is there. | | hidden_point_at_end | Option<(f64,f64)> | None | A point that will not be drawn, but the end of the graph will bend as if it is there. |

use cubic_spline::{SplineOpts};

fn main() {
  let options = SplineOpts::new()
    .tension(0.6)
    .num_of_segments(54)
    // .hidden_point_at_start((1.2, 3.1))
    // .hidden_point_at_end((397.9, 105.5))
    ;

}


License

This module is MIT licensed.