Struct Point

Source
pub struct Point {
    pub x: f64,
    pub y: f64,
}
Expand description

A point in 2D space

§Creating a Point

Methods like Turtle::go_to(), Turtle::turn_towards() and Drawing::set_center() all support any type that can be converted into Point. That means that you can pass the same value into those methods in several different ways depending on what you prefer:

// These are equivalent
turtle.go_to([100.0, 40.0]);
turtle.go_to((100.0, 40.0));
// This is equivalent too, but the above examples are easier to type
use turtle::Point;
turtle.go_to(Point {x: 100.0, y: 40.0});

Each of these different styles works because the methods call .into() internally.

assert_eq!(Point {x: 100.0, y: 40.0}, [100.0, 40.0].into());
assert_eq!(Point {x: 100.0, y: 40.0}, (100.0, 40.0).into());

Notice that we need to convert the right side using into() before it can be compared in assert_eq!().

// This will not compile
assert_eq!(Point {x: 100.0, y: 40.0}, [100.0, 40.0]);
assert_eq!(Point {x: 100.0, y: 40.0}, (100.0, 40.0));

§Manipulating Points

You can add or subtract points and multiply or divide points by scalar (f64) values. There are a variety of method functions described in the documentation below that provide even more operations.

// Let's say you have two points with f64 values a, b, c, and d
let pt = Point {x: a, y: b};
let pt2 = Point {x: c, y: d};
assert_eq!(pt + pt2, Point {x: a + c, y: b + d});
assert_eq!(pt - pt2, Point {x: a - c, y: b - d});
assert_eq!(pt * 2.0, Point {x: a * 2.0, y: b * 2.0});
assert_eq!(pt2 / 5.0, Point {x: c / 5.0, y: d / 5.0});
assert_eq!(pt2 * 2.0 - pt, Point {x: c * 2.0 - a, y: d * 2.0 - b});

§Accessing Point Components

Point supports either using the x and y fields to access its components or using indexing if you prefer that style.

let p = Point {x: 100.0, y: 120.0};

// Get x coordinate
let x = p.x;
assert_eq!(x, 100.0);
assert_eq!(p[0], x);

// Get y coordinate
let y = p.y;
assert_eq!(y, 120.0);
assert_eq!(p[1], y);

// With pattern matching
let Point {x, y} = p;
assert_eq!(x, 100.0);
assert_eq!(y, 120.0);

// Modifying x and y
let mut pt: Point = [240.0, 430.0].into();
pt.x = 73.0;
pt.y = 89.0;
assert_eq!(pt.x, 73.0);
assert_eq!(pt.y, 89.0);
// Using indexing
let mut pt2: Point = [100.0, 200.0].into();
pt2[0] = pt.x;
pt2[1] = pt.y;
assert_eq!(pt2.x, 73.0);
assert_eq!(pt2.y, 89.0);

§Generating Random Points

Use the random() function to generate random points. The values of x and y will be between 0.0 and 1.0 (inclusive).

use turtle::{Point, rand::random};

let pt: Point = random();
assert!(pt.x >= 0.0 && pt.x <= 1.0);
assert!(pt.y >= 0.0 && pt.y <= 1.0);

When random_range() is used to generate a Point, it creates a random point within the rectangle formed by the two points given as arguments to random_range().

use turtle::{Point, rand::random_range};

// Generates a Point value with:
//   x-coordinate between 46.0 and 92.0
//   y-coordinate between 39.0 and 103.0
let value: Point = random_range::<_, Point>([92.0, 39.0].into(), [46.0, 103.0].into());
assert!(value.x >= 46.0 && value.x <= 92.0);
assert!(value.y >= 39.0 && value.y <= 103.0);

Fields§

§x: f64

The x-coordinate of the Point

§y: f64

The y-coordinate of the Point

Implementations§

Source§

impl Point

Source

pub fn origin() -> Self

Returns a Point that represents the origin of the coordinate system.

For our “cartesian” coordinate system, this is always (0.0, 0.0)

Source

pub fn is_finite(self) -> bool

Returns true if both x and y are finite (neither infinite nor NaN).

Source

pub fn is_normal(self) -> bool

Returns true if both x and y are neither zero, infinite, subnormal, or NaN.

Source

pub fn is_not_normal(self) -> bool

Returns true if both x and y are either zero, infinite, subnormal, or NaN.

Source

pub fn abs(self) -> Self

Computes the absolute value of x and y and returns a new Point

Source

pub fn round(self) -> Self

Returns a new Point with x and y set to the nearest integer to each of their values. Rounds half-way cases away from 0.0.

Source

pub fn min(self, other: Self) -> Self

Returns the minimum x and y coordinates of the two points

let p1 = Point {x: 100.0, y: 203.18};
let p2 = Point {x: 3.0, y: 1029.677};
assert_eq!(p1.min(p2), Point {x: 3.0, y: 203.18});
Source

pub fn max(self, other: Self) -> Self

Returns the maximum x and y coordinates of the two points

let p1 = Point {x: 100.0, y: 203.18};
let p2 = Point {x: 3.0, y: 1029.677};
assert_eq!(p1.max(p2), Point {x: 100.0, y: 1029.677});
Source

pub fn square_len(self) -> f64

Returns the square of the length of this point.

The length of a point is defined as sqrt(x^2 + y^2)

Source

pub fn len(self) -> f64

Returns the length of this point.

The length of a point is defined as sqrt(x^2 + y^2)

Source

pub fn atan2(self) -> f64

Computes the four quadrant arctangent of self.y and self.x.

Trait Implementations§

Source§

impl Add for Point

Source§

type Output = Point

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Clone for Point

Source§

fn clone(&self) -> Point

Returns a copy 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<'de> Deserialize<'de> for Point

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Div<f64> for Point

Source§

type Output = Point

The resulting type after applying the / operator.
Source§

fn div(self, other: f64) -> Self::Output

Performs the / operation. Read more
Source§

impl From<[f64; 2]> for Point

Source§

fn from(pt: [f64; 2]) -> Self

Converts to this type from the input type.
Source§

impl From<(f64, f64)> for Point

Source§

fn from(pt: (f64, f64)) -> Self

Converts to this type from the input type.
Source§

impl From<Point> for [f64; 2]

Source§

fn from(pt: Point) -> Self

Converts to this type from the input type.
Source§

impl Index<usize> for Point

Source§

type Output = f64

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<usize> for Point

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl Lerp for Point

Source§

type Scalar = f64

The scaling type for linear interpolation.
Source§

fn lerp(&self, other: &Self, scalar: &Self::Scalar) -> Self

Given self and another point other, return a point on a line running between the two that is scalar fraction of the distance between the two points.
Source§

impl Mul<f64> for Point

Source§

type Output = Point

The resulting type after applying the * operator.
Source§

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

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

Source§

fn random() -> Self

Generate a single random value. Read more
Source§

impl<B: Into<Point>> RandomRange<B> for Point

Source§

fn random_range(p1: B, p2: B) -> Self

Generate a single random value in the given range. The value x that is returned will be such that low ≤ x ≤ high. Read more
Source§

impl Serialize for Point

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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::Output

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> SetParameter for T

Source§

fn set<T>(&mut self, value: T) -> <T as Parameter<Self>>::Result
where T: Parameter<Self>,

Sets value as a parameter of self.
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,