From 27734995dd1a37dbf4537bdecaeffae947da11e6 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Sat, 27 Apr 2019 21:08:11 +0200 Subject: [PATCH] Save info about best solutions --- src/display.rs | 27 ++++++++++++++++++++++++++- src/main.rs | 2 ++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/display.rs b/src/display.rs index ecec82b..050f639 100644 --- a/src/display.rs +++ b/src/display.rs @@ -1,6 +1,6 @@ use svg::node::element::path::Data; use svg::node::element::Path; -use svg::node::element::{AnimateMotion, Circle, MotionPath}; +use svg::node::element::Circle; use svg::Document; use svg::Node; @@ -8,6 +8,31 @@ use std::fs; use super::*; +fn idx_to_char(idx: usize) -> char { + match idx { + 0 => 'A', + 1 => 'B', + 2 => 'C', + _ => unreachable!() + } +} + +pub(crate) fn save_info(filename: &str, world: &World, tris: &[Triangle], original_tris: &[Triangle]) { + let mut text = format!("Gesamtabstand: {:.0}\n", world.width); + for (idx, (_, tri)) in world.tris.iter().enumerate() { + let unrotated_tri = tris[idx]; + let original_idx = original_tris.iter().position(|x| x.0 == unrotated_tri.0 && x.1 == unrotated_tri.1 && x.2 == unrotated_tri.2).unwrap(); + text += &format!("D{}:", original_idx); + let mut coordinates = vec![tri.0, tri.1, tri.2]; + coordinates.sort_by_key(|x| (x.x * 100.0) as u32); + for (idx, c) in coordinates.into_iter().enumerate() { + text += &format!(" D{}{}[{:.0} {:.0}]", original_idx, idx_to_char(idx), c.x, c.y); + } + text += "\n"; + } + fs::write(filename, text).unwrap(); +} + pub(crate) fn save_tri(filename: &str, tri: Triangle) { fs::write(filename, generate_svg(&[tri])).unwrap(); } diff --git a/src/main.rs b/src/main.rs index 90237f9..be021cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -154,6 +154,7 @@ fn main() { tri.2.y -= tri.0.y; tri.0.y = 0.0; } + let original_tris = tris.clone(); let mut best = f32::MAX; let mut i = 0u64; let save_prefix = format!("rand_sel_{}_", random::()); @@ -166,6 +167,7 @@ fn main() { //eprint!("\r"); println!("[{}] BEST {:.3} ", i, best); display::save_world(&format!("{}{}.svg", save_prefix, i), &new_best); + display::save_info(&format!("{}{}.txt", save_prefix, i), &new_best, &tris, &original_tris); } else { //eprint!("\r"); println!("[{}] best {:.3} / prev. {:.3} ", i, new_best.width, best);