pub struct Point<T: CoordNum = f64>(pub Coord<T>);
Expand description
A single point in 2D space.
Points can be created using the Point::new
constructor,
the point!
macro, or from a Coord
, two-element
tuples, or arrays – see the From
impl section for a
complete list.
§Semantics
The interior of the point is itself (a singleton set),
and its boundary is empty. A point is valid if and
only if the Coord
is valid.
§Examples
use geo_types::{coord, Point};
let p1: Point = (0., 1.).into();
let c = coord! { x: 10., y: 20. };
let p2: Point = c.into();
Tuple Fields§
§0: Coord<T>
Implementations§
Source§impl<T: CoordNum> Point<T>
impl<T: CoordNum> Point<T>
Sourcepub fn new(x: T, y: T) -> Self
pub fn new(x: T, y: T) -> Self
Creates a new point.
§Examples
use geo_types::Point;
let p = Point::new(1.234, 2.345);
assert_eq!(p.x(), 1.234);
assert_eq!(p.y(), 2.345);
Sourcepub fn x(self) -> T
pub fn x(self) -> T
Returns the x/horizontal component of the point.
§Examples
use geo_types::Point;
let p = Point::new(1.234, 2.345);
assert_eq!(p.x(), 1.234);
Sourcepub fn set_x(&mut self, x: T) -> &mut Self
pub fn set_x(&mut self, x: T) -> &mut Self
Sets the x/horizontal component of the point.
§Examples
use geo_types::Point;
let mut p = Point::new(1.234, 2.345);
p.set_x(9.876);
assert_eq!(p.x(), 9.876);
Sourcepub fn x_mut(&mut self) -> &mut T
pub fn x_mut(&mut self) -> &mut T
Returns a mutable reference to the x/horizontal component of the point
§Examples
use approx::assert_relative_eq;
use geo_types::Point;
let mut p = Point::new(1.234, 2.345);
let mut p_x = p.x_mut();
*p_x += 1.0;
assert_relative_eq!(p.x(), 2.234);
Sourcepub fn y(self) -> T
pub fn y(self) -> T
Returns the y/vertical component of the point.
§Examples
use geo_types::Point;
let p = Point::new(1.234, 2.345);
assert_eq!(p.y(), 2.345);
Sourcepub fn set_y(&mut self, y: T) -> &mut Self
pub fn set_y(&mut self, y: T) -> &mut Self
Sets the y/vertical component of the point.
§Examples
use geo_types::Point;
let mut p = Point::new(1.234, 2.345);
p.set_y(9.876);
assert_eq!(p.y(), 9.876);
Sourcepub fn y_mut(&mut self) -> &mut T
pub fn y_mut(&mut self) -> &mut T
Returns a mutable reference to the x/horizontal component of the point
§Examples
use approx::assert_relative_eq;
use geo_types::Point;
let mut p = Point::new(1.234, 2.345);
let mut p_y = p.y_mut();
*p_y += 1.0;
assert_relative_eq!(p.y(), 3.345);
Sourcepub fn x_y(self) -> (T, T)
pub fn x_y(self) -> (T, T)
Returns a tuple that contains the x/horizontal & y/vertical component of the point.
§Examples
use geo_types::Point;
let mut p = Point::new(1.234, 2.345);
let (x, y) = p.x_y();
assert_eq!(y, 2.345);
assert_eq!(x, 1.234);
Sourcepub fn lng(self) -> T
👎Deprecated: use Point::x
instead, it’s less ambiguous
pub fn lng(self) -> T
Point::x
instead, it’s less ambiguousReturns the longitude/horizontal component of the point.
§Examples
use geo_types::Point;
let p = Point::new(1.234, 2.345);
assert_eq!(p.x(), 1.234);
Sourcepub fn set_lng(&mut self, lng: T) -> &mut Self
👎Deprecated: use Point::set_x
instead, it’s less ambiguous
pub fn set_lng(&mut self, lng: T) -> &mut Self
Point::set_x
instead, it’s less ambiguousSets the longitude/horizontal component of the point.
§Examples
use geo_types::Point;
let mut p = Point::new(1.234, 2.345);
#[allow(deprecated)]
p.set_lng(9.876);
assert_eq!(p.x(), 9.876);
Source§impl<T: CoordNum> Point<T>
impl<T: CoordNum> Point<T>
Sourcepub fn dot(self, other: Self) -> T
pub fn dot(self, other: Self) -> T
Returns the dot product of the two points:
dot = x1 * x2 + y1 * y2
§Examples
use geo_types::{point, Point};
let point = point! { x: 1.5, y: 0.5 };
let dot = point.dot(point! { x: 2.0, y: 4.5 });
assert_eq!(dot, 5.25);
Sourcepub fn cross_prod(self, point_b: Self, point_c: Self) -> T
pub fn cross_prod(self, point_b: Self, point_c: Self) -> T
Returns the cross product of 3 points. A positive value implies
self
→ point_b
→ point_c
is counter-clockwise, negative implies
clockwise.
§Note on Robustness
This function is not robust against floating-point errors.
The geo
crate
offers robust predicates for standard numeric types using the
Kernel
trait, and these should be preferred if possible.
§Examples
use geo_types::point;
let point_a = point! { x: 1., y: 2. };
let point_b = point! { x: 3., y: 5. };
let point_c = point! { x: 7., y: 12. };
let cross = point_a.cross_prod(point_b, point_c);
assert_eq!(cross, 2.0)
Source§impl<T: CoordFloat> Point<T>
impl<T: CoordFloat> Point<T>
Sourcepub fn to_degrees(self) -> Self
pub fn to_degrees(self) -> Self
Converts the (x,y) components of Point to degrees
§Example
use geo_types::Point;
let p = Point::new(1.234, 2.345);
let (x, y): (f32, f32) = p.to_degrees().x_y();
assert_eq!(x.round(), 71.0);
assert_eq!(y.round(), 134.0);
Sourcepub fn to_radians(self) -> Self
pub fn to_radians(self) -> Self
Converts the (x,y) components of Point to radians
§Example
use geo_types::Point;
let p = Point::new(180.0, 341.5);
let (x, y): (f32, f32) = p.to_radians().x_y();
assert_eq!(x.round(), 3.0);
assert_eq!(y.round(), 6.0);
Trait Implementations§
Source§impl<T: CoordNum> AddAssign for Point<T>
impl<T: CoordNum> AddAssign for Point<T>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Add a point to the given point and assign it to the original point.
§Examples
use geo_types::Point;
let mut p = Point::new(1.25, 2.5);
p += Point::new(1.5, 2.5);
assert_eq!(p.x(), 2.75);
assert_eq!(p.y(), 5.0);
Source§impl<T: CoordNum> DivAssign<T> for Point<T>
impl<T: CoordNum> DivAssign<T> for Point<T>
Source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
Scaler division of a point in place
§Examples
use geo_types::Point;
let mut p = Point::new(2.0, 3.0);
p /= 2.0;
assert_eq!(p.x(), 1.0);
assert_eq!(p.y(), 1.5);
Source§impl<T: CoordNum> MulAssign<T> for Point<T>
impl<T: CoordNum> MulAssign<T> for Point<T>
Source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
Scaler multiplication of a point in place
§Examples
use geo_types::Point;
let mut p = Point::new(2.0, 3.0);
p *= 2.0;
assert_eq!(p.x(), 4.0);
assert_eq!(p.y(), 6.0);
Source§impl<T: CoordNum> SubAssign for Point<T>
impl<T: CoordNum> SubAssign for Point<T>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Subtract a point from the given point and assign it to the original point.
§Examples
use geo_types::Point;
let mut p = Point::new(1.25, 2.5);
p -= Point::new(1.5, 2.5);
assert_eq!(p.x(), -0.25);
assert_eq!(p.y(), 0.0);
Source§impl<T: CoordNum> TryFrom<Geometry<T>> for Point<T>
impl<T: CoordNum> TryFrom<Geometry<T>> for Point<T>
Convert a Geometry enum into its inner type.
Fails if the enum case does not match the type you are trying to convert it to.