math_interpolate/lib.rs
1/*
2math-interpolate uses linear interpolation to interpolate two points to find the
3abscissa of the 3rd point.
4*/
5
6pub struct Point {
7 x: f32,
8 y: f32
9}
10
11impl Point {
12 pub fn new(x: f32, y: f32) -> Self {
13 Self { x, y }
14 }
15}
16
17pub fn interpolate(p1: Point, p2: Point, x: f32) -> f32 {
18 (x - p2.x) / (p1.x - p2.x) * (p1.y - p2.y) + p2.y
19}
20
21#[cfg(test)]
22mod tests {
23 use super::*;
24
25 #[test]
26 fn test2() {
27 let p1 = Point::new(0.0, 0.0);
28 let p2 = Point::new(5.0, 5.0);
29 let x = 3.0;
30
31 let result = interpolate(p1, p2, x);
32
33 assert_eq!(result, 3.0);
34 }
35}