From eb24c7e0536dd749272af251e0a72d4ffead1b9c Mon Sep 17 00:00:00 2001 From: r0r-5chach Date: Sat, 2 Nov 2024 22:55:43 +0000 Subject: [PATCH] plotting example --- src/lib.rs | 3 ++- src/main.rs | 27 +++++++++++++++++------- src/plotting.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7048535..3b486fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,9 @@ use clap::{Parser, Subcommand}; use std::path::PathBuf; -mod message; mod encoding; +mod message; +pub mod plotting; pub use message::Message; pub use encoding::encrypt_message; diff --git a/src/main.rs b/src/main.rs index 4a2b7df..d9511cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,22 @@ -use circle_cipher:: { - encrypt_message, - Message -}; +use circle_cipher::plotting; +use eframe::egui; -fn main() { - let mut msg = Message::new("Hello".to_string(), 12); - let circles = encrypt_message(&mut msg, &127); +struct MyApp; - 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::::default()))) } diff --git a/src/plotting.rs b/src/plotting.rs index e69de29..eba1ae1 100644 --- a/src/plotting.rs +++ b/src/plotting.rs @@ -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 = (0..num_peaks) + .map(|i| 2.0 * PI * (i as f64) / (num_peaks as f64)) + .collect(); + + + let theta: Vec = (0..num_points) + .map(|i| 2.0 * PI * (i as f64) / (num_points as f64)) + .collect(); + + let mut r: Vec = 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))); + }); + }); +}