Trait cgmath::VectorSpace [] [src]

pub trait VectorSpace: Copy + Clone where Self: Zero, Self: Add<Self, Output=Self>, Self: Sub<Self, Output=Self>, Self: Mul<Self::Scalar, Output=Self>, Self: Div<Self::Scalar, Output=Self>, Self: Rem<Self::Scalar, Output=Self> {
    type Scalar: BaseNum;

Vectors that can be added together and multiplied by scalars.

Examples include vectors, matrices, and quaternions.

Required operators

Vector addition

Vectors can be added, subtracted, or negated via the following traits:

  • Add<Output = Self>
  • Sub<Output = Self>
  • Neg<Output = Self>
use cgmath::Vector3;

let velocity0 = Vector3::new(1, 2, 0);
let velocity1 = Vector3::new(1, 1, 0);

let total_velocity = velocity0 + velocity1;
let velocity_diff = velocity1 - velocity0;
let reversed_velocity0 = -velocity0;

Vector spaces are also required to implement the additive identity trait, Zero. Adding this to another vector should have no effect:

use cgmath::prelude::*;
use cgmath::Vector2;

let v = Vector2::new(1, 2);
assert_eq!(v + Vector2::zero(), v);

Scalar multiplication

Vectors can be multiplied or divided by their associated scalars via the following traits:

  • Mul<Self::Scalar, Output = Self>
  • Div<Self::Scalar, Output = Self>
  • Rem<Self::Scalar, Output = Self>
use cgmath::Vector2;

let translation = Vector2::new(3.0, 4.0);
let scale_factor = 2.0;

let upscaled_translation = translation * scale_factor;
let downscaled_translation = translation / scale_factor;

Associated Types

type Scalar: BaseNum

The associated scalar.