Add trace image generator
This commit is contained in:
1 parent
e26160b80a
commit
76e14bbdf7
5 files changed
+146
-9
No files matched your search
@@ -0,0 +1,58 @@
|
||||
use symplectic_double_pendulum::*;
|
||||
|
||||
use tiny_skia::*;
|
||||
|
||||
const WIDTH: u32 = 800;
|
||||
const HEIGHT: u32 = 800;
|
||||
|
||||
const MIN: f32 = -2.0;
|
||||
const MAX: f32 = 2.0;
|
||||
const MARGIN: f32 = 0.2;
|
||||
|
||||
fn system_xy(q: &DoublePendulum) -> [f64; 2] {
|
||||
[
|
||||
q.theta.sin() + (q.theta + q.phi).sin(),
|
||||
q.theta.cos() + (q.theta + q.phi).cos()
|
||||
]
|
||||
}
|
||||
|
||||
fn canvas_xy(q: &DoublePendulum) -> [f32; 2] {
|
||||
system_xy(q).map(|x| (x as f32 * (1.0 - MARGIN) - MIN) * WIDTH as f32 / (MAX - MIN))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let omega = 500.0;
|
||||
let h = 1e-3;
|
||||
let t = 20.0;
|
||||
|
||||
let mut state = Augmented::from(State {
|
||||
q: DoublePendulum { theta: std::f64::consts::PI, phi: 0.1 },
|
||||
p: DoublePendulum { theta: 0.0, phi: 0.0 },
|
||||
}, omega);
|
||||
|
||||
let path = {
|
||||
let mut pb = PathBuilder::new();
|
||||
|
||||
let [x, y] = canvas_xy(&state.average().q);
|
||||
pb.move_to(x, y);
|
||||
|
||||
let n = (t / h) as u32;
|
||||
for _ in 0..n {
|
||||
state.evolve(h);
|
||||
let [x, y] = canvas_xy(&state.average().q);
|
||||
pb.line_to(x, y);
|
||||
}
|
||||
|
||||
pb.finish().unwrap()
|
||||
};
|
||||
|
||||
let mut paint = Paint::default();
|
||||
paint.set_color_rgba8(0, 0, 0, 0xff);
|
||||
paint.anti_alias = true;
|
||||
let mut stroke = Stroke::default();
|
||||
stroke.width = 4.0;
|
||||
stroke.line_cap = LineCap::Round;
|
||||
let mut pixmap = Pixmap::new(WIDTH, HEIGHT).unwrap();
|
||||
pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None);
|
||||
pixmap.save_png("trace.png").unwrap();
|
||||
}
|
||||
Reference in new issue
Block a user