plotting
This commit is contained in:
parent
eb24c7e053
commit
1e8d62ff14
|
|
@ -2,20 +2,27 @@ use std::{iter, ops::Add};
|
|||
use crate::message::{self, Message};
|
||||
|
||||
pub fn encrypt_message(message: &mut Message, circle_length: &usize) -> Vec<Circle> {
|
||||
fill_circles(
|
||||
&mut encode_circles(
|
||||
encode_circles(
|
||||
&mut triples_into_circles(&message.to_triples(), circle_length),
|
||||
&mut message.parts.start_shift,
|
||||
),
|
||||
circle_length
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
fn caesar_shift(char: char, shift: u8) -> char {
|
||||
(char as u8 + shift) as char
|
||||
shift.wrapping_add(char as u8) as char
|
||||
}
|
||||
|
||||
type Circle = Vec<message::Triple>;
|
||||
pub type Circle = Vec<message::Triple>;
|
||||
|
||||
pub fn circle_to_points(circle: Circle) -> Vec<f64> {
|
||||
circle.iter().flat_map(|triple| {
|
||||
vec![
|
||||
triple.first as u8 as f64,
|
||||
triple.second as u8 as f64,
|
||||
triple.third as u8 as f64
|
||||
]
|
||||
}).collect()
|
||||
}
|
||||
|
||||
fn triples_into_circles(triples: &Vec<message::Triple>, circle_length: &usize) -> Vec<Circle> {
|
||||
triples.chunks(*circle_length)
|
||||
|
|
@ -60,13 +67,3 @@ fn encode_circles(circles: &mut Vec<Circle>, shift: &mut u8) -> Vec<Circle> {
|
|||
}).collect()
|
||||
}
|
||||
|
||||
fn fill_circles(circles: &mut Vec<Circle>, circle_length: &usize) -> Vec<Circle> {
|
||||
circles.iter_mut().map(|circle| {
|
||||
circle.extend(iter::repeat(message::Triple::filler())
|
||||
.take(
|
||||
circle_length - circle.len()
|
||||
)
|
||||
);
|
||||
circle.clone()
|
||||
}).collect()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ pub mod plotting;
|
|||
|
||||
pub use message::Message;
|
||||
pub use encoding::encrypt_message;
|
||||
pub use encoding::circle_to_points;
|
||||
pub use encoding::Circle;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(version, about, long_about = None)]
|
||||
|
|
|
|||
48
src/main.rs
48
src/main.rs
|
|
@ -1,22 +1,52 @@
|
|||
use circle_cipher::plotting;
|
||||
use circle_cipher::{encrypt_message, plotting};
|
||||
use eframe::egui;
|
||||
use egui_plot::{Plot, PlotPoints};
|
||||
|
||||
struct MyApp;
|
||||
struct MyApp {
|
||||
circles: Vec<circle_cipher::Circle>,
|
||||
cicrle_length: usize,
|
||||
}
|
||||
|
||||
impl Default for MyApp {
|
||||
fn default() -> Self {
|
||||
Self
|
||||
impl MyApp {
|
||||
fn new(circles: Vec<circle_cipher::Circle>, cicrle_length: usize) -> Self {
|
||||
Self {
|
||||
circles,
|
||||
cicrle_length,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl eframe::App for MyApp {
|
||||
|
||||
impl eframe::App for MyApp { //TODO: Define axis ranges
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
plotting::example(ctx, _frame);
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
Plot::new("Polar Plot")
|
||||
.height(500.0)
|
||||
.width(500.0)
|
||||
.data_aspect(1.0)
|
||||
.view_aspect(1.0)
|
||||
.show(ui, |plot_ui| {
|
||||
self.circles.iter().enumerate().for_each(|(count, circle)| {
|
||||
plot_ui.line(egui_plot::Line::new(
|
||||
plotting::plot(ctx, _frame,
|
||||
self.cicrle_length,
|
||||
circle_cipher::circle_to_points(circle.clone()),
|
||||
count
|
||||
)
|
||||
));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() -> Result<(), eframe::Error> {
|
||||
let mut message = circle_cipher::Message::new("Hello".to_string(), 12);
|
||||
let circles = encrypt_message(&mut message, &127);
|
||||
println!("{:?}", circles.len());
|
||||
|
||||
let options = eframe::NativeOptions::default();
|
||||
eframe::run_native("Polar Plot Example", options, Box::new(|_cc| Ok(Box::<MyApp>::default())))
|
||||
eframe::run_native("Polar Plot Example", options, Box::new(|_cc| Ok(Box::<MyApp>::new(
|
||||
MyApp::new(circles, 1000)
|
||||
))))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,16 @@
|
|||
use std::f64::{self, consts::PI, INFINITY, NEG_INFINITY};
|
||||
|
||||
use egui_plot::{Plot, PlotPoints, Points};
|
||||
use egui_plot::{Plot, PlotPoints};
|
||||
|
||||
pub fn example(ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
let num_points = 1000;
|
||||
pub fn plot(ctx: &egui::Context, _frame: &mut eframe::Frame, num_points: usize, peak_values: Vec<f64>, offset: usize) -> PlotPoints {
|
||||
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))
|
||||
.map(|i| {
|
||||
let angle = 2.0 * PI * (i as f64) / (num_peaks as f64);
|
||||
angle + (PI / 2.0)
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
||||
|
|
@ -19,7 +20,7 @@ pub fn example(ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
|||
|
||||
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;
|
||||
let peak_width: f64 = 1.0 / num_points as f64;
|
||||
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();
|
||||
|
|
@ -28,29 +29,16 @@ pub fn example(ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
|||
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);
|
||||
});
|
||||
}
|
||||
let r: Vec<f64> = r.iter_mut().map(|value| {
|
||||
1.0 + (offset as f64) + (*value - min_r) / (max_r - min_r)
|
||||
}).collect();
|
||||
|
||||
|
||||
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)));
|
||||
});
|
||||
});
|
||||
theta.iter()
|
||||
.zip(r.iter())
|
||||
.map(|(&theta, &radius)| {
|
||||
let x = radius * theta.cos();
|
||||
let y = radius * theta.sin();
|
||||
[x, y]
|
||||
}).collect()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue