use crate::{color, Point};
use crate::draw::shape::vertex::Vertex;
use crate::widget::primitive::shape::triangles::ColoredPoint;
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Triangle<V>(pub [V; 3])
where V: Vertex;
impl<V> Triangle<V>
where V: Vertex,
{
pub fn add(self, amount: Point) -> Self {
let a = self[0].add(amount);
let b = self[1].add(amount);
let c = self[2].add(amount);
Triangle([a, b, c])
}
pub fn points(self) -> [Point; 3] {
[self[0].point(), self[1].point(), self[2].point()]
}
}
impl Triangle<Point> {
pub fn color(self, a: color::Rgba, b: color::Rgba, c: color::Rgba) -> Triangle<ColoredPoint> {
Triangle([(self[0], a), (self[1], b), (self[2], c)])
}
pub fn color_all(self, color: color::Rgba) -> Triangle<ColoredPoint> {
Triangle([(self[0], color), (self[1], color), (self[2], color)])
}
pub fn from_point_list(points: Vec<Point>) -> Vec<Triangle<Point>> {
let len = points.len();
if len == 0 {
return vec![]
}
if len % 3 != 0 {
panic!("The vector of points can not be converted to a list of triangles because its length is not divisible by three")
}
let mut res = vec![];
for i in (0..len).step_by(3) {
res.push((points[i], points[i+1], points[i+2]).into())
}
res
}
}
impl<V> std::ops::Deref for Triangle<V>
where V: Vertex,
{
type Target = [V; 3];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<V> From<[V; 3]> for Triangle<V>
where V: Vertex,
{
fn from(points: [V; 3]) -> Self {
Triangle(points)
}
}
impl<V> From<(V, V, V)> for Triangle<V>
where V: Vertex,
{
fn from((a, b, c): (V, V, V)) -> Self {
Triangle([a, b, c])
}
}
impl<V> Into<[V; 3]> for Triangle<V>
where V: Vertex,
{
fn into(self) -> [V; 3] {
self.0
}
}
impl<V> Into<(V, V, V)> for Triangle<V>
where V: Vertex,
{
fn into(self) -> (V, V, V) {
(self[0], self[1], self[2])
}
}