pub struct Coordinates {
pub x: u32,
pub y: u32,
}
Expand description
§Coordinates
A Coordinates
represents a 2D grid position with unsigned x
and y
values. It is the primary spatial unit used throughout the RPGX engine for addressing tiles, shapes, and spatial operations.
Coordinates support basic arithmetic, delta-based translation, and bounding logic.
§Example
use rpgx::prelude::*;
let a = Coordinates::new(2, 3);
let b = Coordinates::new(1, 1);
let delta = Delta::new(-1, 2);
assert_eq!(a + b, Coordinates::new(3, 4));
assert_eq!(a.offseted(delta), Coordinates::new(1, 5));
§Fields
§x: u32
The horizontal position on the grid (0-indexed).
§y: u32
The vertical position on the grid (0-indexed).
§Constructors
§Coordinates::new(x: u32, y: u32) -> Self
Creates a new Coordinates
instance at the specified x
, y
position.
use rpgx::prelude::*;
let coord = Coordinates::new(2, 3);
§Coordinates::bounding_box(coords: &[Coordinates]) -> Option<(Coordinates, Coordinates)>
Computes the smallest exclusive bounding box that contains all the given coordinates.
use rpgx::prelude::*;
let points = vec![
Coordinates::new(1, 2),
Coordinates::new(3, 4),
Coordinates::new(2, 1),
];
let (min, max) = Coordinates::bounding_box(&points).unwrap();
assert_eq!(min, Coordinates::new(1, 1));
assert_eq!(max, Coordinates::new(4, 5)); // Exclusive
Returns None
if the input is empty.
§Methods
§fn is_origin(&self) -> bool
Returns true
if the coordinate is at the origin (0, 0)
.
§fn is_aligned_with(self, other: Self) -> bool
Returns true
if self
shares either the same x
or y
value with other
.
§fn is_within(&self, origin: Coordinates, shape: Shape) -> bool
Checks if the coordinate lies inside the rectangle defined by origin
and shape
. The bounds are exclusive: [origin, origin + shape)
.
use rpgx::prelude::*;
let inside = Coordinates::new(3, 4).is_within(Coordinates::new(2, 2), Shape::new(3, 3));
§fn offseted(self, delta: Delta) -> Self
Applies a Delta
to this coordinate, using saturating arithmetic to prevent underflow.
§fn try_offseted(self, delta: Delta) -> Option<Coordinates>
Applies a delta, returning None
if it would result in negative coordinates.
§fn to_delta(self) -> Delta
Converts the coordinate into a delta with dx = x
and dy = y
.
§Design Notes
- Coordinates are unsigned and saturate on subtraction. Use
try_offseted
when negative shifts should be validated. - Supports composable arithmetic with
Shape
,Delta
, and otherCoordinates
. - Designed for grid-based logic in pathfinding, masking, and tile manipulation.
§See Also
Fields§
§x: u32
§y: u32
Implementations§
Source§impl Coordinates
Constructors
impl Coordinates
Constructors
Source§impl Coordinates
Coordinates utils
impl Coordinates
Coordinates utils
Source§impl Coordinates
Shape integration
impl Coordinates
Shape integration
Sourcepub fn is_within(&self, origin: Coordinates, shape: Shape) -> bool
pub fn is_within(&self, origin: Coordinates, shape: Shape) -> bool
Checks whether this coordinate lies within the rectangular area
defined by an origin and a Shape
(width × height).
The bounds are half-open: [origin, origin + shape)
.
Source§impl Coordinates
Delta integration
impl Coordinates
Delta integration
Sourcepub fn offseted(self, delta: Delta) -> Self
pub fn offseted(self, delta: Delta) -> Self
Returns a new coordinate by applying the given Delta
.
Will saturate at u32::MIN
or u32::MAX
when under-/overflowing.
Sourcepub fn try_offseted(self, delta: Delta) -> Option<Self>
pub fn try_offseted(self, delta: Delta) -> Option<Self>
Returns a new coordinate by applying the given Delta
, or None
if it would
result in a negative coordinate (invalid in unsigned space).
Trait Implementations§
Source§impl Add<Coordinates> for Shape
Arithmetic with coordinates
impl Add<Coordinates> for Shape
Arithmetic with coordinates
Source§impl Add<Delta> for Coordinates
impl Add<Delta> for Coordinates
Source§impl Add<Shape> for Coordinates
impl Add<Shape> for Coordinates
Source§fn add(self, shape: Shape) -> Coordinates
fn add(self, shape: Shape) -> Coordinates
Adds a Shape
(width, height) to the coordinate.
Source§type Output = Coordinates
type Output = Coordinates
+
operator.Source§impl Add for Coordinates
impl Add for Coordinates
Source§fn add(self, rhs: Coordinates) -> Coordinates
fn add(self, rhs: Coordinates) -> Coordinates
Adds another coordinate component-wise.
Source§type Output = Coordinates
type Output = Coordinates
+
operator.Source§impl AddAssign for Coordinates
impl AddAssign for Coordinates
Source§fn add_assign(&mut self, rhs: Coordinates)
fn add_assign(&mut self, rhs: Coordinates)
Adds another coordinate in-place component-wise.
Source§impl Clone for Coordinates
impl Clone for Coordinates
Source§fn clone(&self) -> Coordinates
fn clone(&self) -> Coordinates
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for Coordinates
impl Debug for Coordinates
Source§impl Default for Coordinates
impl Default for Coordinates
Source§fn default() -> Coordinates
fn default() -> Coordinates
Source§impl Hash for Coordinates
impl Hash for Coordinates
Source§impl PartialEq for Coordinates
impl PartialEq for Coordinates
Source§impl Sub<Coordinates> for Shape
impl Sub<Coordinates> for Shape
Source§impl Sub<Delta> for Coordinates
impl Sub<Delta> for Coordinates
Source§impl Sub<Shape> for Coordinates
impl Sub<Shape> for Coordinates
Source§fn sub(self, shape: Shape) -> Coordinates
fn sub(self, shape: Shape) -> Coordinates
Subtracts a Shape
(width, height) from the coordinate using saturating subtraction.
Source§type Output = Coordinates
type Output = Coordinates
-
operator.Source§impl Sub for Coordinates
impl Sub for Coordinates
Source§fn sub(self, rhs: Coordinates) -> Coordinates
fn sub(self, rhs: Coordinates) -> Coordinates
Subtracts another coordinate component-wise using saturating subtraction.
Source§type Output = Coordinates
type Output = Coordinates
-
operator.impl Copy for Coordinates
impl Eq for Coordinates
impl StructuralPartialEq for Coordinates
Auto Trait Implementations§
impl Freeze for Coordinates
impl RefUnwindSafe for Coordinates
impl Send for Coordinates
impl Sync for Coordinates
impl Unpin for Coordinates
impl UnwindSafe for Coordinates
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.