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^2wheredis the largest coordinate difference. At the fulli16range that is8_589_672_450— it overflowsi32and reports the wrong side. Capped, it is2_147_352_578, which fitsi32with 131_069 to spare, making every predicate exact. Seecrate::predicates. f32resolves0.002at the cap, against0.004at 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: i16Horizontal coordinate.
y: i16Vertical coordinate.
Implementations§
Source§impl Point
impl Point
Sourcepub fn distance_squared(self, other: Point) -> u32
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
impl Point
Sourcepub fn cast<T: NumCast>(self) -> Option<(T, T)>
Available on crate feature num-traits only.
pub fn cast<T: NumCast>(self) -> Option<(T, T)>
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);Sourcepub fn try_cast<T: ToPrimitive>(x: T, y: T) -> Option<Point>
Available on crate feature num-traits only.
pub fn try_cast<T: ToPrimitive>(x: T, y: T) -> Option<Point>
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§
impl Copy for Point
Source§impl<'de> Deserialize<'de> for Point
impl<'de> Deserialize<'de> for Point
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for Point
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.
impl From<Point> for Coord<f64>
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§impl From<Point> for Vec2
Available on crate feature glam only.i16 -> f32 is exact: 16 bits fits comfortably in a 24-bit mantissa.
impl From<Point> for Vec2
glam only.i16 -> f32 is exact: 16 bits fits comfortably in a 24-bit mantissa.