Struct Coordinates

Source
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 other Coordinates.
  • Designed for grid-based logic in pathfinding, masking, and tile manipulation.

§See Also

  • Delta: Describes relative movement across coordinates.
  • Shape: Describes dimensions for regions starting at a coordinate.
  • Rect: Represents rectangular areas of the grid.
  • Mask, Layer, Map Represents a 2D grid coordinate with x and y components.

Fields§

§x: u32§y: u32

Implementations§

Source§

impl Coordinates

Constructors

Source

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

Source

pub fn bounding_box(coords: &[Self]) -> Option<(Self, Self)>

Computes the exclusive bounding box that contains all the given coordinates.

Returns None if the input slice is empty.

Source§

impl Coordinates

Coordinates utils

Source

pub fn is_origin(&self) -> bool

Returns true if the coordinate is at the origin (0, 0).

Source

pub fn is_aligned_with(self, other: Self) -> bool

Returns true if the two coordinates share either x or y axis.

Source§

impl Coordinates

Shape integration

Source

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

Source

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.

Source

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).

Source

pub fn to_delta(self) -> Delta

Converts this coordinate into a Delta where dx = x and dy = y.

Trait Implementations§

Source§

impl Add<Coordinates> for Shape

Arithmetic with coordinates

Source§

type Output = Shape

The resulting type after applying the + operator.
Source§

fn add(self, coordinates: Coordinates) -> Shape

Performs the + operation. Read more
Source§

impl Add<Delta> for Coordinates

Source§

fn add(self, delta: Delta) -> Self::Output

Adds a Delta, returning None if the result is negative.

Source§

type Output = Option<Coordinates>

The resulting type after applying the + operator.
Source§

impl Add<Shape> for Coordinates

Source§

fn add(self, shape: Shape) -> Coordinates

Adds a Shape (width, height) to the coordinate.

Source§

type Output = Coordinates

The resulting type after applying the + operator.
Source§

impl Add for Coordinates

Source§

fn add(self, rhs: Coordinates) -> Coordinates

Adds another coordinate component-wise.

Source§

type Output = Coordinates

The resulting type after applying the + operator.
Source§

impl AddAssign for Coordinates

Source§

fn add_assign(&mut self, rhs: Coordinates)

Adds another coordinate in-place component-wise.

Source§

impl Clone for Coordinates

Source§

fn clone(&self) -> Coordinates

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Coordinates

Source§

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

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

impl Default for Coordinates

Source§

fn default() -> Coordinates

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

impl Hash for Coordinates

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 PartialEq for Coordinates

Source§

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

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

const 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 Sub<Coordinates> for Shape

Source§

type Output = Shape

The resulting type after applying the - operator.
Source§

fn sub(self, coordinates: Coordinates) -> Shape

Performs the - operation. Read more
Source§

impl Sub<Delta> for Coordinates

Source§

fn sub(self, delta: Delta) -> Self::Output

Subtracts a Delta, returning None if the result is negative.

Source§

type Output = Option<Coordinates>

The resulting type after applying the - operator.
Source§

impl Sub<Shape> for Coordinates

Source§

fn sub(self, shape: Shape) -> Coordinates

Subtracts a Shape (width, height) from the coordinate using saturating subtraction.

Source§

type Output = Coordinates

The resulting type after applying the - operator.
Source§

impl Sub for Coordinates

Source§

fn sub(self, rhs: Coordinates) -> Coordinates

Subtracts another coordinate component-wise using saturating subtraction.

Source§

type Output = Coordinates

The resulting type after applying the - operator.
Source§

impl Copy for Coordinates

Source§

impl Eq for Coordinates

Source§

impl StructuralPartialEq for Coordinates

Auto Trait Implementations§

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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.