chiark / gitweb /
die: wip svg, fix cooldown timer path and add test cases
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 11 Apr 2022 19:27:16 +0000 (20:27 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 15 Apr 2022 18:04:23 +0000 (19:04 +0100)
I've verified that the 1. test case shows as expected.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
base/misc.rs
base/prelude.rs

index ba3029cf80a4a5036a0871c2dbb0c7228828cba0..e87325e51418796bd29247ff6aa2987202238886 100644 (file)
@@ -37,25 +37,36 @@ pub fn die_cooldown_path<W: fmt::Write>(mut w: W, r: f64, remaining: f64) {
   write!(w, "M 0,-{r} A")?;
 
   let mut arcto = move |proportion: f64| {
-    let angle = proportion * std::f64::consts::TAU;
-    let x = r * angle.cos();
-    let y = -r * angle.sin();
+    let angle = proportion * TAU;
+    let x = r * angle.sin();
+    let y = -r * angle.cos();
     write!(w, " {r},{r} 0 0 1 {x},{y}")
-    //                            | | `sweep-flag (1 = clockwise)
-    //                            | `large-arc-flag (see below)
-    //                            `"angle" (ellipse skew angle)
+    //                  | | `sweep-flag (1 = clockwise)
+    //                  | `large-arc-flag (see below)
+    //                  `"angle" (ellipse skew angle)
   };
   
   // This avoids ever trying to draw an arc segment that is around 180 degrees.
   // If we did so there could be rounding errors that would mean we might
   // disagree with the SVG renderer about whether the angle is <=> 180.
-  for split in [0.33, 0.67] {
+  for split in [0.49, 0.98] {
     if split >= remaining { break }
     arcto(split)?;
   }
   arcto(remaining)?;
 }
 
+#[test]
+fn die_cooldown_path_test() {
+  let t80 = |remaining, exp: &str| {
+    let mut got = String::new();
+    die_cooldown_path(&mut got, 80., remaining).unwrap();
+    assert_eq!(&got, exp);
+  };
+  t80(1./3., "M 0,-80 A 80,80 0 0 1 69.2820323027551,39.999999999999986");
+  t80(1.   , "M 0,-80 A 80,80 0 0 1 5.023241562345087,79.84213827426173 80,80 0 0 1 -10.026658685144373,-79.36917610515822 80,80 0 0 1 -0.000000000000019594348786357652,-80");
+}
+
 pub fn default<T:Default>() -> T { Default::default() }
 
 #[macro_export]
index b1ec9193078d25d84ff7bd5162c11deafad89fe8..c5da23f0655360dfb769769663fa3424a9a13493 100644 (file)
@@ -5,6 +5,7 @@
 pub use std::borrow::Borrow;
 pub use std::cmp::{max, Ordering};
 pub use std::convert::{TryFrom, TryInto};
+pub use std::f64::consts::TAU;
 pub use std::fmt::{self, Debug, Display, Formatter, Write as _};
 pub use std::hash::{Hash, Hasher};
 pub use std::iter;