use super::*;
use std::vec::IntoIter;
mod constructors;
#[cfg_attr(feature = "serde", repr(C), derive(Serialize, Deserialize))]
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Square<T> {
pub x: T,
pub y: T,
pub s: T,
}
impl<T> Square<T> {
pub fn origin(&self) -> Point<&T>
where
T: Clone,
{
Point { x: &self.x, y: &self.y }
}
}
impl<T> Shape2D for Square<T>
where
T: Signed + Clone + PartialOrd,
{
type Value = T;
type VertexIterator<'a>
= IntoIter<Point<T>>where
T: 'a;
type LineIterator<'a>
= IntoIter<Line<T>>where
T: 'a;
fn is_valid(&self) -> bool {
self.s > T::zero()
}
fn normalize(&mut self) -> bool {
todo!()
}
fn boundary(&self) -> Rectangle<Self::Value> {
Rectangle {
min: Point { x: self.x.clone(), y: self.y.clone() },
max: Point { x: self.x.clone() + self.s.clone(), y: self.y.clone() + self.s.clone() },
}
}
fn vertices<'a>(&'a self, _: usize) -> Self::VertexIterator<'a> {
vec![
Point { x: self.x.clone(), y: self.y.clone() },
Point { x: self.x.clone() + self.s.clone(), y: self.y.clone() },
Point { x: self.x.clone() + self.s.clone(), y: self.y.clone() + self.s.clone() },
Point { x: self.x.clone(), y: self.y.clone() + self.s.clone() },
]
.into_iter()
}
fn edges<'a>(&'a self, _: usize) -> Self::LineIterator<'a> {
let mut start = Point { x: self.x.clone(), y: self.y.clone() };
let mut end = Point { x: self.x.clone() + self.s.clone(), y: self.y.clone() };
let mut out = Vec::with_capacity(4);
{
out.push(Line::new(start.clone(), end.clone()));
start = end;
end = Point { x: self.x.clone() + self.s.clone(), y: self.y.clone() + self.s.clone() };
out.push(Line::new(start.clone(), end.clone()));
start = end;
end = Point { x: self.x.clone(), y: self.y.clone() + self.s.clone() };
out.push(Line::new(start.clone(), end.clone()));
start = end;
end = Point { x: self.x.clone(), y: self.y.clone() };
out.push(Line::new(start, end));
};
out.into_iter()
}
}