use crisol_core::data::Bounds;
use super::axis::Scale;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ScreenPoint {
pub x: f32,
pub y: f32,
}
impl ScreenPoint {
pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
pub fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
pub fn is_finite(self) -> bool {
self.x.is_finite() && self.y.is_finite()
}
pub fn distance(self, other: Self) -> f32 {
((self.x - other.x).powi(2) + (self.y - other.y).powi(2)).sqrt()
}
pub fn distance_sq(self, other: Self) -> f32 {
(self.x - other.x).powi(2) + (self.y - other.y).powi(2)
}
pub fn length_sq(self) -> f32 {
self.x * self.x + self.y * self.y
}
pub fn dot(self, other: Self) -> f32 {
self.x * other.x + self.y * other.y
}
}
impl std::ops::Sub for ScreenPoint {
type Output = Self;
fn sub(self, other: Self) -> Self {
Self::new(self.x - other.x, self.y - other.y)
}
}
impl std::ops::Mul<f32> for ScreenPoint {
type Output = Self;
fn mul(self, factor: f32) -> Self {
Self::new(self.x * factor, self.y * factor)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ScreenRect {
pub min: ScreenPoint,
pub max: ScreenPoint,
}
impl ScreenRect {
pub fn from_min_max(a: ScreenPoint, b: ScreenPoint) -> Self {
Self {
min: ScreenPoint::new(a.x.min(b.x), a.y.min(b.y)),
max: ScreenPoint::new(a.x.max(b.x), a.y.max(b.y)),
}
}
pub fn from_min_size(min: ScreenPoint, width: f32, height: f32) -> Self {
Self {
min,
max: ScreenPoint::new(min.x + width, min.y + height),
}
}
pub fn left(self) -> f32 {
self.min.x
}
pub fn right(self) -> f32 {
self.max.x
}
pub fn top(self) -> f32 {
self.min.y
}
pub fn bottom(self) -> f32 {
self.max.y
}
pub fn width(self) -> f32 {
self.max.x - self.min.x
}
pub fn height(self) -> f32 {
self.max.y - self.min.y
}
pub fn is_positive(self) -> bool {
self.width() > 0.0 && self.height() > 0.0
}
pub fn contains(self, point: ScreenPoint) -> bool {
point.x >= self.min.x && point.x <= self.max.x && point.y >= self.min.y && point.y <= self.max.y
}
pub fn expand(self, amount: f32) -> Self {
Self {
min: ScreenPoint::new(self.min.x - amount, self.min.y - amount),
max: ScreenPoint::new(self.max.x + amount, self.max.y + amount),
}
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum YAxis {
#[default]
Left,
Right,
}
#[derive(Clone, Copy, Debug)]
pub struct Transform {
pub frame: ScreenRect,
pub view: Bounds,
pub x_scale: Scale,
pub y_scale: Scale,
pub y2: Option<SecondAxis>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SecondAxis {
pub min: f64,
pub max: f64,
pub scale: Scale,
}
impl Transform {
pub fn for_axis(self, axis: YAxis) -> Self {
match (axis, self.y2) {
(YAxis::Right, Some(y2)) => Self {
view: Bounds {
min_y: y2.min,
max_y: y2.max,
..self.view
},
y_scale: y2.scale,
..self
},
_ => self,
}
}
pub fn to_screen(self, point: [f64; 2]) -> ScreenPoint {
let x = self.x_scale.forward(point[0]);
let y = self.y_scale.forward(point[1]);
self.scaled_to_screen(x, y)
}
pub fn scaled_to_screen(&self, x: f64, y: f64) -> ScreenPoint {
let tx = (x - self.view.min_x) / self.view.width();
let ty = (y - self.view.min_y) / self.view.height();
ScreenPoint::new(
self.frame.left() + (tx * f64::from(self.frame.width())) as f32,
self.frame.bottom() - (ty * f64::from(self.frame.height())) as f32,
)
}
pub fn to_scaled(self, pos: ScreenPoint) -> [f64; 2] {
let tx = f64::from((pos.x - self.frame.left()) / self.frame.width());
let ty = f64::from((self.frame.bottom() - pos.y) / self.frame.height());
[
self.view.min_x + tx * self.view.width(),
self.view.min_y + ty * self.view.height(),
]
}
pub fn to_data(self, pos: ScreenPoint) -> [f64; 2] {
let [x, y] = self.to_scaled(pos);
[self.x_scale.inverse(x), self.y_scale.inverse(y)]
}
pub fn units_per_point(&self) -> [f64; 2] {
[
self.view.width() / f64::from(self.frame.width().max(1.0)),
self.view.height() / f64::from(self.frame.height().max(1.0)),
]
}
}