pub struct Coord {
pub x: usize,
pub y: usize,
}
Expand description
Unsigned-int coordinates
Fields§
§x: usize
§y: usize
Implementations§
Source§impl Coord
impl Coord
Sourcepub fn to_2d_idx(self, width: usize) -> usize
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
.)
Sourcepub fn neighbors4(self) -> Vec<Coord>
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
ory
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
]
);
Sourcepub fn neighbors8(self) -> Vec<Coord>
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
ory
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 AddAssign for Coord
impl AddAssign for Coord
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+=
operation. Read moreSource§impl MulAssign<usize> for Coord
impl MulAssign<usize> for Coord
Source§fn mul_assign(&mut self, rhs: usize)
fn mul_assign(&mut self, rhs: usize)
*=
operation. Read moreSource§impl SubAssign for Coord
impl SubAssign for Coord
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-=
operation. Read moreSource§impl TryFrom<ICoord> for Coord
Try to convert an ICoord to a Coord.
Will return Error if the ICoord has any negatives in it.
impl TryFrom<ICoord> for Coord
Try to convert an ICoord to a Coord. Will return Error if the ICoord has any negatives in it.
impl Copy for Coord
impl Eq for Coord
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> 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> CallHasher for T
impl<T> CallHasher for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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