Struct turtle::Point [−][src]
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
let pt: Point = random(); assert!(pt.x >= 0.0 && pt.x < 1.0); assert!(pt.y >= 0.0 && pt.y < 1.0);
See the documentation for the rand
module and the section
Generating Random Values in a Range
for more information.
Fields
x: f64
The x-coordinate of the Point
y: f64
The y-coordinate of the Point
Methods
impl Point
[src]
impl Point
pub fn origin() -> Self
[src]
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)
pub fn is_finite(self) -> bool
[src]
pub fn is_finite(self) -> bool
Returns true if both x and y are finite (neither infinite nor NaN
).
pub fn is_normal(self) -> bool
[src]
pub fn is_normal(self) -> bool
Returns true if both x and y are neither zero, infinite,
subnormal, or NaN
.
pub fn is_not_normal(self) -> bool
[src]
pub fn is_not_normal(self) -> bool
Returns true if both x and y are either zero, infinite,
subnormal, or NaN
.
pub fn abs(self) -> Self
[src]
pub fn abs(self) -> Self
Computes the absolute value of x and y and returns a new Point
pub fn round(self) -> Self
[src]
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.
pub fn min(self, other: Self) -> Self
[src]
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});
pub fn max(self, other: Self) -> Self
[src]
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});
pub fn square_len(self) -> f64
[src]
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)
pub fn len(self) -> f64
[src]
pub fn len(self) -> f64
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]
pub fn atan2(self) -> f64
Computes the four quadrant arctangent of self.y
and self.x
.
Trait Implementations
impl Debug for Point
[src]
impl Debug for Point
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl Clone for Point
[src]
impl Clone for Point
fn clone(&self) -> Point
[src]
fn clone(&self) -> Point
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl Copy for Point
[src]
impl Copy for Point
impl PartialEq for Point
[src]
impl PartialEq for Point
fn eq(&self, other: &Point) -> bool
[src]
fn eq(&self, other: &Point) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Point) -> bool
[src]
fn ne(&self, other: &Point) -> bool
This method tests for !=
.
impl From<(f64, f64)> for Point
[src]
impl From<(f64, f64)> for Point
impl From<[f64; 2]> for Point
[src]
impl From<[f64; 2]> for Point
impl From<Point> for [f64; 2]
[src]
impl From<Point> for [f64; 2]
impl Add for Point
[src]
impl Add for Point
type Output = Self
The resulting type after applying the +
operator.
fn add(self, other: Self) -> Self::Output
[src]
fn add(self, other: Self) -> Self::Output
Performs the +
operation.
impl Sub for Point
[src]
impl Sub for Point
type Output = Self
The resulting type after applying the -
operator.
fn sub(self, other: Self) -> Self::Output
[src]
fn sub(self, other: Self) -> Self::Output
Performs the -
operation.
impl Mul<f64> for Point
[src]
impl Mul<f64> for Point
type Output = Self
The resulting type after applying the *
operator.
fn mul(self, other: f64) -> Self::Output
[src]
fn mul(self, other: f64) -> Self::Output
Performs the *
operation.
impl Div<f64> for Point
[src]
impl Div<f64> for Point
type Output = Self
The resulting type after applying the /
operator.
fn div(self, other: f64) -> Self::Output
[src]
fn div(self, other: f64) -> Self::Output
Performs the /
operation.
impl Index<usize> for Point
[src]
impl Index<usize> for Point
type Output = f64
The returned type after indexing.
fn index(&self, index: usize) -> &Self::Output
[src]
fn index(&self, index: usize) -> &Self::Output
Performs the indexing (container[index]
) operation.
impl IndexMut<usize> for Point
[src]
impl IndexMut<usize> for Point
fn index_mut(&mut self, index: usize) -> &mut Self::Output
[src]
fn index_mut(&mut self, index: usize) -> &mut Self::Output
Performs the mutable indexing (container[index]
) operation.
impl Spatial for Point
[src]
impl Spatial for Point
type Scalar = f64
The scalar type.
fn add(&self, other: &Self) -> Self
[src]
fn add(&self, other: &Self) -> Self
Add
fn sub(&self, other: &Self) -> Self
[src]
fn sub(&self, other: &Self) -> Self
Subtract
fn scale(&self, scalar: &Self::Scalar) -> Self
[src]
fn scale(&self, scalar: &Self::Scalar) -> Self
Scales with a scalar.
impl Distribution<Point> for Standard
[src]
impl Distribution<Point> for Standard
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Point
[src]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Point
Generate a random value of T
, using rng
as the source of randomness.
ⓘImportant traits for DistIter<'a, D, R, T>fn sample_iter<R>(&'a self, rng: &'a mut R) -> DistIter<'a, Self, R, T> where
R: Rng,
[src]
fn sample_iter<R>(&'a self, rng: &'a mut R) -> DistIter<'a, Self, R, T> where
R: Rng,
Create an iterator that generates random values of T
, using rng
as the source of randomness. Read more
impl RandomRange for Point
[src]
impl RandomRange for Point
fn random_range<R: Rng>(rng: &mut R, p1: Self, p2: Self) -> Self
[src]
fn random_range<R: Rng>(rng: &mut R, p1: Self, p2: Self) -> Self