Struct Coord

Source
pub struct Coord {
    pub x: usize,
    pub y: usize,
}
Expand description

Unsigned-int coordinates

Fields§

§x: usize§y: usize

Implementations§

Source§

impl Coord

Source

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

Make a new coord.

Source

pub fn to_2d_idx(self, width: usize) -> usize

Get this as an index into an array representing a 2d array.

(AKA, y * width + x.)

Source

pub fn to_icoord(self) -> ICoord

Convert this into an ICoord.

Source

pub fn neighbors4(self) -> Vec<Coord>

Get a list of this coordinate’s orthagonal neighbors. They are given in clockwise order starting with the neighbor to the north, as if each of Direction4::DIRECTIONS had been added to them.

If a neighbor is out of bounds, it is skipped in the output.

There may be 2, 3, or 4 neighbors:

  • 2 if this is at (0, 0)
  • 3 if this is on an edge (x or y are 0)
  • 4 otherwise.

assert_eq!(
    Coord::new(5, 7).neighbors4(),
    &[
        Coord::new(5, 6),
        Coord::new(6, 7),
        Coord::new(5, 8),
        Coord::new(4, 7),
    ]
);

// May return fewer than 4 neighbors
assert_eq!(
    Coord::new(0, 5).neighbors4(),
    &[
        Coord::new(0, 4),
        Coord::new(1, 5),
        Coord::new(0, 6),
        // Skip (-1, 5) for being out of bounds
    ]
);
Source

pub fn neighbors8(self) -> Vec<Coord>

Get a list of this coordinate’s orthagonal and diagonal neighbors. They are given in clockwise order starting with the neighbor to the north, as if each of Direction8::DIRECTIONS had been added to them.

If a neighbor is out of bounds, it is skipped in the output.

There may be 3, 5, or 8 neighbors:

  • 3 if this is at (0, 0)
  • 5 if this is on an edge (x or y are 0)
  • 8 otherwise.

assert_eq!(
    Coord::new(5, 7).neighbors8(),
    [
        Coord::new(5, 6),
        Coord::new(6, 6),
        Coord::new(6, 7),
        Coord::new(6, 8),
        Coord::new(5, 8),
        Coord::new(4, 8),
        Coord::new(4, 7),
        Coord::new(4, 6),
    ]
);

// May return fewer than 8 neighbors
assert_eq!(
    Coord::new(0, 5).neighbors8(),
    &[
        Coord::new(0, 4),
        Coord::new(1, 4),
        Coord::new(1, 5),
        Coord::new(1, 6),
        Coord::new(0, 6),
        // Skip (-1, 6) for being out of bounds
        // Skip (-1, 5)
        // Skip (-1, 4)
    ]
);

Trait Implementations§

Source§

impl Add for Coord

Source§

type Output = Coord

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl AddAssign for Coord

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl Clone for Coord

Source§

fn clone(&self) -> Coord

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

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Coord

Source§

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

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

impl Display for Coord

Source§

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

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

impl From<Coord> for ICoord

Source§

fn from(value: Coord) -> Self

Converts to this type from the input type.
Source§

impl Hash for Coord

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 Mul<usize> for Coord

Source§

type Output = Coord

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: usize) -> Self::Output

Performs the * operation. Read more
Source§

impl MulAssign<usize> for Coord

Source§

fn mul_assign(&mut self, rhs: usize)

Performs the *= operation. Read more
Source§

impl PartialEq for Coord

Source§

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

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

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 for Coord

Source§

type Output = Coord

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl SubAssign for Coord

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl TryFrom<ICoord> for Coord

Try to convert an ICoord to a Coord. Will return Error if the ICoord has any negatives in it.

Source§

type Error = TryFromIntError

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

fn try_from(value: ICoord) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Copy for Coord

Source§

impl Eq for Coord

Source§

impl StructuralPartialEq for Coord

Auto Trait Implementations§

§

impl Freeze for Coord

§

impl RefUnwindSafe for Coord

§

impl Send for Coord

§

impl Sync for Coord

§

impl Unpin for Coord

§

impl UnwindSafe for Coord

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> CallHasher for T
where T: Hash + ?Sized,

Source§

default fn get_hash<H, B>(value: &H, build_hasher: &B) -> u64
where H: Hash + ?Sized, B: BuildHasher,

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V