-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy patharea_under_spline.rs
More file actions
76 lines (66 loc) · 2.3 KB
/
area_under_spline.rs
File metadata and controls
76 lines (66 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use anyhow::Result;
use peroxide::fuga::*;
fn main() -> Result<()> {
// 1. Define the B-spline
let degree = 3;
let knots = vec![0f64, 1f64, 2f64, 3f64, 4f64];
let control_points = vec![
vec![0f64, 0f64],
vec![1f64, 2f64],
vec![2f64, -1f64],
vec![3f64, 1f64],
vec![4f64, -1f64],
vec![5f64, 2f64],
vec![6f64, 0f64],
];
let spline = BSpline::clamped(degree, knots, control_points.clone())?;
let deriv_spline = spline.derivative()?;
// 2. Define the integrand for ∫y dx = ∫ y(t) * (dx/dt) dt
let integrand = |t: f64, s: &BSpline, ds: &BSpline| -> f64 {
let p = s.eval(t);
let dp = ds.eval(t);
let y_t = p.1;
let dx_dt = dp.0;
y_t * dx_dt
};
// 3. Perform numerical integration using self-contained Gauss-Legendre quadrature
let t_start = 0f64;
let t_end = 4f64;
//let area = gauss_legendre_integrate(
// |t| integrand(t, &spline, &deriv_spline),
// t_start,
// t_end,
// 64,
//);
let area = integrate(
|t| integrand(t, &spline, &deriv_spline),
(t_start, t_end),
G7K15R(1e-4, 20),
);
// 4. Print the result
println!("The area under the B-spline curve (∫y dx) is: {}", area);
// For verification, let's plot the original curve
#[cfg(feature = "plot")]
{
let t = linspace(t_start, t_end, 200);
let (x, y): (Vec<f64>, Vec<f64>) = spline.eval_vec(&t).into_iter().unzip();
let mut plt = Plot2D::new();
plt.set_title(&format!("Original B-Spline (Area approx. {:.4})", area))
.set_xlabel("x")
.set_ylabel("y")
.insert_pair((x.clone(), y.clone()))
.insert_pair((
control_points.iter().map(|p| p[0]).collect(),
control_points.iter().map(|p| p[1]).collect(),
))
.set_plot_type(vec![(0, PlotType::Line), (1, PlotType::Scatter)])
.set_color(vec![(0, "blue"), (1, "red")])
.set_legend(vec!["Spline", "Control Points"])
.set_style(PlotStyle::Nature)
.set_path("example_data/bspline_with_area.png")
.set_dpi(600)
.savefig()?;
println!("Generated plot: example_data/bspline_with_area.png");
}
Ok(())
}