plotting example

This commit is contained in:
Joshua Perry 2024-11-02 22:55:43 +00:00
parent f7950b903b
commit eb24c7e053
3 changed files with 77 additions and 9 deletions

View File

@ -1,8 +1,9 @@
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use std::path::PathBuf; use std::path::PathBuf;
mod message;
mod encoding; mod encoding;
mod message;
pub mod plotting;
pub use message::Message; pub use message::Message;
pub use encoding::encrypt_message; pub use encoding::encrypt_message;

View File

@ -1,11 +1,22 @@
use circle_cipher:: { use circle_cipher::plotting;
encrypt_message, use eframe::egui;
Message
};
fn main() { struct MyApp;
let mut msg = Message::new("Hello".to_string(), 12);
let circles = encrypt_message(&mut msg, &127);
println!("{:?}", circles) impl Default for MyApp {
fn default() -> Self {
Self
}
}
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
plotting::example(ctx, _frame);
}
}
fn main() -> Result<(), eframe::Error> {
let options = eframe::NativeOptions::default();
eframe::run_native("Polar Plot Example", options, Box::new(|_cc| Ok(Box::<MyApp>::default())))
} }

View File

@ -0,0 +1,56 @@
use std::f64::{self, consts::PI, INFINITY, NEG_INFINITY};
use egui_plot::{Plot, PlotPoints, Points};
pub fn example(ctx: &egui::Context, _frame: &mut eframe::Frame) {
let num_points = 1000;
let baseline_r = 1.0;
let peak_values = vec![3.0, 7.0, 5.0];
let num_peaks = peak_values.len();
let peak_angles: Vec<f64> = (0..num_peaks)
.map(|i| 2.0 * PI * (i as f64) / (num_peaks as f64))
.collect();
let theta: Vec<f64> = (0..num_points)
.map(|i| 2.0 * PI * (i as f64) / (num_points as f64))
.collect();
let mut r: Vec<f64> = vec![baseline_r; num_points];
for (peak_value, peak_angle) in peak_values.iter().zip(peak_angles.iter()) {
let peak_width: f64 = 0.1;
for (j, theta_value) in theta.iter().enumerate() {
let delta = (theta_value - peak_angle + PI).rem_euclid(2.0 * PI) - PI;
r[j] += peak_value * (-((delta.powf(2.0)) / (2.0 * peak_width.powf(2.0)))).exp();
}
}
let min_r = r.iter().cloned().fold(INFINITY, f64::min);
let max_r = r.iter().cloned().fold(NEG_INFINITY, f64::max);
let range = max_r - min_r;
if range != 0.0 {
r.iter_mut().for_each(|value| {
*value = 1.0 + (*value - min_r) / (max_r - min_r);
});
}
egui::CentralPanel::default().show(ctx, |ui| {
Plot::new("Polar Plot")
.height(400.0)
.width(400.0)
.view_aspect(1.0)
.show(ui, |plot_ui| {
let points = theta.iter()
.zip(r.iter())
.map(|(&theta, &radius)| {
let x = radius * theta.cos();
let y = radius * theta.sin();
[x, y]
}).collect();
plot_ui.line(egui_plot::Line::new(PlotPoints::new(points)));
});
});
}