use crate::render::Rect;
use super::Coord;
pub struct CoordPolar {
pub theta: String,
pub start: f64,
pub direction: f64,
}
impl CoordPolar {
pub fn new() -> Self {
CoordPolar {
theta: "x".to_string(),
start: 0.0,
direction: 1.0,
}
}
pub fn theta(mut self, theta: &str) -> Self {
self.theta = theta.to_string();
self
}
pub fn start(mut self, start: f64) -> Self {
self.start = start;
self
}
pub fn direction(mut self, dir: f64) -> Self {
self.direction = dir;
self
}
}
impl Default for CoordPolar {
fn default() -> Self {
Self::new()
}
}
impl Coord for CoordPolar {
fn transform(&self, point: (f64, f64), plot_area: &Rect) -> (f64, f64) {
let (nx, ny) = point;
let (angle_norm, radius_norm) = if self.theta == "x" {
(nx, ny)
} else {
(ny, nx)
};
let angle = self.start + self.direction * angle_norm * std::f64::consts::TAU;
let max_radius = plot_area.width.min(plot_area.height) / 2.0;
let radius = radius_norm * max_radius;
let cx = plot_area.x + plot_area.width / 2.0;
let cy = plot_area.y + plot_area.height / 2.0;
let px = cx + radius * angle.sin();
let py = cy - radius * angle.cos();
(px, py)
}
fn gridlines(&self) -> bool {
false
}
fn is_flipped(&self) -> bool {
false
}
fn is_polar(&self) -> bool {
true
}
}