Trait 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§

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

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so 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