use std::f64::consts::PI;
pub trait FigureDrawer {
fn draw<F>(&self, x: f64, y: f64, size: f64, get_neighbor: Option<F>) -> String
where
F: Fn(i32, i32) -> bool;
}
pub type NeighborFn = dyn Fn(i32, i32) -> bool;
pub fn rotate_transform(x: f64, y: f64, size: f64, rotation: f64) -> Option<String> {
if rotation.abs() < 0.0001 {
return None;
}
let cx = x + size / 2.0;
let cy = y + size / 2.0;
let degrees = (180.0 * rotation) / PI;
Some(format!("rotate({},{},{})", degrees, cx, cy))
}
pub fn svg_circle(cx: f64, cy: f64, r: f64, transform: Option<&str>) -> String {
match transform {
Some(t) => format!(
r#"<circle cx="{}" cy="{}" r="{}" transform="{}"/>"#,
cx, cy, r, t
),
None => format!(r#"<circle cx="{}" cy="{}" r="{}"/>"#, cx, cy, r),
}
}
pub fn svg_rect(x: f64, y: f64, width: f64, height: f64, transform: Option<&str>) -> String {
match transform {
Some(t) => format!(
r#"<rect x="{}" y="{}" width="{}" height="{}" transform="{}"/>"#,
x, y, width, height, t
),
None => format!(
r#"<rect x="{}" y="{}" width="{}" height="{}"/>"#,
x, y, width, height
),
}
}
pub fn svg_path(d: &str, clip_rule: Option<&str>, transform: Option<&str>) -> String {
let mut attrs = format!(r#"d="{}""#, d);
if let Some(rule) = clip_rule {
attrs.push_str(&format!(r#" clip-rule="{}""#, rule));
}
if let Some(t) = transform {
attrs.push_str(&format!(r#" transform="{}""#, t));
}
format!(r#"<path {}/>"#, attrs)
}