mat/
coordinate.rs

1#[cfg(feature="serde")]
2use serde::{Deserialize, Serialize};
3
4use super::Dimensions;
5
6/// Row-major coordinate.
7///
8/// ```
9/// # use mat::{Coordinate, Dimensions};
10/// let c = Coordinate::from ((1, 2));
11/// assert_eq!(c.row,    1);
12/// assert_eq!(c.column, 2);
13/// let c = Coordinate::from ([1, 2]);
14/// assert_eq!(c.row,    1);
15/// assert_eq!(c.column, 2);
16/// let a = Coordinate::from ((1, 2));
17/// let b = Dimensions::from ((1, 1));
18/// assert_eq!(a + b, (2, 3).into());
19/// let a = Coordinate::from ((1, 2));
20/// let b = Dimensions::from ((1, 1));
21/// assert_eq!(a - b, (0, 1).into());
22/// ```
23#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
24#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
25pub struct Coordinate {
26  pub row    : usize,
27  pub column : usize
28}
29
30impl From <(usize, usize)> for Coordinate {
31  fn from ((row, column) : (usize, usize)) -> Self {
32    Coordinate { row, column }
33  }
34}
35impl From <[usize; 2]> for Coordinate {
36  fn from ([row, column] : [usize; 2]) -> Self {
37    Coordinate { row, column }
38  }
39}
40impl From <Coordinate> for (usize, usize) {
41  fn from (Coordinate { row, column } : Coordinate) -> Self {
42    (row, column)
43  }
44}
45impl From <Coordinate> for [usize; 2] {
46  fn from (Coordinate { row, column } : Coordinate) -> Self {
47    [row, column]
48  }
49}
50impl std::ops::Add <Dimensions> for Coordinate {
51  type Output = Coordinate;
52  fn add (self, rhs : Dimensions) -> Coordinate {
53    (self.row + rhs.rows, self.column + rhs.columns).into()
54  }
55}
56impl std::ops::Sub <Dimensions> for Coordinate {
57  type Output = Coordinate;
58  fn sub (self, rhs : Dimensions) -> Coordinate {
59    (self.row - rhs.rows, self.column - rhs.columns).into()
60  }
61}