feat: add other fractals
This commit is contained in:
+27
-5
@@ -2,15 +2,18 @@
|
||||
//! iterations, Julia constant, coloring) as a compact URL fragment so deep-zoom
|
||||
//! locations can be shared or bookmarked.
|
||||
//!
|
||||
//! Format: `m=m&re=<dec>&im=<dec>&hh=<f64>&it=<u32>&cs=<f32>&co=<f32>` with
|
||||
//! Format: `m=m&f=<str>&re=<dec>&im=<dec>&hh=<f64>&it=<u32>&cs=<f32>&co=<f32>` with
|
||||
//! `m=j&jr=<f64>&ji=<f64>` added for Julia. `re`/`im` are full-precision decimal
|
||||
//! strings.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::fractal::FractalKind;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ShareState {
|
||||
pub julia: bool,
|
||||
pub kind: FractalKind,
|
||||
pub center_re: String,
|
||||
pub center_im: String,
|
||||
pub half_height: f64,
|
||||
@@ -24,14 +27,21 @@ impl ShareState {
|
||||
pub fn encode(&self) -> String {
|
||||
let mut s = String::new();
|
||||
s.push_str(if self.julia { "m=j" } else { "m=m" });
|
||||
s.push_str(&format!("&f={}", match self.kind {
|
||||
FractalKind::Mandelbrot => "mandel",
|
||||
FractalKind::BurningShip => "burning",
|
||||
FractalKind::Multibrot => "multi",
|
||||
FractalKind::Tricorn => "tricorn",
|
||||
}));
|
||||
s.push_str(&format!(
|
||||
"&re={}&im={}&hh={}&it={}",
|
||||
self.center_re, self.center_im, self.half_height, self.iterations
|
||||
));
|
||||
if self.julia {
|
||||
s.push_str(&format!("&jr={}&ji={}", self.julia_c.0, self.julia_c.1));
|
||||
}
|
||||
s.push_str(&format!("&cs={}&co={}", self.color_scale, self.color_offset));
|
||||
s.push_str(&format!("&jr={}&ji={}", self.julia_c.0, self.julia_c.1));
|
||||
s.push_str(&format!(
|
||||
"&cs={}&co={}",
|
||||
self.color_scale, self.color_offset
|
||||
));
|
||||
s
|
||||
}
|
||||
|
||||
@@ -46,6 +56,16 @@ impl ShareState {
|
||||
|
||||
Some(ShareState {
|
||||
julia: map.get("m").map(|m| *m == "j").unwrap_or(false),
|
||||
kind: map
|
||||
.get("f")
|
||||
.map(|f| match *f {
|
||||
"mandel" => FractalKind::Mandelbrot,
|
||||
"multi" => FractalKind::Multibrot,
|
||||
"burning" => FractalKind::BurningShip,
|
||||
"tricorn" => FractalKind::Tricorn,
|
||||
_ => FractalKind::Mandelbrot
|
||||
})
|
||||
.unwrap_or(FractalKind::Mandelbrot),
|
||||
center_re: (*map.get("re")?).to_string(),
|
||||
center_im: (*map.get("im")?).to_string(),
|
||||
half_height: map.get("hh")?.parse().ok()?,
|
||||
@@ -68,6 +88,7 @@ mod tests {
|
||||
fn round_trip() {
|
||||
let s = ShareState {
|
||||
julia: true,
|
||||
kind: FractalKind::Tricorn,
|
||||
center_re: "-0.743643887037158704752191506114774".into(),
|
||||
center_im: "0.131825904205311970493132056385139".into(),
|
||||
half_height: 1.5e-20,
|
||||
@@ -78,6 +99,7 @@ mod tests {
|
||||
};
|
||||
let d = ShareState::decode(&s.encode()).unwrap();
|
||||
assert_eq!(d.julia, s.julia);
|
||||
assert_eq!(d.kind, s.kind);
|
||||
assert_eq!(d.center_re, s.center_re);
|
||||
assert_eq!(d.center_im, s.center_im);
|
||||
assert_eq!(d.half_height, s.half_height);
|
||||
|
||||
Reference in New Issue
Block a user