[][src]Struct wrapping_coords2d::WrappingCoords2d

pub struct WrappingCoords2d { /* fields omitted */ }

Represents a 2D grid with wrapping.

Methods

impl WrappingCoords2d[src]

pub fn new(width: i32, height: i32) -> Result<WrappingCoords2d, ErrorKind>[src]

Constructs a new WrappingCoords2d representation.

Errors

Both width and height must be larger than 0. Also, their product must be smaller than std::i32::MAX.

pub fn width(&self) -> i32[src]

Returns the width of the grid.

Examples

use wrapping_coords2d::WrappingCoords2d;
let w2d = WrappingCoords2d::new(10, 10).unwrap();
assert_eq!(w2d.width(), 10);

pub fn height(&self) -> i32[src]

Returns the height of the grid.

Examples

use wrapping_coords2d::WrappingCoords2d;
let w2d = WrappingCoords2d::new(10, 10).unwrap();
assert_eq!(w2d.height(), 10);

pub fn size(&self) -> i32[src]

Returns the total number of cells in the grid.

Examples

use wrapping_coords2d::WrappingCoords2d;
let w2d = WrappingCoords2d::new(10, 10).unwrap();
assert_eq!(w2d.size(), 100);

pub fn modulo(lhs: i32, rhs: i32) -> i32[src]

Returns the Euclidean modulo, a non-negative number. This operation is also available in the DivRem crate. In contrast, the standard modulo operator can be negative; for example, -11 % 10 = -1.

Examples

use wrapping_coords2d::WrappingCoords2d;
assert_eq!(-11 % 10, -1);
assert_eq!(WrappingCoords2d::modulo(-11, 10), 9);

pub fn index(&self, x: i32, y: i32) -> usize[src]

Returns an index into the grid based on x and y coordinates.

Examples

use wrapping_coords2d::WrappingCoords2d;
let w2d = WrappingCoords2d::new(10, 10).unwrap();
// Here are some basic coordinates:
assert_eq!(w2d.coords(0), (0, 0));
assert_eq!(w2d.coords(1), (1, 0));
assert_eq!(w2d.coords(10), (0, 1));
assert_eq!(w2d.coords(11), (1, 1));
assert_eq!(w2d.coords(90), (0, 9));
assert_eq!(w2d.coords(91), (1, 9));
// Here are the cell at (5, 9) and its 8 neighbors, counterclockwise, starting from the right neighbor:
assert_eq!(w2d.index(5, 9), 95);
assert_eq!(w2d.index(6, 9), 96);
assert_eq!(w2d.index(6, 0), 6);
assert_eq!(w2d.index(5, 0), 5);
assert_eq!(w2d.index(4, 0), 4);
assert_eq!(w2d.index(4, 9), 94);
assert_eq!(w2d.index(4, 8), 84);
assert_eq!(w2d.index(5, 8), 85);
assert_eq!(w2d.index(6, 8), 86);
// Here are the cell at (0, 0) and its 8 neighbors, counterclockwise, starting from the right neighbor:
assert_eq!(w2d.index(0, 0), 0);
assert_eq!(w2d.index(1, 0), 1);
assert_eq!(w2d.index(1, 1), 11);
assert_eq!(w2d.index(0, 1), 10);
assert_eq!(w2d.index(-1, 1), 19);
assert_eq!(w2d.index(-1, 0), 9);
assert_eq!(w2d.index(-1, -1), 99);
assert_eq!(w2d.index(0, -1), 90);
assert_eq!(w2d.index(1, -1), 91);
// Here are the 8 neighbors of the cell at (5, 9), counterclockwise, starting from the right neighbor:
assert_eq!(w2d.shift(95, 1, 0), 96);
assert_eq!(w2d.shift(95, 1, 1), 6);
assert_eq!(w2d.shift(95, 0, 1), 5);
assert_eq!(w2d.shift(95, -1, 1), 4);
assert_eq!(w2d.shift(95, -1, 0), 94);
assert_eq!(w2d.shift(95, -1, -1), 84);
assert_eq!(w2d.shift(95, 0, -1), 85);
assert_eq!(w2d.shift(95, 1, -1), 86);
// Here are the 8 neighbors of the cell at (0, 0), counterclockwise, starting from the right neighbor:
assert_eq!(w2d.shift(0, 1, 0), 1);
assert_eq!(w2d.shift(0, 1, 1), 11);
assert_eq!(w2d.shift(0, 0, 1), 10);
assert_eq!(w2d.shift(0, -1, 1), 19);
assert_eq!(w2d.shift(0, -1, 0), 9);
assert_eq!(w2d.shift(0, -1, -1), 99);
assert_eq!(w2d.shift(0, 0, -1), 90);
assert_eq!(w2d.shift(0, 1, -1), 91);

pub fn coords(&self, idx: usize) -> (i32, i32)[src]

Returns x and y coordinates based on an index into the 1D container.

Examples

use wrapping_coords2d::WrappingCoords2d;
let w2d = WrappingCoords2d::new(10, 10).unwrap();
// Here are some basic coordinates:
assert_eq!(w2d.coords(0), (0, 0));
assert_eq!(w2d.coords(1), (1, 0));
assert_eq!(w2d.coords(10), (0, 1));
assert_eq!(w2d.coords(11), (1, 1));
assert_eq!(w2d.coords(90), (0, 9));
assert_eq!(w2d.coords(91), (1, 9));

pub fn shift(&self, start_idx: usize, delta_x: i32, delta_y: i32) -> usize[src]

Returns a new index into the grid based on a starting index start_idx, an x offset, and a y offset. x and y can be negative.

Safety

This function does not check that start_idx is a valid index.

Examples

use wrapping_coords2d::WrappingCoords2d;
let w2d = WrappingCoords2d::new(10, 10).unwrap();
// Here are the 8 neighbors of the cell at (5, 9), counterclockwise, starting from the right neighbor:
assert_eq!(w2d.shift(95, 1, 0), 96);
assert_eq!(w2d.shift(95, 1, 1), 6);
assert_eq!(w2d.shift(95, 0, 1), 5);
assert_eq!(w2d.shift(95, -1, 1), 4);
assert_eq!(w2d.shift(95, -1, 0), 94);
assert_eq!(w2d.shift(95, -1, -1), 84);
assert_eq!(w2d.shift(95, 0, -1), 85);
assert_eq!(w2d.shift(95, 1, -1), 86);
// Here are the 8 neighbors of the cell at (0, 0), counterclockwise, starting from the right neighbor:
assert_eq!(w2d.shift(0, 1, 0), 1);
assert_eq!(w2d.shift(0, 1, 1), 11);
assert_eq!(w2d.shift(0, 0, 1), 10);
assert_eq!(w2d.shift(0, -1, 1), 19);
assert_eq!(w2d.shift(0, -1, 0), 9);
assert_eq!(w2d.shift(0, -1, -1), 99);
assert_eq!(w2d.shift(0, 0, -1), 90);
assert_eq!(w2d.shift(0, 1, -1), 91);

Trait Implementations

impl Debug for WrappingCoords2d[src]

impl PartialEq<WrappingCoords2d> for WrappingCoords2d[src]

impl StructuralPartialEq for WrappingCoords2d[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.