/
phyalex
/
particle-electromagnetic-simulation
Обзор
Документация
Войти
/
phyalex
/
particle-electromagnetic-simulation
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/visualization.rs
53 строки
2 KB
phyalex
Optimized imports.
05 ноя 2025, 20:40
05 ноя 2025, 20:40
532c729
Код
Авторство
О чём код?
use crate::vectors::Vector2D; use crate::physics::constants::CELL_SIZE; use gnuplot::{Figure, Color, GnuplotInitError}; type Pixel = f64; // Was ist Pixel? pub struct Scene { fig : Figure, wire_image: Vec<Pixel>, image_rows_number: usize, } impl Scene { pub fn new(wire_image: &Vec<Vec<Pixel>>) -> Self { let (n, m) = (wire_image.len(), wire_image[0].len()); let mut matrix = Vec::with_capacity(n * m); for line in wire_image { for x in line { matrix.push(*x); } } Self { fig : Figure::new(), wire_image : matrix, image_rows_number : n, } } pub fn update<'a, I: IntoIterator<Item=&'a Vector2D>>(&mut self, proton_positions: I, electron_positions: I) -> Result<(), GnuplotInitError>{ let n = self.image_rows_number; let m = self.wire_image.len() / n; let (red_xs, red_ys): (Vec<f64>, Vec<f64>) = proton_positions.into_iter() .map(|v| (v.x, v.y)).unzip(); let (blue_xs, blue_ys): (Vec<f64>, Vec<f64>) = electron_positions.into_iter() .map(|v| (v.x, v.y)).unzip(); self.fig.clear_axes() .axes2d() .image(&self.wire_image, n, m, Some((0.0, 0.0, n as f64 * CELL_SIZE, m as f64 * CELL_SIZE)), &[]) .points(red_xs, red_ys, &[Color("red".into())]) .points(blue_xs, blue_ys, &[Color("blue".into())]); self.fig.show_and_keep_running().map(|_| {}) } }