Trait gridly::vector::VectorLike
source · pub trait VectorLike: Sized {
// Required methods
fn rows(&self) -> Rows;
fn columns(&self) -> Columns;
fn as_vector(&self) -> Vector;
// Provided methods
fn manhattan_length(&self) -> isize { ... }
fn checked_manhattan_length(&self) -> Option<isize> { ... }
fn clockwise(&self) -> Vector { ... }
fn anticlockwise(&self) -> Vector { ... }
fn reverse(&self) -> Vector { ... }
fn rotate(&self, rotation: Rotation) -> Vector { ... }
fn get_component<T: Component>(&self) -> T { ... }
fn transpose(&self) -> Vector { ... }
fn direction(&self) -> Option<Direction> { ... }
}
Expand description
VectorLike
is implemented for types that can be used as a vector. They
can participate in vector arithmetic, comparison, and other vector oprations.
Required Methods§
Provided Methods§
sourcefn manhattan_length(&self) -> isize
fn manhattan_length(&self) -> isize
Return the manhattan length of the vector. The manhattan length of a vector is the sum of the absolute values of its components.
Example
use gridly::vector::*;
let vec = Vector::new(-8, 3);
assert_eq!(vec.manhattan_length(), 11);
sourcefn checked_manhattan_length(&self) -> Option<isize>
fn checked_manhattan_length(&self) -> Option<isize>
Return the manhattan length of the vector, or None
if there are
any overflows.
Example
use std::isize;
use gridly::vector::*;
let vec = Vector::new(isize::MIN, 0);
assert_eq!(vec.checked_manhattan_length(), None);
let vec = Vector::new(isize::MAX, isize::MAX);
assert_eq!(vec.checked_manhattan_length(), None);
let vec = Vector::new(-54, 30);
assert_eq!(vec.checked_manhattan_length(), Some(84));
sourcefn clockwise(&self) -> Vector
fn clockwise(&self) -> Vector
Return a new vector, rotated 90 degrees clockwise.
Example
use gridly::vector::*;
let vec = Vector::new(3, 8);
assert_eq!(vec.clockwise(), Vector::new(8, -3));
sourcefn anticlockwise(&self) -> Vector
fn anticlockwise(&self) -> Vector
Return a new vector, rotated 90 degrees counterclockwise.
Example
use gridly::vector::*;
let vec = Vector::new(-2, -4);
assert_eq!(vec.anticlockwise(), Vector::new(4, -2));
sourcefn reverse(&self) -> Vector
fn reverse(&self) -> Vector
Return a new vector, facing the opposite direction of this one
Example
use gridly::vector::*;
let vec = Vector::new(4, -5);
assert_eq!(vec.reverse(), Vector::new(-4, 5));
sourcefn rotate(&self, rotation: Rotation) -> Vector
fn rotate(&self, rotation: Rotation) -> Vector
Return a new vector, rotated by a given rotation
Example
use gridly::vector::*;
use gridly::rotation::{Clockwise, Anticlockwise};
let vec = Vector::new(-5, 3);
assert_eq!(vec.rotate(Clockwise), vec.clockwise());
assert_eq!(vec.rotate(Anticlockwise), vec.anticlockwise());
sourcefn get_component<T: Component>(&self) -> T
fn get_component<T: Component>(&self) -> T
Generically get either the Rows
or Columns
of a vector
Example
use gridly::vector::*;
let vec = Vector::new(-3, 4);
let rows: Rows = vec.get_component();
let columns: Columns = vec.get_component();
assert_eq!(rows, Rows(-3));
assert_eq!(columns, Columns(4));
sourcefn transpose(&self) -> Vector
fn transpose(&self) -> Vector
Swap the rows and columns of this Vector
Example
use gridly::vector::*;
let vec = Vector::new(2, 8);
assert_eq!(vec.transpose(), Vector::new(8, 2));
sourcefn direction(&self) -> Option<Direction>
fn direction(&self) -> Option<Direction>
If the vector is pointing in an orthogonal direction, return that direction
Example
use gridly::vector::*;
use gridly::direction::*;
assert_eq!(Vector::new(3, 0).direction(), Some(Down));
assert_eq!(Vector::new(0, 2).direction(), Some(Right));
assert_eq!(Vector::new(-10, 0).direction(), Some(Up));
assert_eq!(Vector::new(0, -1).direction(), Some(Left));
assert_eq!(Vector::zero().direction(), None);
assert_eq!(Vector::new(3, 4).direction(), None);
Object Safety§
Implementations on Foreign Types§
source§impl VectorLike for (isize, isize)
impl VectorLike for (isize, isize)
source§impl VectorLike for (Columns, Rows)
impl VectorLike for (Columns, Rows)
source§impl VectorLike for (Rows, Columns)
impl VectorLike for (Rows, Columns)
source§impl<T: VectorLike> VectorLike for &T
impl<T: VectorLike> VectorLike for &T
fn rows(&self) -> Rows
fn columns(&self) -> Columns
fn as_vector(&self) -> Vector
fn manhattan_length(&self) -> isize
fn checked_manhattan_length(&self) -> Option<isize>
fn clockwise(&self) -> Vector
fn anticlockwise(&self) -> Vector
fn reverse(&self) -> Vector
fn rotate(&self, rotation: Rotation) -> Vector
fn get_component<C: Component>(&self) -> C
fn transpose(&self) -> Vector
fn direction(&self) -> Option<Direction>
Implementors§
impl VectorLike for Direction
A Direction
acts like a unit vector in the given direction. This allows
it to be used in things like Vector arithmetic.
Example:
use gridly::prelude::*;
assert_eq!(Vector::new(1, 1) + Up, Vector::new(0, 1));
assert_eq!(Location::new(3, 4) - Left, Location::new(3, 5));