Skip to content

Instantly share code, notes, and snippets.

@wareya
Created January 14, 2023 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wareya/e21a199ddd3e5d8fbc6214191d2c3570 to your computer and use it in GitHub Desktop.
Save wareya/e21a199ddd3e5d8fbc6214191d2c3570 to your computer and use it in GitHub Desktop.
// cargo add sha2 and plotters
use sha2::Digest;
use plotters::prelude::*;
const OUT_FILE_NAME: &'static str = "output-test.png";
fn main() {
// same for Sha512
let mut bins_a = vec!(0; 256);
let mut bins_b = vec!(0; 256);
let mut max = 0;
for i in 0..0xFFFF
{
let mut hasher = sha2::Sha512::new();
hasher.update((i as u16).to_le_bytes());
let result = hasher.finalize()[..].to_vec();
let a = result[0];
let mut hasher = sha2::Sha512::new();
hasher.update([a]);
let result = hasher.finalize()[..].to_vec();
let b = result[1];
bins_a[a as usize] += 1;
bins_b[b as usize] += 1;
max = std::cmp::max(bins_a[a as usize], max);
max = std::cmp::max(bins_b[b as usize], max);
}
println!("{:?}", bins_a);
println!("{:?}", bins_b);
let root = BitMapBackend::new(OUT_FILE_NAME, (1280, 720)).into_drawing_area();
root.fill(&WHITE).unwrap();
let mut chart = ChartBuilder::on(&root)
.set_label_area_size(LabelAreaPosition::Left, 40)
.set_label_area_size(LabelAreaPosition::Bottom, 40)
.build_cartesian_2d(0u32..256u32, (0u32..max).into_segmented())
.unwrap();
chart
.configure_mesh()
.bold_line_style(&WHITE.mix(0.3))
.y_desc("Count")
.x_desc("Bucket")
.axis_desc_style(("sans-serif", 15))
.draw().unwrap();
chart.draw_series(bins_a.iter().enumerate().map(|(i, n)| {
let i = i as u32;
let mut bar = Rectangle::new([(i, SegmentValue::Exact(0)), (i+1, SegmentValue::Exact(*n as u32))], RED.mix(0.5).filled());
bar.set_margin(0, 0, 5, 5);
bar
})).unwrap();
chart.draw_series(bins_b.iter().enumerate().map(|(i, n)| {
let i = i as u32;
let mut bar = Rectangle::new([(i, SegmentValue::Exact(0)), (i+1, SegmentValue::Exact(*n as u32))], BLUE.mix(0.5).filled());
bar.set_margin(0, 0, 5, 5);
bar
})).unwrap();
root.present().unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment