use crate::Vec2;
#[cfg(not(feature = "std"))]
use num_traits::float::Float as _;
use crate::color::ColorType;
#[derive(Copy, Clone)]
pub struct Led<Color: ColorType> {
pub color: Color,
position: Vec2,
angle: f32,
distance: f32,
index: u16,
segment: u8,
}
impl<Color: ColorType> Led<Color> {
pub(crate) fn new(
color: Color,
position: Vec2,
index: u16,
segment: u8,
center_point: Vec2,
) -> Self {
let offset = position - center_point;
let angle = offset.y.atan2(offset.x);
let distance = offset.length();
Led {
color,
position,
angle,
distance,
index,
segment,
}
}
pub fn position(&self) -> Vec2 {
self.position
}
pub fn direction(&self) -> Vec2 {
Vec2::new(self.angle.cos(), self.angle.sin())
}
pub fn angle(&self) -> f32 {
self.angle
}
pub fn distance(&self) -> f32 {
self.distance
}
pub fn index(&self) -> u16 {
self.index
}
pub fn segment(&self) -> u8 {
self.segment
}
}
impl<Color: ColorType> PartialEq for Led<Color> {
fn eq(&self, other: &Self) -> bool {
self.index() == other.index()
}
}
impl<Color: ColorType> Eq for Led<Color> {}
impl<Color: ColorType> PartialOrd for Led<Color> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<Color: ColorType> Ord for Led<Color> {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.index.cmp(&other.index())
}
}
impl<Color: ColorType> core::hash::Hash for Led<Color> {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.index.hash(state);
}
}
impl<Color: ColorType> core::fmt::Debug for Led<Color> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let dir = self.direction();
f.debug_struct("Led")
.field("color", &self.color)
.field("position", &(self.position.x, self.position.y))
.field("direction", &(dir.x, dir.y))
.field("angle", &self.angle)
.field("distance", &self.distance)
.field("index", &self.index)
.field("segment", &self.segment)
.finish()
}
}
impl<Color: ColorType> core::fmt::Display for Led<Color> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}: {:?}", self.index, self.color)
}
}