use std::f64::consts::PI;
use std::{
iter,
ops::{Add, Mul, Sub},
};
use crate::{Affine, Arc, ArcAppendIter, Circle, PathEl, Point, Rect, Shape, Size, Vec2};
#[derive(Clone, Copy, Default, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Ellipse {
inner: Affine,
}
impl Ellipse {
#[inline]
pub fn new(center: impl Into<Point>, radii: impl Into<Vec2>, x_rotation: f64) -> Ellipse {
let Point { x: cx, y: cy } = center.into();
let Vec2 { x: rx, y: ry } = radii.into();
Ellipse::private_new(Vec2 { x: cx, y: cy }, rx, ry, x_rotation)
}
#[inline]
pub fn from_rect(rect: Rect) -> Self {
let center = rect.center().to_vec2();
let Size { width, height } = rect.size() / 2.0;
Ellipse::private_new(center, width, height, 0.0)
}
#[inline]
pub fn from_affine(affine: Affine) -> Self {
Ellipse { inner: affine }
}
#[inline]
fn private_new(center: Vec2, scale_x: f64, scale_y: f64, x_rotation: f64) -> Ellipse {
Ellipse {
inner: Affine::translate(center)
* Affine::rotate(x_rotation)
* Affine::scale_non_uniform(scale_x.abs(), scale_y.abs()),
}
}
#[inline]
pub fn center(&self) -> Point {
let Vec2 { x: cx, y: cy } = self.inner.get_translation();
Point { x: cx, y: cy }
}
#[inline]
pub fn with_center(self, new_center: Point) -> Ellipse {
let Point { x: cx, y: cy } = new_center;
Ellipse {
inner: self.inner.set_translation(Vec2 { x: cx, y: cy }),
}
}
pub fn radii(&self) -> Vec2 {
self.inner.svd().0
}
#[must_use]
pub fn with_radii(self, new_radii: Vec2) -> Ellipse {
let rotation = self.inner.svd().1;
let translation = self.inner.get_translation();
Ellipse::private_new(translation, new_radii.x, new_radii.y, rotation)
}
pub fn x_rotation(&self) -> f64 {
self.inner.svd().1
}
pub fn with_x_rotation(self, new_x_rotation: f64) -> Ellipse {
let scale = self.inner.svd().0;
let translation = self.inner.get_translation();
Ellipse::private_new(translation, scale.x, scale.y, new_x_rotation)
}
}
impl Add<Vec2> for Ellipse {
type Output = Ellipse;
#[inline]
#[allow(clippy::suspicious_arithmetic_impl)]
fn add(self, v: Vec2) -> Ellipse {
Ellipse {
inner: Affine::translate(v) * self.inner,
}
}
}
impl Sub<Vec2> for Ellipse {
type Output = Ellipse;
#[inline]
fn sub(self, v: Vec2) -> Ellipse {
Ellipse {
inner: Affine::translate(-v) * self.inner,
}
}
}
impl Mul<Ellipse> for Affine {
type Output = Ellipse;
fn mul(self, other: Ellipse) -> Self::Output {
Ellipse {
inner: self * other.inner,
}
}
}
impl From<Circle> for Ellipse {
fn from(circle: Circle) -> Self {
Ellipse::new(circle.center, Vec2::splat(circle.radius), 0.0)
}
}
impl Shape for Ellipse {
type BezPathIter = iter::Chain<iter::Once<PathEl>, ArcAppendIter>;
fn to_bez_path(&self, tolerance: f64) -> Self::BezPathIter {
let (radii, x_rotation) = self.inner.svd();
Arc {
center: self.center(),
radii,
start_angle: 0.0,
sweep_angle: 2.0 * PI,
x_rotation,
}
.to_bez_path(tolerance)
}
#[inline]
fn area(&self) -> f64 {
let Vec2 { x, y } = self.radii();
PI * x * y
}
#[inline]
fn perimeter(&self, accuracy: f64) -> f64 {
self.clone()
.into_bez_path(0.1)
.elements()
.perimeter(accuracy)
}
fn winding(&self, pt: Point) -> i32 {
let inv = self.inner.inverse();
if (inv * pt).to_vec2().hypot2() < 1.0 {
1
} else {
0
}
}
#[inline]
fn bounding_box(&self) -> Rect {
let aff = self.inner.as_coeffs();
let a2 = aff[0] * aff[0];
let b2 = aff[1] * aff[1];
let c2 = aff[2] * aff[2];
let d2 = aff[3] * aff[3];
let cx = aff[4];
let cy = aff[5];
let range_x = (a2 + c2).sqrt();
let range_y = (b2 + d2).sqrt();
Rect {
x0: cx - range_x,
y0: cy - range_y,
x1: cx + range_x,
y1: cy + range_y,
}
}
}
#[cfg(test)]
mod tests {
use crate::{Ellipse, Point, Shape};
use std::f64::consts::PI;
fn assert_approx_eq(x: f64, y: f64) {
assert!((x - y).abs() < 1e-7, "{} != {}", x, y);
}
#[test]
fn area_sign() {
let center = Point::new(5.0, 5.0);
let e = Ellipse::new(center, (5.0, 5.0), 1.0);
assert_approx_eq(e.area(), 25.0 * PI);
let e = Ellipse::new(center, (5.0, 10.0), 1.0);
assert_approx_eq(e.area(), 50.0 * PI);
assert_eq!(e.winding(center), 1);
let p = e.into_bez_path(1e-9);
assert_approx_eq(e.area(), p.area());
assert_eq!(e.winding(center), p.winding(center));
let e_neg_radius = Ellipse::new(center, (-5.0, 10.0), 1.0);
assert_approx_eq(e_neg_radius.area(), 50.0 * PI);
assert_eq!(e_neg_radius.winding(center), 1);
let p_neg_radius = e_neg_radius.into_bez_path(1e-9);
assert_approx_eq(e_neg_radius.area(), p_neg_radius.area());
assert_eq!(e_neg_radius.winding(center), p_neg_radius.winding(center));
}
}