pub struct Point<T = f64>(pub Coordinate<T>)
where
T: CoordNum;
Expand description
A single point in 2D space.
Points can be created using the Point::new
constructor,
the point!
macro, or from a Coordinate
, 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 Coordinate
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: Coordinate<T>
Implementations
sourceimpl<T> Point<T> where
T: CoordNum,
impl<T> Point<T> where
T: CoordNum,
sourcepub fn new(x: T, y: T) -> Point<T>
pub fn new(x: T, y: T) -> Point<T>
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 Point<T>
pub fn set_x(&mut self, x: T) -> &mut Point<T>
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 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 Point<T>
pub fn set_y(&mut self, y: T) -> &mut Point<T>
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 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
use Point::x
instead, it’s less ambiguous
Returns 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 Point<T>
👎 Deprecated: use Point::set_x
instead, it’s less ambiguous
pub fn set_lng(&mut self, lng: T) -> &mut Point<T>
use Point::set_x
instead, it’s less ambiguous
Sets 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);
sourcepub fn lat(self) -> T
👎 Deprecated: use Point::y
instead, it’s less ambiguous
pub fn lat(self) -> T
use Point::y
instead, it’s less ambiguous
Returns the latitude/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_lat(&mut self, lat: T) -> &mut Point<T>
👎 Deprecated: use Point::set_y
instead, it’s less ambiguous
pub fn set_lat(&mut self, lat: T) -> &mut Point<T>
use Point::set_y
instead, it’s less ambiguous
Sets the latitude/vertical component of the point.
Examples
use geo_types::Point;
let mut p = Point::new(1.234, 2.345);
#[allow(deprecated)]
p.set_lat(9.876);
assert_eq!(p.y(), 9.876);
sourceimpl<T> Point<T> where
T: CoordNum,
impl<T> Point<T> where
T: CoordNum,
sourcepub fn dot(self, other: Point<T>) -> T
pub fn dot(self, other: Point<T>) -> 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: Point<T>, point_c: Point<T>) -> T
pub fn cross_prod(self, point_b: Point<T>, point_c: Point<T>) -> 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)
sourceimpl<T> Point<T> where
T: CoordFloat,
impl<T> Point<T> where
T: CoordFloat,
sourcepub fn to_degrees(self) -> Point<T>
pub fn to_degrees(self) -> Point<T>
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) -> Point<T>
pub fn to_radians(self) -> Point<T>
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
sourceimpl<T> Add<Point<T>> for Point<T> where
T: CoordNum,
impl<T> Add<Point<T>> for Point<T> where
T: CoordNum,
sourceimpl<T> AddAssign<Point<T>> for Point<T> where
T: CoordNum,
impl<T> AddAssign<Point<T>> for Point<T> where
T: CoordNum,
sourcefn add_assign(&mut self, rhs: Point<T>)
fn add_assign(&mut self, rhs: Point<T>)
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);
sourceimpl<T> DivAssign<T> for Point<T> where
T: CoordNum,
impl<T> DivAssign<T> for Point<T> where
T: CoordNum,
sourcefn 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);
sourceimpl<T> From<Coordinate<T>> for Point<T> where
T: CoordNum,
impl<T> From<Coordinate<T>> for Point<T> where
T: CoordNum,
sourcefn from(x: Coordinate<T>) -> Point<T>
fn from(x: Coordinate<T>) -> Point<T>
Converts to this type from the input type.
sourceimpl<T> From<Point<T>> for Coordinate<T> where
T: CoordNum,
impl<T> From<Point<T>> for Coordinate<T> where
T: CoordNum,
sourcefn from(point: Point<T>) -> Coordinate<T>
fn from(point: Point<T>) -> Coordinate<T>
Converts to this type from the input type.
sourceimpl<T> MulAssign<T> for Point<T> where
T: CoordNum,
impl<T> MulAssign<T> for Point<T> where
T: CoordNum,
sourcefn 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);
sourceimpl<T> Sub<Point<T>> for Point<T> where
T: CoordNum,
impl<T> Sub<Point<T>> for Point<T> where
T: CoordNum,
sourceimpl<T> SubAssign<Point<T>> for Point<T> where
T: CoordNum,
impl<T> SubAssign<Point<T>> for Point<T> where
T: CoordNum,
sourcefn sub_assign(&mut self, rhs: Point<T>)
fn sub_assign(&mut self, rhs: Point<T>)
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);
sourceimpl<T> TryFrom<Geometry<T>> for Point<T> where
T: CoordNum,
impl<T> TryFrom<Geometry<T>> for Point<T> where
T: CoordNum,
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.
impl<T> Copy for Point<T> where
T: Copy + CoordNum,
impl<T> Eq for Point<T> where
T: Eq + CoordNum,
impl<T> StructuralEq for Point<T> where
T: CoordNum,
impl<T> StructuralPartialEq for Point<T> where
T: CoordNum,
Auto Trait Implementations
impl<T> RefUnwindSafe for Point<T> where
T: RefUnwindSafe,
impl<T> Send for Point<T> where
T: Send,
impl<T> Sync for Point<T> where
T: Sync,
impl<T> Unpin for Point<T> where
T: Unpin,
impl<T> UnwindSafe for Point<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to key
and return true
if they are equal.
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more