pub use crate::rectangle::{centered, centered_square as circle};
use crate::{
math::Matrix2d,
triangulation,
types::{Color, Radius, Rectangle, Resolution},
DrawState, Graphics,
};
#[derive(Copy, Clone)]
pub struct Border {
pub color: Color,
pub radius: Radius,
}
#[derive(Copy, Clone)]
pub struct Ellipse {
pub color: Color,
pub border: Option<Border>,
pub resolution: Resolution,
}
impl Ellipse {
pub fn new(color: Color) -> Ellipse {
Ellipse {
color,
border: None,
resolution: 128,
}
}
pub fn new_border(color: Color, radius: Radius) -> Ellipse {
Ellipse {
color: [0.0; 4],
border: Some(Border { color, radius }),
resolution: 128,
}
}
pub fn color(mut self, value: Color) -> Self {
self.color = value;
self
}
pub fn border(mut self, value: Border) -> Self {
self.border = Some(value);
self
}
pub fn maybe_border(mut self, value: Option<Border>) -> Self {
self.border = value;
self
}
pub fn resolution(mut self, value: Resolution) -> Self {
self.resolution = value;
self
}
#[inline(always)]
pub fn draw_from_to<P: Into<crate::types::Vec2d>, G>(
&self,
from: P,
to: P,
draw_state: &DrawState,
transform: Matrix2d,
g: &mut G,
) where
G: Graphics,
{
use crate::rectangle::rectangle_by_corners;
let from = from.into();
let to = to.into();
g.ellipse(
self,
rectangle_by_corners(from[0], from[1], to[0], to[1]),
draw_state,
transform,
);
}
#[inline(always)]
pub fn draw<R: Into<Rectangle>, G>(
&self,
rectangle: R,
draw_state: &DrawState,
transform: Matrix2d,
g: &mut G,
) where
G: Graphics,
{
g.ellipse(self, rectangle, draw_state, transform);
}
pub fn draw_tri<R: Into<Rectangle>, G>(
&self,
rectangle: R,
draw_state: &DrawState,
transform: Matrix2d,
g: &mut G,
) where
G: Graphics,
{
let rectangle = rectangle.into();
g.tri_list(draw_state, &self.color, |f| {
triangulation::with_ellipse_tri_list(
self.resolution,
transform,
rectangle,
|vertices| f(vertices),
)
});
if let Some(Border {
color,
radius: border_radius,
}) = self.border
{
g.tri_list(draw_state, &color, |f| {
triangulation::with_ellipse_border_tri_list(
self.resolution,
transform,
rectangle,
border_radius,
|vertices| f(vertices),
)
});
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_ellipse() {
let _ellipse = Ellipse::new([1.0; 4]).color([0.0; 4]).border(Border {
color: [1.0; 4],
radius: 3.0,
});
}
}