Skip to content

Math

Data structures that are useful when working with lineal algebra.

Vec2

Two dimensional vector data structure.

Fields

  • x First component of the vector. (float)
  • y Second component of the vector. (float)

Constructors

  • Vec2(float n) Take a scalar and make all components this value.
  • Vec2(float x, float y) Take one scalar for each component.
  • Vec2(Vec3 v) Convert a Vec3 to Vec2, losing the third component.
  • Vec2(Vec4 v) Convert a Vec4 to Vec2, losing the third and fourth component.

Methods

  • [] Operator overload to access each respective component.
  • +(Vec2 i, Vec2 j) Operator overload.
  • -(Vec2 i, Vec2 j) Operator overload.
  • *(Vec2 i, float k) Operator overload, scalar affects each component.
  • /(Vec2 i, float k) Operator overload, scalar affects each component.
  • +(Vec2 i, float k) Operator overload, scalar affects each component.
  • -(Vec2 i, float k) Operator overload, scalar affects each component.
  • Set(float x, float y) Set the components of the Vec2.
  • MagnitudeSqrt() Calculates the magnitude of the Vec2, squared.
  • Magnitude() Calculates the magnitude of the Vec2.
  • Normalize() Normalize the Vec2 to a length of 1.
  • Dot(Vec2 other) Calculates the dot product of two Vec2.
  • Angle(Vec2 other) Calculates the angle respective to other Vec2.
  • DistanceSqrt(Vec2 other) Calculate the distance between two Vec2, squared.
  • Distance(Vec2 other) Calculate the distance between two Vec2.

  • static down() Returns Vec2(0, -1).

  • static left() Returns Vec2(-1, 0).
  • static up() Returns Vec2(0, 1).
  • static right() Returns Vec2(1, 0).
  • static zero() Returns Vec2(0).
  • static one() Returns Vec2(1).

Example

using System;
using VisualKey;

class App {

  Window window;
  Vec2 pos = new Vec2(0);

  void Start() {
    var x = new Vec2(10, 20);
    x += 3;
    Console.WriteLine(x); // (x: 13, y: 23)

    var d = x.Dot(new Vec2(20, 10));
    Console.WriteLine(d); // 490

    var n = x.Normalize();
    Console.WriteLine(n); // (x: 0.4920573, y: 0.8705629)

    window = new Window((uint)size.x, (uint)size.y);
  }

  void Update() {
    if (Key.A.IsPressed())
      pos.x += Time.deltaTime * 1;
    Console.WriteLine("x: " + pos.x); // Same as pos[0]
  }

}

Vec3

Three dimensional vector data structure.

Fields

  • x First component of the vector. (float)
  • y Second component of the vector. (float)
  • z Third component of the vector. (float)

Constructors

  • Vec3(float n) Take a scalar and make all components this value.
  • Vec3(float x, float y, float z) Take one scalar for each component.
  • Vec3(Vec2 v, float z) Convert a Vec2 to Vec3, adding the third component.
  • Vec3(Vec4 v) Convert a Vec4 to Vec3, losing the fourth component.

Methods

  • [] Operator overload to access each respective component.
  • *(Vec3 v, Mat3 m) Operator overload, multiply Vec3 to Mat3.
  • +(Vec3 i, Vec3 j) Operator overload.
  • -(Vec3 i, Vec3 j) Operator overload.
  • *(Vec3 i, float k) Operator overload, scalar affects each component.
  • /(Vec3 i, float k) Operator overload, scalar affects each component.
  • +(Vec3 i, float k) Operator overload, scalar affects each component.
  • -(Vec3 i, float k) Operator overload, scalar affects each component.
  • Set(float x, float y, float z) Set the components of the Vec3.
  • MagnitudeSqrt() Calculates the magnitude of the Vec3, squared.
  • Magnitude() Calculates the magnitude of the Vec3.
  • Normalize() Normalize the Vec3 to a length of 1.
  • Dot(Vec3 other) Calculates the dot product of two Vec3.
  • Angle(Vec3 other) Calculates the angle respective to other Vec3.
  • Cross(Vec3 other) Calculates the cross product respective to other Vec3.
  • DistanceSqrt(Vec3 other) Calculate the distance between two Vec3, squared.
  • Distance(Vec3 other) Calculate the distance between two Vec3.
  • RGB() Return a new Vec3 with same components divided by 255.0f.

  • static back() Returns Vec3(0, 0, -1).

  • static down() Returns Vec3(0, -1, 0).
  • static left() Returns Vec3(-1, 0, 0).
  • static forward() Returns Vec3(0, 0, 1).
  • static up() Returns Vec3(0, 1, 0).
  • static right() Returns Vec3(1, 0, 0).
  • static zero() Returns Vec3(0).
  • static one() Returns Vec3(1).

Example

using System;
using VisualKey;

class App {

  Window window;
  Vec3 pos = new Vec3(0);

  void Start() {
    var x = new Vec3(10, 20, 30);
    x += 3;
    Console.WriteLine(x); // (x: 13, y: 23, z: 33)

    var d = x.Dot(new Vec3(30, 20, 10));
    Console.WriteLine(d); // 1180

    var n = x.Normalize();
    Console.WriteLine(n); // (x: 0.3075255, y: 0.5440835, z: 0.7806416)

    window = new Window(1280, 720);
  }

  void Update() {
    if (Key.A.IsPressed())
      pos.x += Time.deltaTime * 1;
    Console.WriteLine("x: " + pos.x); // Same as pos[0]
  }

}

Vec4

Four dimensional vector data structure.

Fields

  • x First component of the vector. (float)
  • y Second component of the vector. (float)
  • z Third component of the vector. (float)
  • z Fourth component of the vector. (float)

Constructors

  • Vec4(float n) Take a scalar and make all components this value.
  • Vec4(float x, float y, float z, float w) Take one scalar for each component.
  • Vec4(Vec2 v, float z, float w) Convert a Vec2 to Vec4, adding the third and fourth component.
  • Vec4(Vec3 v, float w) Convert a Vec3 to Vec4, adding the fourth component.
  • Vec4(Quat q) Convert a Quat to Vec4.

Methods

  • [] Operator overload to access each respective component.
  • +(Vec4 i, Vec4 j) Operator overload.
  • -(Vec4 i, Vec4 j) Operator overload.
  • *(Vec4 i, float k) Operator overload, scalar affects each component.
  • /(Vec4 i, float k) Operator overload, scalar affects each component.
  • +(Vec4 i, float k) Operator overload, scalar affects each component.
  • -(Vec4 i, float k) Operator overload, scalar affects each component.
  • Set(float x, float y, float z, float w) Set the components of the Vec4.
  • MagnitudeSqrt() Calculates the magnitude of the Vec4, squared.
  • Magnitude() Calculates the magnitude of the Vec4.
  • Normalize() Normalize the Vec4 to a length of 1.
  • Dot(Vec4 other) Calculates the dot product of two Vec4.
  • Angle(Vec4 other) Calculates the angle respective to other Vec4.
  • DistanceSqrt(Vec4 other) Calculate the distance between two Vec4, squared.
  • Distance(Vec4 other) Calculate the distance between two Vec4.
  • RGBA() Return a new Vec4 with same components divided by 255.0f, except the fourth which is divided by 100.0f.

  • static zero() Returns Vec4(0).

  • static one() Returns Vec4(1).

Example

using System;
using VisualKey;

class App {

  Window window;
  Vec4 pos = new Vec4(0);

  void Start() {
    var x = new Vec4(10, 20, 30, 40);
    x += 3;
    Console.WriteLine(x); // (x: 13, y: 23, z: 33)

    var d = x.Dot(new Vec4(40, 30, 20, 10));
    Console.WriteLine(d); // 2300

    var n = x.Normalize();
    Console.WriteLine(n); // (x: 0.2155914, y: 0.3814309, z: 0.5472704, w: 0.71311)

    window = new Window(1280, 720);
  }

  void Update() {
    if (Key.A.IsPressed())
      pos.x += Time.deltaTime * 1;
    Console.WriteLine("x: " + pos.x); // Same as pos[0]
  }

}

Mat3

Three dimensional matrix data structure.

Fields

  • row0 First component of the matrix. (Vec3)
  • row1 Second component of the matrix. (Vec3)
  • row2 Third component of the matrix. (Vec3)

Constructors

  • Mat3(float n) Take a scalar and make all components this value.
  • Mat3(Vec3 row0, Vec3 row1, Vec3 row2) Take three Vec3 and make each row this value.
  • Take one scalar per each component of the Mat3:
    Mat3(float v00, float v01, float v02,
         float v03, float v04, float v05,
         float v06, float v07, float v08)
    

Methods

  • [] Operator overload to access each respective component.
  • *(Mat3 m, float k) Operator overload, scalar affects each component.
  • /(Mat3 m, float k) Operator overload, scalar affects each component.
  • +(Mat3 m, float k) Operator overload, scalar affects each component.
  • -(Mat3 m, float k) Operator overload, scalar affects each component.
  • *(Mat3 m, Mat3 other) Multiply two Mat3.
  • ToArray() Convert Mat3 to float[9].

  • static identity() Returns an identity Mat3.

  • static zero() Returns Mat3(0).
  • static one() Returns Mat3(1).

Example

using System;
using VisualKey;

class App {

  void Start() {
    var m = new Mat3(1, 2, 3,
                     4, 5, 6,
                     7, 8, 9);
    var o = m * Mat3.identity();
    Console.WriteLine(o);
  }

}

Mat4

Four dimensional matrix data structure.

Fields

  • row0 First component of the matrix. (Vec4)
  • row1 Second component of the matrix. (Vec4)
  • row2 Third component of the matrix. (Vec4)
  • row3 Fourth component of the matrix. (Vec4)

Constructors

  • Mat4(float n) Take a scalar and make all components this value.
  • Mat4(Vec4 row0, Vec4 row1, Vec4 row2, Vec4 row3) Take four Vec4 and make each row this value.
  • Take one scalar per each component of the Mat4:
    Mat4(float v00, float v01, float v02, float v03,
         float v04, float v05, float v06, float v07,
         float v08, float v09, float v10, float v11,
         float v12, float v13, float v14, float v15)
    

Methods

  • [] Operator overload to access each respective component.
  • *(Mat4 m, float k) Operator overload, scalar affects each component.
  • /(Mat4 m, float k) Operator overload, scalar affects each component.
  • +(Mat4 m, float k) Operator overload, scalar affects each component.
  • -(Mat4 m, float k) Operator overload, scalar affects each component.
  • *(Mat4 m, Mat4 other) Multiply two Mat4.
  • ToArray() Convert Mat4 to float[16].
  • Scale(Vec3 elements)
  • LookAt(Vec3 eye, Vec3 at, Vec3 up)
  • Ortho(float left, float right, float bottom, float top, float zNear, float zFar)
  • Perspective(float fov, float aspect, float zNear, float zFar)
  • RotateX(float angle)
  • RotateY(float angle)
  • RotateZ(float angle)
  • Translate(Vec3 offset)

  • static identity() Returns an identity Mat3.

  • static zero() Returns Mat3(0).
  • static one() Returns Mat3(1).

Example

using System;
using VisualKey;

class App {

  void Start() {
    var m = new Mat4(1, 2, 3, 4,
                     5, 6, 7, 8,
                     9, 0, 1, 2,
                     3, 4, 5, 6);
    var o = m * Mat4.identity();
    Console.WriteLine(o);
  }

}

Quat

Quaternion data structure.

Fields

  • x First component of the quaternion. (float)
  • y Second component of the quaternion. (float)
  • z Third component of the quaternion. (float)
  • w Fourth component of the quaternion. (float)

Constructors

  • Quat(float n) Take a scalar and make all components this value.
  • Quat(float x, float y, float z, float w) Take a scalar per each component.
  • Quat(Vec4 v) Take a Vec4 and convert it to Quat.
  • Quat(Vec3 axis, float angle) Take a Vec3 and convert it to Quat by providing an angle.
  • Quat(Mat3 m) Convert a Mat3 to Quat.

Methods

  • [] Operator overload to access each respective component.
  • *(Quat q, float k) Operator overload, scalar affects each component.
  • /(Quat q, float k) Operator overload, scalar affects each component.
  • +(Quat q, float k) Operator overload, scalar affects each component.
  • -(Quat q, float k) Operator overload, scalar affects each component.
  • *(Quat q, Quat other) Operator overload, multiply two Quat.
  • *(Quat q, Vec3 v) Operator overload, multiply Quat with a Vec3.
  • Set(float x, float y, float z, float w) Set each component value.
  • Conjugate()
  • Inverse()
  • Rotate(float angle, Vec3 v)
  • Roll()
  • Pitch()
  • Yaw()
  • EulerAngles()
  • Dot()
  • Mat3() Convert Quat to Mat3.
  • Mat4()
  • Mat4(Vec4 v)
  • Mat4(Vec3 v)

  • static identity() Returns an identity Mat3.

  • static zero() Returns Mat3(0).
  • static one() Returns Mat3(1).

MathV

Math utilities to work with float values.

  • Radians(float angle) Convert an angle to radians.
  • Clamp(float v, float l, float r)