use std::ops::{Deref, DerefMut};
use std::rc::Rc;
use super::bezier_curves::{CubicBezierCurve, QuadraticBezierCurve};
use super::curve::Curve;
use super::curve_path::{delegate_curve_path, CurvePath};
use super::ellipse_curve::EllipseCurve;
use super::line_curve::LineCurve;
use super::spline_curve::SplineCurve;
use crate::math::Vector2;
#[derive(Clone, Debug, Default)]
pub struct Path {
pub curve_path: CurvePath<Vector2>,
pub current_point: Vector2,
}
impl Deref for Path {
type Target = CurvePath<Vector2>;
fn deref(&self) -> &Self::Target {
&self.curve_path
}
}
impl DerefMut for Path {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.curve_path
}
}
delegate_curve_path!(Path, curve_path, Vector2, "Path");
impl Path {
pub fn new() -> Self {
Self::default()
}
pub fn from_points(points: &[Vector2]) -> Self {
let mut path = Self::new();
path.set_from_points(points);
path
}
pub fn set_from_points(&mut self, points: &[Vector2]) -> &mut Self {
self.move_to(points[0].x, points[0].y);
for point in &points[1..] {
self.line_to(point.x, point.y);
}
self
}
pub fn move_to(&mut self, x: f64, y: f64) -> &mut Self {
self.current_point.set(x, y);
self
}
pub fn line_to(&mut self, x: f64, y: f64) -> &mut Self {
let curve = LineCurve::new(self.current_point, Vector2::new(x, y));
self.curve_path.curves.push(Rc::new(curve));
self.current_point.set(x, y);
self
}
pub fn quadratic_curve_to(
&mut self,
a_c_px: f64,
a_c_py: f64,
a_x: f64,
a_y: f64,
) -> &mut Self {
let curve = QuadraticBezierCurve::new(
self.current_point,
Vector2::new(a_c_px, a_c_py),
Vector2::new(a_x, a_y),
);
self.curve_path.curves.push(Rc::new(curve));
self.current_point.set(a_x, a_y);
self
}
pub fn bezier_curve_to(
&mut self,
a_cp1x: f64,
a_cp1y: f64,
a_cp2x: f64,
a_cp2y: f64,
a_x: f64,
a_y: f64,
) -> &mut Self {
let curve = CubicBezierCurve::new(
self.current_point,
Vector2::new(a_cp1x, a_cp1y),
Vector2::new(a_cp2x, a_cp2y),
Vector2::new(a_x, a_y),
);
self.curve_path.curves.push(Rc::new(curve));
self.current_point.set(a_x, a_y);
self
}
pub fn spline_thru(&mut self, pts: &[Vector2]) -> &mut Self {
let mut npts = vec![self.current_point];
npts.extend_from_slice(pts);
let curve = SplineCurve::new(npts);
self.curve_path.curves.push(Rc::new(curve));
self.current_point.copy(&pts[pts.len() - 1]);
self
}
pub fn arc(
&mut self,
a_x: f64,
a_y: f64,
a_radius: f64,
a_start_angle: f64,
a_end_angle: f64,
a_clockwise: bool,
) -> &mut Self {
let x0 = self.current_point.x;
let y0 = self.current_point.y;
self.absarc(
a_x + x0,
a_y + y0,
a_radius,
a_start_angle,
a_end_angle,
a_clockwise,
);
self
}
pub fn absarc(
&mut self,
a_x: f64,
a_y: f64,
a_radius: f64,
a_start_angle: f64,
a_end_angle: f64,
a_clockwise: bool,
) -> &mut Self {
self.absellipse(
a_x,
a_y,
a_radius,
a_radius,
a_start_angle,
a_end_angle,
a_clockwise,
0.0,
);
self
}
#[allow(clippy::too_many_arguments)]
pub fn ellipse(
&mut self,
a_x: f64,
a_y: f64,
x_radius: f64,
y_radius: f64,
a_start_angle: f64,
a_end_angle: f64,
a_clockwise: bool,
a_rotation: f64,
) -> &mut Self {
let x0 = self.current_point.x;
let y0 = self.current_point.y;
self.absellipse(
a_x + x0,
a_y + y0,
x_radius,
y_radius,
a_start_angle,
a_end_angle,
a_clockwise,
a_rotation,
);
self
}
#[allow(clippy::too_many_arguments)]
pub fn absellipse(
&mut self,
a_x: f64,
a_y: f64,
x_radius: f64,
y_radius: f64,
a_start_angle: f64,
a_end_angle: f64,
a_clockwise: bool,
a_rotation: f64,
) -> &mut Self {
let curve = EllipseCurve::new(
a_x,
a_y,
x_radius,
y_radius,
a_start_angle,
a_end_angle,
a_clockwise,
a_rotation,
);
if !self.curve_path.curves.is_empty() {
let first_point = curve.get_point(0.0);
if !first_point.equals(&self.current_point) {
self.line_to(first_point.x, first_point.y);
}
}
let last_point = curve.get_point(1.0);
self.curve_path.curves.push(Rc::new(curve));
self.current_point.copy(&last_point);
self
}
}