PathCommand

Enum PathCommand 

Source
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

Source

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());
Source

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());
Source

pub fn point(&self) -> Point

Returns the underlying Point of the 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.point(), pt);
assert_eq!(pc2.point(), pt);
Source

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());
Source

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);
Source

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);
Source

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);
Source

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);
Source

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

Source§

type Output = PathCommand

The resulting type after applying the + operator.
Source§

fn add(self, pc: PathCommand) -> PathCommand

Performs the + operation. Read more
Source§

impl Add<Point> for PathCommand

Source§

type Output = PathCommand

The resulting type after applying the + operator.
Source§

fn add(self, p: Point) -> Self

Performs the + operation. Read more
Source§

impl AlmostEq for PathCommand

Source§

fn almost_eq(&self, other: &Self, epsilon: f64) -> bool

Returns true if the distance between objects is less than epsilon.
Source§

impl Clone for PathCommand

Source§

fn clone(&self) -> PathCommand

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PathCommand

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Mul<PathCommand> for f64

Source§

type Output = PathCommand

The resulting type after applying the * operator.
Source§

fn mul(self, pc: PathCommand) -> PathCommand

Performs the * operation. Read more
Source§

impl Mul<f64> for PathCommand

Source§

type Output = PathCommand

The resulting type after applying the * operator.
Source§

fn mul(self, x: f64) -> Self

Performs the * operation. Read more
Source§

impl PartialEq for PathCommand

Source§

fn eq(&self, other: &PathCommand) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Sub<Point> for PathCommand

Source§

type Output = PathCommand

The resulting type after applying the - operator.
Source§

fn sub(self, p: Point) -> Self

Performs the - operation. Read more
Source§

impl Copy for PathCommand

Source§

impl StructuralPartialEq for PathCommand

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.