Skip to content

Linear Algebra

VonTum edited this page Oct 21, 2020 · 3 revisions

Linear Algebra

Vector<T, Size>

Vectors can be created with an element type and a vector Size. For size 2-4 the fields x, y, z, w are added for ease of use.

Predefined Aliases:

(these are the names you'll often see in the code)

typedef Vector<double, 2>    Vec2;
typedef Vector<float, 2>     Vec2f;
typedef Vector<long long, 2> Vec2l;
typedef Vector<int, 2>       Vec2i;

typedef Vector<double, 3>    Vec3;
typedef Vector<float, 3>     Vec3f;
typedef Vector<long long, 3> Vec3l;
typedef Vector<int, 3>       Vec3i;

typedef Vector<double, 4>    Vec4;
typedef Vector<float, 4>     Vec4f;
typedef Vector<long long, 4> Vec4l;
typedef Vector<int, 4>       Vec4i;

Construction

For sizes 2-4 there is the () constructor

Vec3 v = Vec3(4.0, 5.0, 6.0);
v.x // 4.0
v.y // 5.0
v.z // 6.0
v[2] // == v.z == 6.0

Vector<int, 6> v6{1,2,3,4,5,6};

You can also create a vector where every field is the same value using Vector::full

Vector<int, 7> v7 = Vector<int, 7>::full(5); // creates a vector of size 7 with all 5s

Operators

Vector[n] access the n-th element of the vector
Vector + Vector Addition
Vector - Vector Subtraction
Vector * Vector dot product
Vec3 % Vec3 cross product
Scalar * Vector Multiply vector by a scalar
Vector * Scalar Multiply vector by a scalar
Vector / Scalar Divide vector by a scalar

Standalone functions

Matrix + Matrix => addition
Matrix - Matrix => subtraction
Matrix * Matrix => Matrix-Matrix product
Matrix * Vector => Matrix-Vector product

Clone this wiki locally