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§

source

fn rows(&self) -> Rows

source

fn columns(&self) -> Columns

source

fn as_vector(&self) -> Vector

Provided Methods§

source

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);
source

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));
source

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));
source

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));
source

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));
source

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());
source

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));
source

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));
source

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§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl VectorLike for (isize, isize)

source§

fn rows(&self) -> Rows

source§

fn columns(&self) -> Columns

source§

fn as_vector(&self) -> Vector

source§

impl VectorLike for (Columns, Rows)

source§

fn columns(&self) -> Columns

source§

fn rows(&self) -> Rows

source§

fn as_vector(&self) -> Vector

source§

impl VectorLike for (Rows, Columns)

source§

fn rows(&self) -> Rows

source§

fn columns(&self) -> Columns

source§

fn as_vector(&self) -> Vector

source§

impl<T: VectorLike> VectorLike for &T

Implementors§

source§

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));
source§

impl VectorLike for Columns

A Rows or a Columns value can be treated as a Vector where the converse component is 0.

source§

impl VectorLike for Rows

A Rows or a Columns value can be treated as a Vector where the converse component is 0.

source§

impl VectorLike for Vector