Crate cgmath

source ·
Expand description

A low-dimensional linear algebra library, targeted at computer graphics.

Trait overview

In order to make a clean, composable API, we divide operations into traits that are roughly based on mathematical properties. The main ones that we concern ourselves with are listed below:

  • VectorSpace: Specifies the main operators for vectors, quaternions, and matrices.
  • MetricSpace: For types that have a distance function implemented.
  • InnerSpace: For types that have a dot (or inner) product - ie. vectors or quaternions. This also allows for the definition of operations that are based on the dot product, like finding the magnitude or normalizing.
  • EuclideanSpace: Points in euclidean space, with an associated space of displacement vectors.
  • Matrix: Common operations for matrices of arbitrary dimensions.
  • SquareMatrix: A special trait for matrices where the number of columns equal the number of rows.

Other traits are included for practical convenience, for example:

  • Array: For contiguous, indexable arrays of elements, specifically vectors.
  • ElementWise: For element-wise addition, subtraction, multiplication, division, and remainder operations.

The prelude

Importing each trait individually can become a chore, so we provide a prelude module to allow you to import the main trait all at once. For example:

use cgmath::prelude::*;



  • Constrained conversion functions for assisting in situations where type inference is difficult.
  • This module contains the most common traits used in cgmath. By glob-importing this module, you can avoid the need to import each trait individually, while still being selective about what types you import.



  • A two-dimensional rotation matrix.
  • A three-dimensional rotation matrix.
  • A generic transformation consisting of a rotation, displacement vector and scale amount.
  • An angle, in degrees.
  • A set of Euler angles representing a rotation in three-dimensional space.
  • A 2 x 2, column major matrix
  • A 3 x 3, column major matrix
  • A 4 x 4, column major matrix
  • An orthographic projection with arbitrary left/right/bottom/top distances
  • A perspective projection with arbitrary left/right/bottom/top distances
  • A perspective projection based on a vertical field-of-view angle.
  • A point in 1-dimensional space.
  • A point in 2-dimensional space.
  • A point in 3-dimensional space.
  • A quaternion in scalar/vector form.
  • An angle, in radians.
  • The requisite parameters for testing for approximate equality.
  • The requisite parameters for testing for approximate equality.
  • A 1-dimensional vector.
  • A 2-dimensional vector.
  • A 3-dimensional vector.
  • A 4-dimensional vector.


  • Angles and their associated trigonometric functions.
  • Equality comparisons based on floating point tolerances.
  • An array containing elements of type Element
  • Base floating point types
  • Base numeric types with partial ordering
  • Numbers which have upper and lower bounds
  • Element-wise arithmetic operations. These are supplied for pragmatic reasons, but will usually fall outside of traditional algebraic properties.
  • Points in a Euclidean space with an associated space of displacement vectors.
  • Vectors that also have a dot (or inner) product.
  • A column-major matrix of arbitrary dimensions.
  • A type with a distance function between values.
  • Defines a multiplicative identity element for Self.
  • A trait for a generic rotation. A rotation is a transformation that creates a circular motion, and preserves at least one point in the space.
  • A two-dimensional rotation.
  • A three-dimensional rotation.
  • A column-major major matrix where the rows and column vectors are of the same dimensions.
  • A trait representing an affine transformation that can be applied to points or vectors. An affine transformation is one which
  • Vectors that can be added together and multiplied by scalars.
  • Defines an additive identity element for Self.


  • Dot product of two vectors.
  • Create a perspective matrix from a view frustum.
  • Create an orthographic projection matrix.
  • Create a perspective projection matrix.
  • The short constructor.
  • The short constructor.
  • The short constructor.
  • The short constructor.