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
impl Point
Sourcepub fn new(x: f64, y: f64) -> Self
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);Sourcepub fn is_valid(&self) -> bool
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());
Sourcepub fn err_if_invalid(&self) -> Result<Self, LSystemError>
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());Sourcepub fn from_polar(r: f64, theta: f64) -> Self
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));Sourcepub fn svg_output(&self) -> String
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());Sourcepub fn delta_x(&self, other: &Self) -> f64
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));Sourcepub fn delta_y(&self, other: &Self) -> f64
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));Sourcepub fn delta(&self, other: &Self) -> Self
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));Sourcepub fn angle(&self, other: &Self) -> f64
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));Sourcepub fn dist(&self, other: &Self) -> f64
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));Sourcepub fn third_points(&self, other: &Self) -> (Self, Self)
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));Sourcepub fn rescale_vert(&self, factor: f64) -> Self
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));Sourcepub fn rescale_horiz(&self, factor: f64) -> Self
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));Sourcepub fn rotate_about_origin(&self, angle: f64) -> Self
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
));Sourcepub fn rotate(&self, axis: &Point, angle: f64) -> Self
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<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 more