Struct geo_types::geometry::Point

source ·
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>

source

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

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

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

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

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

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

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

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

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

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

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

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

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

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

impl<T: CoordNum> Point<T>

source

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

pub fn cross_prod(self, point_b: Self, point_c: Self) -> 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)
source§

impl<T: CoordFloat> Point<T>

source

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

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> Add for Point<T>

source§

fn add(self, rhs: Self) -> Self::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);
source§

type Output = Point<T>

The resulting type after applying the + operator.
source§

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

source§

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: Clone + CoordNum> Clone for Point<T>

source§

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

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

source§

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

Formats the value using the given formatter. Read more
source§

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

source§

fn default() -> Point<T>

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

impl<'de, T> Deserialize<'de> for Point<T>
where T: Deserialize<'de> + CoordNum,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

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

source§

fn div(self, rhs: T) -> Self::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);
source§

type Output = Point<T>

The resulting type after applying the / operator.
source§

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

source§

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> From<[T; 2]> for Point<T>

source§

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

Converts to this type from the input type.
source§

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

source§

fn from(coords: (T, T)) -> Self

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

impl<T: CoordNum> From<Point<T>> for Geometry<T>

source§

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

Converts to this type from the input type.
source§

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

source§

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

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

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

source§

fn mul(self, rhs: T) -> Self::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);
source§

type Output = Point<T>

The resulting type after applying the * operator.
source§

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

source§

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> Neg for Point<T>
where T: CoordNum + Neg<Output = T>,

source§

fn neg(self) -> Self::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);
source§

type Output = Point<T>

The resulting type after applying the - operator.
source§

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

source§

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

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

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

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

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

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

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

source§

fn sub(self, rhs: Self) -> Self::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);
source§

type Output = Point<T>

The resulting type after applying the - operator.
source§

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

source§

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>

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.

source§

type Error = Error

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

fn try_from(geom: Geometry<T>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T: Copy + CoordNum> Copy for Point<T>

source§

impl<T: Eq + CoordNum> Eq for Point<T>

source§

impl<T: CoordNum> StructuralPartialEq for Point<T>

Auto Trait Implementations§

§

impl<T> Freeze for Point<T>
where T: Freeze,

§

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

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

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

source§

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

source§

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

source§

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

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