pub enum PathCommand {
LineTo(Point),
MoveTo(Point),
}Expand description
Represents one of the two SVG path
commands implemented by this
crate (L and M).
Path is a wrapper of Vec<PathCommand>.
§Examples
use l_system_fractals::paths::{Path, PathCommand, Point};
use l_system_fractals::num_validity::AlmostEq;
let pth: Path = vec![
Point::new(1.0, 2.0),
Point::new(5.0, 3.0),
Point::new(2.0, 4.0)
].into();
let pcv: Vec<PathCommand> = pth.0;
assert!(pcv.get(0).unwrap().almost_eq(&PathCommand::MoveTo(Point::new(1.0, 2.0)), 0.001));
assert!(pcv.get(1).unwrap().almost_eq(&PathCommand::LineTo(Point::new(5.0, 3.0)), 0.001));
assert!(pcv.get(2).unwrap().almost_eq(&PathCommand::LineTo(Point::new(2.0, 4.0)), 0.001));Adding a Point to a PathCommand will add the underlying points. Likewise
subtracting a Point from a PathCommand subtracts the underlying points.
use l_system_fractals::paths::{PathCommand, Point};
use l_system_fractals::num_validity::AlmostEq;
let pc1 = PathCommand::LineTo(Point::new(1.0, 2.0));
let pc2 = pc1 + Point::new(1.0, -1.0);
assert!(pc2.point().almost_eq(&Point::new(2.0, 1.0), 0.001));
let pc3 = pc1 - Point::new(0.0, 0.5);
assert!(pc3.point().almost_eq(&Point::new(1.0, 1.5), 0.001));
let pc4 = Point::new(5.0, 4.0) + pc1;
assert!(pc4.point().almost_eq(&Point::new(6.0, 6.0), 0.001));Likewise, multiplying a PathCommand by a f64 factor will multiply the
coordinates of the underlying point.
use l_system_fractals::paths::{PathCommand, Point};
use l_system_fractals::num_validity::AlmostEq;
let pc1 = PathCommand::LineTo(Point::new(1.0, 2.0));
let pc2 = pc1 * 2.0;
assert!(pc2.point().almost_eq(&Point::new(2.0, 4.0), 0.001));Variants§
LineTo(Point)
An instruction to draw a line segment to the specified point (corresponds to L in SVG
path
syntax).
§Example
use l_system_fractals::paths::{PathCommand, Point};
let pt = Point::new(3.0, 2.0);
assert_eq!(PathCommand::LineTo(pt).svg_output(), "L 3.00000,2.00000".to_string());MoveTo(Point)
An instruction to move to the specified point without drawing (corresponds to M in SVG
path
syntax).
§Example
use l_system_fractals::paths::{PathCommand, Point};
let pt = Point::new(3.0, 2.0);
assert_eq!(PathCommand::MoveTo(pt).svg_output(), "M 3.00000,2.00000".to_string());Implementations§
Source§impl PathCommand
impl PathCommand
Sourcepub fn is_valid(&self) -> bool
pub fn is_valid(&self) -> bool
Returns true if the underlying Point is valid (has no
coordinates that are NaN or ±∞).
§Examples
use l_system_fractals::paths::{PathCommand, Point};
assert!(PathCommand::LineTo(Point::new(2.5, -3.0)).is_valid());
assert!(!PathCommand::MoveTo(Point::new(f64::NAN, -3.0)).is_valid());
assert!(!PathCommand::LineTo(Point::new(f64::INFINITY, -3.0)).is_valid());
Sourcepub fn err_if_invalid(self) -> Result<Self, LSystemError>
pub fn err_if_invalid(self) -> Result<Self, LSystemError>
Raises an error if the underlying Point is invalid (either
coordinate is NaN or ±∞).
The error raised is LSystemError::InvalidFloat.
§Examples
use l_system_fractals::paths::{PathCommand, Point};
assert!(PathCommand::LineTo(Point::new(2.5, -3.0)).err_if_invalid().is_ok());
assert!(PathCommand::MoveTo(Point::new(f64::NAN, -3.0)).err_if_invalid().is_err());
assert!(PathCommand::LineTo(Point::new(f64::INFINITY, -3.0)).err_if_invalid().is_err());Sourcepub fn svg_output(&self) -> String
pub fn svg_output(&self) -> String
Returns the SVG path
syntax for the path command.
§Examples
use l_system_fractals::paths::{PathCommand, Point};
let pt = Point::new(3.0, 2.0);
let pc1 = PathCommand::MoveTo(pt);
let pc2 = PathCommand::LineTo(pt);
assert_eq!(pc1.svg_output(), "M 3.00000,2.00000".to_string());
assert_eq!(pc2.svg_output(), "L 3.00000,2.00000".to_string());Sourcepub fn same_type(&self, pt: Point) -> Self
pub fn same_type(&self, pt: Point) -> Self
Returns a PathCommand of the same type (LineTo or
MoveTo) but with the specified Point.
§Examples
use l_system_fractals::paths::{PathCommand, Point};
let pt1 = Point::new(3.0, 2.0);
let pt2 = Point::new(4.0, 1.0);
let pc1 = PathCommand::LineTo(pt1);
let pc2 = PathCommand::LineTo(pt2);
assert_eq!(pc1.same_type(pt2), pc2);Sourcepub fn rescale_horiz(&self, factor: f64) -> Self
pub fn rescale_horiz(&self, factor: f64) -> Self
Multiplies the horizontal coordinate of the point in the PathCommand by the specified factor.
§Examples
use l_system_fractals::paths::{PathCommand, Point};
use l_system_fractals::num_validity::AlmostEq;
let pt1 = Point::new(3.0, 2.0);
let pt2 = pt1.rescale_horiz(8.125);
assert!(pt2.almost_eq(&Point::new(24.375, 2.0), 0.001));
let pc1 = PathCommand::LineTo(pt1);
let pc2 = pc1.rescale_horiz(8.125);
assert_eq!(PathCommand::LineTo(pt2), pc2);Sourcepub fn rescale_vert(&self, factor: f64) -> Self
pub fn rescale_vert(&self, factor: f64) -> Self
Multiplies the vertical coordinate of the point in the PathCommand by the specified factor.
§Examples
use l_system_fractals::paths::{PathCommand, Point};
use l_system_fractals::num_validity::AlmostEq;
let pt1 = Point::new(3.0, 2.0);
let pt2 = pt1.rescale_vert(8.125);
assert!(pt2.almost_eq(&Point::new(3.0, 16.25), 0.001));
let pc1 = PathCommand::MoveTo(pt1);
let pc2 = pc1.rescale_vert(8.125);
assert_eq!(PathCommand::MoveTo(pt2), pc2);Sourcepub fn rotate_about_origin(&self, angle: f64) -> Self
pub fn rotate_about_origin(&self, angle: f64) -> Self
Returns a PathCommand of the same type (LineTo or MoveTo) but with the underlying
point having been rotated about the origin by the specified angle.
§Examples
use std::f64::consts::PI;
use l_system_fractals::paths::{PathCommand, Point};
use l_system_fractals::num_validity::AlmostEq;
let pt1 = Point::new(1.0, 1.0);
let pt2 = pt1.rotate_about_origin(0.25 * PI);
assert!(pt2.almost_eq(&Point::new(0.0, 2.0_f64.sqrt()), 0.001));
let pc1 = PathCommand::MoveTo(pt1);
let pc2 = pc1.rotate_about_origin(0.25 * PI);
assert_eq!(PathCommand::MoveTo(pt2), pc2);Sourcepub fn rotate(&self, axis: &Point, angle: f64) -> Self
pub fn rotate(&self, axis: &Point, angle: f64) -> Self
Returns a PathCommand of the same type (LineTo or MoveTo) but with the underlying
point having been rotated about the specified point by the specified angle.
§Examples
use std::f64::consts::PI;
use l_system_fractals::paths::{PathCommand, Point};
use l_system_fractals::num_validity::AlmostEq;
let pt1 = Point::new(3.0, 5.0);
let ax = Point::new(2.0, 4.0);
let pt2 = pt1.rotate(&ax, 0.25 * PI);
assert!(pt2.almost_eq(&Point::new(2.0, 4.0 + 2.0_f64.sqrt()), 0.001));
let pc1 = PathCommand::MoveTo(pt1);
let pc2 = pc1.rotate(&ax, 0.25 * PI);
assert_eq!(PathCommand::MoveTo(pt2), pc2);Trait Implementations§
Source§impl Add<PathCommand> for Point
impl Add<PathCommand> for Point
Source§type Output = PathCommand
type Output = PathCommand
+ operator.Source§fn add(self, pc: PathCommand) -> PathCommand
fn add(self, pc: PathCommand) -> PathCommand
+ operation. Read moreSource§impl Add<Point> for PathCommand
impl Add<Point> for PathCommand
Source§impl AlmostEq for PathCommand
impl AlmostEq for PathCommand
Source§impl Clone for PathCommand
impl Clone for PathCommand
Source§fn clone(&self) -> PathCommand
fn clone(&self) -> PathCommand
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for PathCommand
impl Debug for PathCommand
Source§impl Mul<PathCommand> for f64
impl Mul<PathCommand> for f64
Source§type Output = PathCommand
type Output = PathCommand
* operator.Source§fn mul(self, pc: PathCommand) -> PathCommand
fn mul(self, pc: PathCommand) -> PathCommand
* operation. Read more