[][src]Struct turtle::Point

pub struct Point {
    pub x: f64,
    pub y: f64,
}

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 example deliberately fails to compile
// 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

Methods

impl Point[src]

pub fn origin() -> Self[src]

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

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

pub fn is_finite(self) -> bool[src]

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

pub fn is_normal(self) -> bool[src]

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

pub fn is_not_normal(self) -> bool[src]

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

pub fn abs(self) -> Self[src]

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

pub fn round(self) -> Self[src]

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.

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

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

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

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

pub fn square_len(self) -> f64[src]

Returns the square of the length of this point.

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

pub fn len(self) -> f64[src]

Returns the length of this point.

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

pub fn atan2(self) -> f64[src]

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

Trait Implementations

impl Random for Point[src]

impl<B: Into<Point>> RandomRange<B> for Point[src]

impl From<(f64, f64)> for Point[src]

impl From<[f64; 2]> for Point[src]

impl From<Point> for [f64; 2][src]

impl Clone for Point[src]

impl Copy for Point[src]

impl PartialEq<Point> for Point[src]

impl Debug for Point[src]

impl Div<f64> for Point[src]

type Output = Self

The resulting type after applying the / operator.

impl Sub<Point> for Point[src]

type Output = Self

The resulting type after applying the - operator.

impl Add<Point> for Point[src]

type Output = Self

The resulting type after applying the + operator.

impl Mul<f64> for Point[src]

type Output = Self

The resulting type after applying the * operator.

impl Index<usize> for Point[src]

type Output = f64

The returned type after indexing.

impl IndexMut<usize> for Point[src]

impl StructuralPartialEq for Point[src]

impl Lerp for Point[src]

type Scalar = f64

The scaling type for linear interpolation.

impl Serialize for Point[src]

impl<'de> Deserialize<'de> for Point[src]

Auto Trait Implementations

impl Send for Point

impl Sync for Point

impl Unpin for Point

impl UnwindSafe for Point

impl RefUnwindSafe for Point

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = !

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

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

impl<T> SetParameter for T