pub struct Point<T>(pub Coordinate<T>)
where
T: CoordinateType;
Expand description
A single Point in 2D space.
Points can be created using the new(x, y)
constructor, or from a Coordinate
or pair of points.
Examples
use geo_types::{Point, Coordinate};
let p1: Point<f64> = (0., 1.).into();
let c = Coordinate{ x: 10., y: 20.};
let p2: Point<f64> = c.into();
Tuple Fields
0: Coordinate<T>
Implementations
sourceimpl<T> Point<T>where
T: CoordinateType,
impl<T> Point<T>where
T: CoordinateType,
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
pub fn lng(self) -> T
Returns the longitude/horizontal component of the point.
Examples
use geo_types::Point;
let p = Point::new(1.234, 2.345);
assert_eq!(p.lng(), 1.234);
sourcepub fn set_lng(&mut self, lng: T) -> &mut Point<T>
pub fn set_lng(&mut self, lng: T) -> &mut Point<T>
Sets the longitude/horizontal component of the point.
Examples
use geo_types::Point;
let mut p = Point::new(1.234, 2.345);
p.set_lng(9.876);
assert_eq!(p.lng(), 9.876);
sourceimpl<T> Point<T>where
T: CoordinateType,
impl<T> Point<T>where
T: CoordinateType,
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::{Coordinate, Point};
let point = Point(Coordinate { x: 1.5, y: 0.5 });
let dot = point.dot(Point(Coordinate { 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.
Examples
use geo_types::{Coordinate, Point};
let point_a = Point(Coordinate { x: 1., y: 2. });
let point_b = Point(Coordinate { x: 3., y: 5. });
let point_c = Point(Coordinate { 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: CoordinateType + Float,
impl<T> Point<T>where
T: CoordinateType + Float,
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);