pub struct Point<T = f64>(pub Coord<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 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§

§

impl<T> Point<T>
where T: CoordNum,

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

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

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

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

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

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

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

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

pub fn lng(self) -> T

👎Deprecated: 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);

pub fn set_lng(&mut self, lng: T) -> &mut Point<T>

👎Deprecated: 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);

pub fn lat(self) -> T

👎Deprecated: 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);

pub fn set_lat(&mut self, lat: T) -> &mut Point<T>

👎Deprecated: 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);
§

impl<T> Point<T>
where T: CoordNum,

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

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 selfpoint_bpoint_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)
§

impl<T> Point<T>
where T: CoordFloat,

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

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§

§

impl<T> AbsDiffEq for Point<T>
where T: AbsDiffEq<Epsilon = T> + CoordNum, <T as AbsDiffEq>::Epsilon: Copy,

§

fn abs_diff_eq( &self, other: &Point<T>, epsilon: <Point<T> as AbsDiffEq>::Epsilon ) -> bool

Equality assertion with an absolute limit.

Examples
use geo_types::Point;

let a = Point::new(2.0, 3.0);
let b = Point::new(2.0, 3.0000001);

approx::assert_relative_eq!(a, b, epsilon=0.1)
§

type Epsilon = <T as AbsDiffEq>::Epsilon

Used for specifying relative comparisons.
§

fn default_epsilon() -> <Point<T> as AbsDiffEq>::Epsilon

The default tolerance to use when testing values that are close together. Read more
§

fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool

The inverse of [AbsDiffEq::abs_diff_eq].
§

impl<T> Add for Point<T>
where T: CoordNum,

§

fn add(self, rhs: Point<T>) -> <Point<T> as Add>::Output

Add a point to the given point.

Examples
use geo_types::Point;

let p = Point::new(1.25, 2.5) + Point::new(1.5, 2.5);

assert_eq!(p.x(), 2.75);
assert_eq!(p.y(), 5.0);
§

type Output = Point<T>

The resulting type after applying the + operator.
§

impl<T> AddAssign for Point<T>
where T: CoordNum,

§

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

impl<T> Clone for Point<T>
where T: Clone + CoordNum,

§

fn clone(&self) -> Point<T>

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
§

impl<T> Debug for Point<T>
where T: Debug + CoordNum,

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<T> Default for Point<T>
where T: Default + CoordNum,

§

fn default() -> Point<T>

Returns the “default value” for a type. Read more
§

impl<T> Div<T> for Point<T>
where T: CoordNum,

§

fn div(self, rhs: T) -> <Point<T> as Div<T>>::Output

Scaler division of a point

Examples
use geo_types::Point;

let p = Point::new(2.0, 3.0) / 2.0;

assert_eq!(p.x(), 1.0);
assert_eq!(p.y(), 1.5);
§

type Output = Point<T>

The resulting type after applying the / operator.
§

impl<T> DivAssign<T> for Point<T>
where T: CoordNum,

§

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

impl<T> From<[T; 2]> for Point<T>
where T: CoordNum,

§

fn from(coords: [T; 2]) -> Point<T>

Converts to this type from the input type.
§

impl<T> From<(T, T)> for Point<T>
where T: CoordNum,

§

fn from(coords: (T, T)) -> Point<T>

Converts to this type from the input type.
§

impl<T> From<Coord<T>> for Point<T>
where T: CoordNum,

§

fn from(x: Coord<T>) -> Point<T>

Converts to this type from the input type.
§

impl From<GeoPoint> for Point

§

fn from(field: GeoPoint) -> Point

Converts to this type from the input type.
§

impl<T> From<Point<T>> for Coord<T>
where T: CoordNum,

§

fn from(point: Point<T>) -> Coord<T>

Converts to this type from the input type.
§

impl From<Point> for GeoPoint

§

fn from(field: Point) -> GeoPoint

Converts to this type from the input type.
§

impl<T> Hash for Point<T>
where T: Hash + CoordNum,

§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
§

impl<T> Mul<T> for Point<T>
where T: CoordNum,

§

fn mul(self, rhs: T) -> <Point<T> as Mul<T>>::Output

Scaler multiplication of a point

Examples
use geo_types::Point;

let p = Point::new(2.0, 3.0) * 2.0;

assert_eq!(p.x(), 4.0);
assert_eq!(p.y(), 6.0);
§

type Output = Point<T>

The resulting type after applying the * operator.
§

impl<T> MulAssign<T> for Point<T>
where T: CoordNum,

§

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

impl<T> Neg for Point<T>
where T: CoordNum + Neg<Output = T>,

§

fn neg(self) -> <Point<T> as Neg>::Output

Returns a point with the x and y components negated.

Examples
use geo_types::Point;

let p = -Point::new(-1.25, 2.5);

assert_eq!(p.x(), 1.25);
assert_eq!(p.y(), -2.5);
§

type Output = Point<T>

The resulting type after applying the - operator.
§

impl<T> PartialEq for Point<T>
where T: PartialEq + CoordNum,

§

fn eq(&self, other: &Point<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl<T> Point for Point<T>
where T: Float + RTreeNum,

§

type Scalar = T

The number type used by this point type.
§

const DIMENSIONS: usize = 2usize

The number of dimensions of this point type.
§

fn generate( generator: impl FnMut(usize) -> <Point<T> as Point>::Scalar ) -> Point<T>

Creates a new point value with given values for each dimension. Read more
§

fn nth(&self, index: usize) -> <Point<T> as Point>::Scalar

Returns a single coordinate of this point. Read more
§

fn nth_mut(&mut self, index: usize) -> &mut <Point<T> as Point>::Scalar

Mutable variant of nth.
§

impl<T> RelativeEq for Point<T>
where T: AbsDiffEq<Epsilon = T> + CoordNum + RelativeEq,

§

fn relative_eq( &self, other: &Point<T>, epsilon: <Point<T> as AbsDiffEq>::Epsilon, max_relative: <Point<T> as AbsDiffEq>::Epsilon ) -> bool

Equality assertion within a relative limit.

Examples
use geo_types::Point;

let a = Point::new(2.0, 3.0);
let b = Point::new(2.0, 3.01);

approx::assert_relative_eq!(a, b, max_relative=0.1)
§

fn default_max_relative() -> <Point<T> as AbsDiffEq>::Epsilon

The default relative tolerance for testing values that are far-apart. Read more
§

fn relative_ne( &self, other: &Rhs, epsilon: Self::Epsilon, max_relative: Self::Epsilon ) -> bool

The inverse of [RelativeEq::relative_eq].
§

impl<T> Sub for Point<T>
where T: CoordNum,

§

fn sub(self, rhs: Point<T>) -> <Point<T> as Sub>::Output

Subtract a point from the given point.

Examples
use geo_types::Point;

let p = Point::new(1.25, 3.0) - Point::new(1.5, 2.5);

assert_eq!(p.x(), -0.25);
assert_eq!(p.y(), 0.5);
§

type Output = Point<T>

The resulting type after applying the - operator.
§

impl<T> SubAssign for Point<T>
where T: CoordNum,

§

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

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.

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from( geom: Geometry<T> ) -> Result<Point<T>, <Point<T> as TryFrom<Geometry<T>>>::Error>

Performs the conversion.
§

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§

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
§

impl<T> CallHasher for T
where T: Hash + ?Sized,

§

fn get_hash<H, B>(value: &H, build_hasher: &B) -> u64
where H: Hash + ?Sized, B: BuildHasher,

source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoRequest<T> for T

source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<P> PointDistance for P
where P: Point,

§

fn distance_2(&self, point: &P) -> <P as Point>::Scalar

Returns the squared euclidean distance between an object to a point.
§

fn contains_point( &self, point: &<<P as RTreeObject>::Envelope as Envelope>::Point ) -> bool

Returns true if a point is contained within this object. Read more
§

fn distance_2_if_less_or_equal( &self, point: &<<P as RTreeObject>::Envelope as Envelope>::Point, max_distance_2: <<<P as RTreeObject>::Envelope as Envelope>::Point as Point>::Scalar ) -> Option<<P as Point>::Scalar>

Returns the squared distance to this object, or None if the distance is larger than a given maximum value. Read more
§

impl<P> RTreeObject for P
where P: Point,

§

type Envelope = AABB<P>

The object’s envelope type. Usually, [AABB] will be the right choice. This type also defines the object’s dimensionality.
§

fn envelope(&self) -> AABB<P>

Returns the object’s envelope. Read more
source§

impl<T> ToOwned for T
where T: Clone,

§

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>,

§

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>,

§

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more