use std::cmp;
use crate::data::errors::DivisionByZero;
#[derive(Debug, Clone, Copy)]
pub struct Slope2D {
pub from: (f32, f32),
pub to: (f32, f32),
pub is: f32,
}
impl Default for Slope2D {
fn default() -> Self {
Slope2D {
from: (0_f32, 0_f32),
to: (0_f32, 0_f32),
is: 0.0_f32,
}
}
}
impl Slope2D {
pub fn new(from: (f32, f32), to: (f32, f32)) -> Result<Slope2D, DivisionByZero> {
let delta_x = to.0 - from.0;
if delta_x == 0.0_f32 {
return Err(DivisionByZero);
}
let delta_y = to.1 - from.1;
let is = delta_y / delta_x;
Ok(Slope2D { from, to, is })
}
}
impl PartialEq for Slope2D {
fn eq(&self, other: &Self) -> bool {
self.is == other.is
}
}
impl Eq for Slope2D {}
impl PartialOrd for Slope2D {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
self.is.partial_cmp(&other.is)
}
}
impl Ord for Slope2D {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.is.total_cmp(&other.is)
}
}