-
Notifications
You must be signed in to change notification settings - Fork 34
Linear Algebra
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.
(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;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 5sVector[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
T length(Vector<T,N> v) returns the length of the given vector
T lengthSquared(Vector<T,N> v) returns the squared length of the vector, useful for reducing square roots
Vector<T,N> normalize(Vector<T,N> v) returns a vector in the same direction as the given vector, but with length 1
Vector<T,N> withLength(Vector<T,N> v, T length) like normalize, but returns a vector with the requested length
Vector<T,N> maxLength(Vector<T,N> v, T maxLength) if the given vector has length(v) > maxLength then returns withLength(v, maxLength) else return v
Vector<T,N> minLength(Vector<T,N> v, T minLength) if the given vector has length(v) < maxLength then returns withLength(v, maxLength) else return v
T angleBetween(Vector<T,N> a, Vector<T,N> b) returns the angle between the two given vectors, computed using acos(normalize(first) * normalize(second))
Vector<T,N> bisect(Vector<T,N> a, Vector<T,N> b) returns a vector that is the bisection of the two given vectors, with unspecified length. Such that angleBetween(a,bisect(a,b)) == angleBetween(b,bisect(a,b))
Vector<T,N1+N2> join(Vector<T,N1> a, Vector<T,N2> b) joins the two given vectors into one larger vector
Matrix + Matrix => addition
Matrix - Matrix => subtraction
Matrix * Matrix => Matrix-Matrix product
Matrix * Vector => Matrix-Vector product