Skip to main content

Point

Struct Point 

Source
pub struct Point {
    pub x: i16,
    pub y: i16,
}
Expand description

A point on the integer lattice.

i16 is the crate’s coordinate type for both input and output.

§The coordinate cap

Coordinates are restricted to Point::MIN_COORD..=Point::MAX_COORD, i.e. -16384..=16383 — half of what i16 could hold. Polygon rejects anything outside it.

That one bit is what pays for everything else. It is exactly the bit that lets the crate compute the whole skeleton in i32 and f32, with no f64 and no i64 in the algorithm:

  • The orientation determinant of three points needs 2 * d^2 where d is the largest coordinate difference. At the full i16 range that is 8_589_672_450 — it overflows i32 and reports the wrong side. Capped, it is 2_147_352_578, which fits i32 with 131_069 to spare, making every predicate exact. See crate::predicates.
  • f32 resolves 0.002 at the cap, against 0.004 at the full range, which is what leaves the simulation enough room to work in.

§Rounding

A straight skeleton’s interior nodes generally land on irrational coordinates even when every input vertex is an integer, so there is no lattice to compute on. The algorithm works internally in f32 and rounds only at the boundary, so a Node’s position is the nearest lattice point to its true location. When you need the unrounded value, every node also carries Node::exact.

§Examples

use straight_skeleton::Point;

let p = Point::new(3, 4);
assert_eq!(p.x, 3);
assert_eq!(p.y, 4);

// Points convert from the obvious tuple and array forms.
assert_eq!(Point::from((3, 4)), p);
assert_eq!(Point::from([3, 4]), p);

Fields§

§x: i16

Horizontal coordinate.

§y: i16

Vertical coordinate.

Implementations§

Source§

impl Point

Source

pub const ORIGIN: Point

The origin, (0, 0).

Source

pub const MIN_COORD: i16 = -16384

The most negative coordinate a Polygon may use, -16384.

See Point for why the range is capped below what i16 could hold.

Source

pub const MAX_COORD: i16 = 16383

The largest coordinate a Polygon may use, 16383.

See Point for why the range is capped below what i16 could hold.

Source

pub const fn in_range(self) -> bool

Whether both coordinates are within MIN_COORD..=MAX_COORD.

§Examples
use straight_skeleton::Point;

assert!(Point::new(16383, -16384).in_range());
assert!(!Point::new(16384, 0).in_range());
assert!(!Point::new(0, i16::MIN).in_range());
Source

pub const fn new(x: i16, y: i16) -> Self

Constructs a point from its coordinates.

Source

pub fn distance_squared(self, other: Point) -> u32

Squared distance to other, computed exactly in i32.

Exact for every in-range pair: the largest possible value is 2 * 32767^2 = 2_147_352_578, which fits comfortably.

Points outside the coordinate cap saturate rather than wrap, which keeps comparisons monotone; a Polygon cannot contain such a point anyway.

§Examples
use straight_skeleton::Point;

assert_eq!(Point::new(0, 0).distance_squared(Point::new(3, 4)), 25);
Source§

impl Point

Source

pub fn cast<T: NumCast>(self) -> Option<(T, T)>

Available on crate feature num-traits only.

Converts to any numeric type num-traits can cast to.

Always succeeds for the usual targets, since every i16 fits.

§Examples
use straight_skeleton::Point;

let p = Point::new(3, -4);
assert_eq!(p.cast::<f32>(), Some((3.0, -4.0)));
assert_eq!(p.cast::<i64>(), Some((3, -4)));
// -4 has no unsigned representation.
assert_eq!(p.cast::<u8>(), None);
Source

pub fn try_cast<T: ToPrimitive>(x: T, y: T) -> Option<Point>

Available on crate feature num-traits only.

Builds a point from any numeric type, if both coordinates land exactly on the i16 lattice.

Returns None rather than rounding. Note this is stricter than num_traits’ own to_i16, which truncates 0.5 to 0; a silently moved vertex is the last thing a geometry crate should hand you.

§Examples
use straight_skeleton::Point;

assert_eq!(Point::try_cast(3.0f64, -4.0f64), Some(Point::new(3, -4)));
assert_eq!(Point::try_cast(1e9f64, 0.0f64), None);   // out of range
assert_eq!(Point::try_cast(0.5f64, 0.0f64), None);   // off the lattice
assert_eq!(Point::try_cast(3i64, -4i64), Some(Point::new(3, -4)));

Trait Implementations§

Source§

impl Clone for Point

Source§

fn clone(&self) -> Point

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Point

Source§

impl Debug for Point

Source§

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

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

impl Default for Point

Source§

fn default() -> Point

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

impl<'de> Deserialize<'de> for Point

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 Eq for Point

Source§

impl From<(i16, i16)> for Point

Source§

fn from((x, y): (i16, i16)) -> Self

Converts to this type from the input type.
Source§

impl From<Coord<i16>> for Point

Available on crate feature geo-types only.
Source§

fn from(c: Coord<i16>) -> Self

Converts to this type from the input type.
Source§

impl From<I16Vec2> for Point

Available on crate feature glam only.
Source§

fn from(v: I16Vec2) -> Self

Converts to this type from the input type.
Source§

impl From<Point2<i16>> for Point

Available on crate feature mint only.
Source§

fn from(p: Point2<i16>) -> Self

Converts to this type from the input type.
Source§

impl From<Point<i16>> for Point

Available on crate feature geo-types only.
Source§

fn from(p: Point<i16>) -> Self

Converts to this type from the input type.
Source§

impl From<Point> for (i16, i16)

Source§

fn from(p: Point) -> Self

Converts to this type from the input type.
Source§

impl From<Point> for [i16; 2]

Source§

fn from(p: Point) -> Self

Converts to this type from the input type.
Source§

impl From<Point> for Coord<i16>

Available on crate feature geo-types only.
Source§

fn from(p: Point) -> Self

Converts to this type from the input type.
Source§

impl From<Point> for Point<i16>

Available on crate feature geo-types only.
Source§

fn from(p: Point) -> Self

Converts to this type from the input type.
Source§

impl From<Point> for Coord<f64>

Available on crate feature geo-types only.

Widening to f64 is exact, which is what makes this From rather than TryFrom: f64 has 53 mantissa bits and i16 needs 16.

Source§

fn from(p: Point) -> Self

Converts to this type from the input type.
Source§

impl From<Point> for I16Vec2

Available on crate feature glam only.
Source§

fn from(p: Point) -> Self

Converts to this type from the input type.
Source§

impl From<Point> for IVec2

Available on crate feature glam only.
Source§

fn from(p: Point) -> Self

Converts to this type from the input type.
Source§

impl From<Point> for Vec2

Available on crate feature glam only.

i16 -> f32 is exact: 16 bits fits comfortably in a 24-bit mantissa.

Source§

fn from(p: Point) -> Self

Converts to this type from the input type.
Source§

impl From<Point> for Point2<i16>

Available on crate feature mint only.
Source§

fn from(p: Point) -> Self

Converts to this type from the input type.
Source§

impl From<Point> for Vector2<i16>

Available on crate feature mint only.
Source§

fn from(p: Point) -> Self

Converts to this type from the input type.
Source§

impl From<Point> for Point2<f32>

Available on crate feature mint only.
Source§

fn from(p: Point) -> Self

Converts to this type from the input type.
Source§

impl From<Vector2<i16>> for Point

Available on crate feature mint only.
Source§

fn from(v: Vector2<i16>) -> Self

Converts to this type from the input type.
Source§

impl From<[i16; 2]> for Point

Source§

fn from([x, y]: [i16; 2]) -> Self

Converts to this type from the input type.
Source§

impl Hash for Point

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 Ord for Point

Source§

fn cmp(&self, other: &Point) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Point

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialOrd for Point

Source§

fn partial_cmp(&self, other: &Point) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Point

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 StructuralPartialEq for Point

Source§

impl TryFrom<Coord> for Point

Available on crate feature geo-types only.
Source§

type Error = CoordError

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

fn try_from(c: Coord<f64>) -> Result<Self, CoordError>

Performs the conversion.
Source§

impl TryFrom<IVec2> for Point

Available on crate feature glam only.
Source§

type Error = CoordError

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

fn try_from(v: IVec2) -> Result<Self, CoordError>

Performs the conversion.
Source§

impl TryFrom<Point2<f32>> for Point

Available on crate feature mint only.
Source§

type Error = CoordError

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

fn try_from(p: Point2<f32>) -> Result<Self, CoordError>

Performs the conversion.
Source§

impl TryFrom<Vec2> for Point

Available on crate feature glam only.
Source§

type Error = CoordError

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

fn try_from(v: Vec2) -> Result<Self, CoordError>

Performs the conversion.

Auto Trait Implementations§

§

impl Freeze for Point

§

impl RefUnwindSafe for Point

§

impl Send for Point

§

impl Sync for Point

§

impl Unpin for Point

§

impl UnsafeUnpin for Point

§

impl UnwindSafe for Point

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, dest: *mut u8)

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

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

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.