Struct Delta

Source
pub struct Delta {
    pub dx: i32,
    pub dy: i32,
}
Expand description

§Delta

A Delta represents a signed 2D offset — a difference in position, movement, or translation across a grid.

It is typically used to describe relative shifts between Coordinates, direction-based motion, or area translation.


§Example

use rpgx::prelude::*;

let d1 = Delta::new(3, -2);
let d2 = Delta::new(-1, 1);
let combined = d1 + d2; // Delta { dx: 2, dy: -1 }

§Fields

§dx: i32

The horizontal delta (positive is rightward, negative is leftward).

§dy: i32

The vertical delta (positive is downward, negative is upward).


§Constructors

§Delta::new(dx: i32, dy: i32) -> Self

Creates a new delta with the specified components.

use rpgx::prelude::*;

let d = Delta::new(2, -1);

§Delta::zero() -> Self

Returns a delta representing no movement: (0, 0).


§Methods

§fn invert(self) -> Self

Returns a new delta with inverted components (-dx, -dy).


§fn is_zero(self) -> bool

Returns true if both components are zero.


§fn manhattan(self) -> u32

Computes the Manhattan distance, i.e., the sum of absolute values of dx and dy.

use rpgx::prelude::*;

let d = Delta::new(-2, 3);
assert_eq!(d.manhattan(), 5);

§fn is_axis_aligned(self) -> bool

Returns true if the delta moves only in one direction (either horizontal or vertical).


§fn is_diagonal(self) -> bool

Returns true if both dx and dy are nonzero, i.e., the movement is diagonal.


§Design Notes

  • Delta is signed and supports flexible offsetting in all directions.
  • Used heavily in coordinate math, pathfinding, area translation, and movement logic.
  • Use Coordinates::offseted() or try_offseted() to apply deltas to unsigned positions.

§See Also

  • Coordinates: Grid position values.
  • Shape: Region dimensions.
  • Rect: Rectangular areas of the grid.
  • Layer, Map Represents a 2D movement or directional offset with signed deltas.

Fields§

§dx: i32§dy: i32

Implementations§

Source§

impl Delta

Constructors

Source

pub fn new(dx: i32, dy: i32) -> Self

Creates a new delta with given x and y differences.

Source

pub fn zero() -> Self

Returns a zero delta (no movement).

Source§

impl Delta

Utilities

Source

pub fn invert(self) -> Self

Returns the inverse of the delta (negates both components).

Source

pub fn is_zero(self) -> bool

Returns true if the delta is zero.

Source

pub fn manhattan(self) -> u32

Returns the Manhattan distance represented by this delta.

Source

pub fn is_axis_aligned(self) -> bool

Returns true if the delta moves in only one axis.

Source

pub fn is_diagonal(self) -> bool

Returns true if this delta is diagonal (nonzero dx and dy).

Trait Implementations§

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<Delta> for Rect

Source§

type Output = Rect

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add for Delta

Source§

type Output = Delta

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl AddAssign for Delta

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl Clone for Delta

Source§

fn clone(&self) -> Delta

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 Delta

Source§

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

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

impl Default for Delta

Source§

fn default() -> Delta

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

impl Hash for Delta

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 Neg for Delta

Source§

type Output = Delta

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl PartialEq for Delta

Source§

fn eq(&self, other: &Delta) -> 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<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<Delta> for Rect

Source§

type Output = Rect

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub for Delta

Source§

type Output = Delta

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl SubAssign for Delta

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl Copy for Delta

Source§

impl Eq for Delta

Source§

impl StructuralPartialEq for Delta

Auto Trait Implementations§

§

impl Freeze for Delta

§

impl RefUnwindSafe for Delta

§

impl Send for Delta

§

impl Sync for Delta

§

impl Unpin for Delta

§

impl UnwindSafe for Delta

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.