Point

Struct Point 

Source
pub struct Point { /* private fields */ }
Expand description

Your garden-variety pair of f64s.

Points can be added, subtracted, and rescaled in various ways.

§Examples

use l_system_fractals::paths::Point;
use l_system_fractals::num_validity::AlmostEq;

let p1 = Point::new(1.5, 2.5);

assert!(p1.almost_eq(&Point::new(1.4999, 2.5001), 0.001));
assert!(!p1.almost_eq(&Point::new(1.4999, 2.5001), 0.000001));

let p2 = Point::new(2.0, 3.0);

assert!((p1 + p2).almost_eq(&Point::new(3.5, 5.5), 0.001));
assert!((p2 - p1).almost_eq(&Point::new(0.5, 0.5), 0.001));
assert!((5.0 * p2).almost_eq(&Point::new(10.0, 15.0), 0.001));

Implementations§

Source§

impl Point

Source

pub fn new(x: f64, y: f64) -> Self

Creates a new point with the given coordinates.

§Example
use l_system_fractals::paths::Point;

let p1 = Point::new(1.5, 2.5);
Source

pub fn is_valid(&self) -> bool

Returns true if each coordinate is neither NaN or ±∞.

§Examples
use l_system_fractals::paths::Point;

assert!(Point::new(2.5, -3.0).is_valid());
assert!(!Point::new(f64::NAN, -3.0).is_valid());
assert!(!Point::new(f64::INFINITY, -3.0).is_valid());
Source

pub fn err_if_invalid(&self) -> Result<Self, LSystemError>

Raises an error if either coordinate is NaN or ±∞.

The error raised is LSystemError::InvalidFloat.

§Examples
use l_system_fractals::paths::Point;

assert!(Point::new(2.5, -3.0).err_if_invalid().is_ok());
assert!(Point::new(f64::NAN, -3.0).err_if_invalid().is_err());
assert!(Point::new(f64::INFINITY, -3.0).err_if_invalid().is_err());
Source

pub fn from_polar(r: f64, theta: f64) -> Self

Creates a point with the specified polar coordinates.

§Examples
use std::f64::consts::PI;

use l_system_fractals::paths::Point;
use l_system_fractals::num_validity::AlmostEq;

let p1 = Point::from_polar(5.0, PI / 4.0);
let p2 = Point::from_polar(-5.0, 5.0 * PI / 4.0);
let p3 = Point::new(5.0 / 2.0_f64.sqrt(), 5.0 / 2.0_f64.sqrt());

assert!(p1.almost_eq(&p2, 0.001));
assert!(p1.almost_eq(&p3, 0.001));
Source

pub fn svg_output(&self) -> String

Returns a string for the coordinates for use in SVG paths.

§Example
use l_system_fractals::paths::Point;

assert_eq!(Point::new(1.25, 1.25).svg_output(), "1.25000,1.25000".to_string());
Source

pub fn delta_x(&self, other: &Self) -> f64

Returns the difference in horizontal coordinates.

§Examples
use std::f64::consts::PI;

use l_system_fractals::paths::Point;
use l_system_fractals::num_validity::AlmostEq;

let p1 = Point::new(5.0, 4.0);
let p2 = Point::new(3.0, 9.0);
assert!(p1.delta_x(&p2).almost_eq(&-2.0, 0.001));

let p3 = Point::from_polar(10.0, PI / 6.0);
assert!(p1.delta_x(&p3).almost_eq(&(5.0 * 3.0_f64.sqrt() - 5.0), 0.001));
Source

pub fn delta_y(&self, other: &Self) -> f64

Returns the difference in vertical coordinates.

§Examples
use std::f64::consts::PI;

use l_system_fractals::paths::Point;
use l_system_fractals::num_validity::AlmostEq;

let p1 = Point::new(5.0, 4.0);
let p2 = Point::new(3.0, 9.0);
assert!(p1.delta_y(&p2).almost_eq(&5.0, 0.001));

let p3 = Point::from_polar(10.0, PI / 3.0);
assert!(p1.delta_y(&p3).almost_eq(&(5.0 * 3.0_f64.sqrt() - 4.0), 0.001));
Source

pub fn delta(&self, other: &Self) -> Self

Returns a point that represents the vector between the two points.

This is equivalent to other - self.

§Examples
use std::f64::consts::PI;

use l_system_fractals::paths::Point;
use l_system_fractals::num_validity::AlmostEq;

let p1 = Point::new(5.0, 5.0);
let p2 = Point::new(3.0, 9.0);

assert!(p1.delta(&p2).almost_eq(&Point::new(-2.0, 4.0), 0.001));
assert_eq!(p1.delta(&p2), p2 - p1);

let p3 = Point::from_polar(10.0, PI / 3.0);
assert!(p1.delta(&p3).almost_eq(&Point::new(0.0, 5.0 * (3.0_f64.sqrt() - 1.0)), 0.001));
Source

pub fn angle(&self, other: &Self) -> f64

Returns the angle between the two points.

§Examples
use std::f64::consts::PI;

use l_system_fractals::paths::Point;
use l_system_fractals::num_validity::AlmostEq;

let p1 = Point::new(5.0, 5.0);
let p2 = Point::new(8.0, 8.0);

assert!(p1.angle(&p2).almost_eq(&(PI / 4.0), 0.001));

let p3 = Point::from_polar(1000.0, 1.23456);
let origin = Point::new(0.0, 0.0);
assert!(origin.angle(&p3).almost_eq(&1.23456, 0.0001));
Source

pub fn dist(&self, other: &Self) -> f64

Returns the distance between two points.

§Examples
use std::f64::consts::PI;

use l_system_fractals::paths::Point;
use l_system_fractals::num_validity::AlmostEq;

let p1 = Point::new(1.0, 2.0);
let p2 = Point::new(4.0, 6.0);

assert!(p1.dist(&p2).almost_eq(&5.0, 0.001));

let p3 = Point::from_polar(2024.0125, 1.23456);
let origin = Point::new(0.0, 0.0);
assert!(origin.dist(&p3).almost_eq(&2024.0125, 0.0001));
Source

pub fn third_points(&self, other: &Self) -> (Self, Self)

Returns a pair of points one-third and two-third of the way between the given points.

§Examples
use std::f64::consts::PI;

use l_system_fractals::paths::Point;
use l_system_fractals::num_validity::AlmostEq;

let p1 = Point::new(1.0, 1.0);
let p2 = Point::new(7.0, 16.0);

let (q1, q2) = p1.third_points(&p2);

assert!(q1.almost_eq(&Point::new(3.0, 6.0), 0.001));
assert!(q2.almost_eq(&Point::new(5.0, 11.0), 0.001));

let p3 = Point::from_polar(3000.0, 1.23456);
let origin = Point::new(0.0, 0.0);

let (q3, q4) = p3.third_points(&origin);

assert!(q3.almost_eq(&Point::from_polar(2000.0, 1.23456), 0.001));
assert!(q4.almost_eq(&Point::from_polar(1000.0, 1.23456), 0.001));
Source

pub fn rescale_vert(&self, factor: f64) -> Self

Multiply the vertical coordinate by the given factor.

§Example
use l_system_fractals::paths::Point;
use l_system_fractals::num_validity::AlmostEq;

let p1 = Point::new(1.0, 1.0);

assert!(p1.rescale_vert(32.1).almost_eq(&Point::new(1.0, 32.1), 0.001));
Source

pub fn rescale_horiz(&self, factor: f64) -> Self

Multiply the horizontal coordinate by the given factor.

§Example
use l_system_fractals::paths::Point;
use l_system_fractals::num_validity::AlmostEq;

let p1 = Point::new(1.0, 1.0);

assert!(p1.rescale_horiz(32.1).almost_eq(&Point::new(32.1, 1.0), 0.001));
Source

pub fn rotate_about_origin(&self, angle: f64) -> Self

Rotate the point about the origin by the given angle.

§Examples
use std::f64::consts::PI;

use l_system_fractals::paths::Point;
use l_system_fractals::num_validity::AlmostEq;

let p1 = Point::new(3.0, 4.0);
assert!(p1.rotate_about_origin(PI / 2.0).almost_eq(&Point::new(-4.0, 3.0), 0.001));

let p2 = Point::new(1.0, 1.0);
assert!(p2.rotate_about_origin(PI/4.0).almost_eq(
         &Point::new(0.0, 2.0_f64.sqrt()),
         0.001
));
Source

pub fn rotate(&self, axis: &Point, angle: f64) -> Self

Rotate the point about another by the given angle.

§Examples
use std::f64::consts::PI;

use l_system_fractals::paths::Point;
use l_system_fractals::num_validity::AlmostEq;

let p1 = Point::new(4.0, 5.0);
let p2 = Point::new(1.0, 1.0);
assert!(p1.rotate(&p2, PI / 2.0).almost_eq(&Point::new(-3.0, 4.0), 0.001));

Trait Implementations§

Source§

impl Add<Path> for Point

Source§

type Output = Path

The resulting type after applying the + operator.
Source§

fn add(self, other: Path) -> Self::Output

Performs the + operation. Read more
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 Path

Source§

type Output = Path

The resulting type after applying the + operator.
Source§

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

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 Add for Point

Source§

type Output = Point

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl AlmostEq for Point

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 Point

Source§

fn clone(&self) -> Point

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 Point

Source§

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

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

impl Mul<Point> for f64

Source§

type Output = Point

The resulting type after applying the * operator.
Source§

fn mul(self, other: Point) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<f64> for Point

Source§

type Output = Point

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl PartialEq for Point

Source§

fn eq(&self, other: &Point) -> 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 Path

Source§

type Output = Path

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
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 Sub for Point

Source§

type Output = Point

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self

Performs the - operation. Read more
Source§

impl Copy for Point

Source§

impl StructuralPartialEq for Point

Auto Trait Implementations§

§

impl Freeze for Point

§

impl RefUnwindSafe for Point

§

impl Send for Point

§

impl Sync for Point

§

impl Unpin for Point

§

impl UnwindSafe for Point

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.