#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct GraphPoint<T>(pub T, pub T);
impl<T> From<(T, T)> for GraphPoint<T> {
fn from((x, y): (T, T)) -> GraphPoint<T> {
GraphPoint(x, y)
}
}
impl<T> GraphPoint<T>
where
T: Into<f64> + Copy,
{
pub fn distance(self, other: GraphPoint<T>) -> f64 {
(self.0.into() - other.0.into()).hypot(self.1.into() - other.1.into())
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct CanvasPoint<T>(pub T, pub T);
impl<T> From<(T, T)> for CanvasPoint<T> {
fn from((x, y): (T, T)) -> CanvasPoint<T> {
CanvasPoint(x, y)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Point<T> {
pub coordinates: GraphPoint<T>,
pub color: (f64, f64, f64),
pub label: Option<String>,
}
impl Default for Point<f64> {
fn default() -> Point<f64> {
Point {
coordinates: (0.0, 0.0).into(),
color: (1.0, 0.0, 0.0),
label: None,
}
}
}
impl<T> Point<T>
where
T: Into<f64> + Copy,
Point<T>: Default,
{
pub fn new(coordinates: impl Into<GraphPoint<T>>) -> Point<T> {
Point {
coordinates: coordinates.into(),
..Default::default()
}
}
pub fn with_color(mut self, color: (f64, f64, f64)) -> Point<T> {
self.color = color;
self
}
pub fn with_label(mut self, label: String) -> Point<T> {
self.label = Some(label);
self
}
}
impl<T> From<(T, T)> for Point<T>
where
Point<T>: Default,
{
fn from((x, y): (T, T)) -> Point<T> {
Point {
coordinates: GraphPoint(x, y),
..Default::default()
}
}
}