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 aVec3
toVec2
, losing the third component.Vec2(Vec4 v)
Convert aVec4
toVec2
, 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 theVec2
.MagnitudeSqrt()
Calculates the magnitude of theVec2
, squared.Magnitude()
Calculates the magnitude of theVec2
.Normalize()
Normalize theVec2
to a length of 1.Dot(Vec2 other)
Calculates the dot product of twoVec2
.Angle(Vec2 other)
Calculates the angle respective to otherVec2
.DistanceSqrt(Vec2 other)
Calculate the distance between twoVec2
, squared.-
Distance(Vec2 other)
Calculate the distance between twoVec2
. -
static down()
ReturnsVec2(0, -1)
. static left()
ReturnsVec2(-1, 0)
.static up()
ReturnsVec2(0, 1)
.static right()
ReturnsVec2(1, 0)
.static zero()
ReturnsVec2(0)
.static one()
ReturnsVec2(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 aVec2
toVec3
, adding the third component.Vec3(Vec4 v)
Convert aVec4
toVec3
, losing the fourth component.
Methods
[]
Operator overload to access each respective component.*(Vec3 v, Mat3 m)
Operator overload, multiplyVec3
toMat3
.+(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 theVec3
.MagnitudeSqrt()
Calculates the magnitude of theVec3
, squared.Magnitude()
Calculates the magnitude of theVec3
.Normalize()
Normalize theVec3
to a length of 1.Dot(Vec3 other)
Calculates the dot product of twoVec3
.Angle(Vec3 other)
Calculates the angle respective to otherVec3
.Cross(Vec3 other)
Calculates the cross product respective to otherVec3
.DistanceSqrt(Vec3 other)
Calculate the distance between twoVec3
, squared.Distance(Vec3 other)
Calculate the distance between twoVec3
.-
RGB()
Return a newVec3
with same components divided by255.0f
. -
static back()
ReturnsVec3(0, 0, -1)
. static down()
ReturnsVec3(0, -1, 0)
.static left()
ReturnsVec3(-1, 0, 0)
.static forward()
ReturnsVec3(0, 0, 1)
.static up()
ReturnsVec3(0, 1, 0)
.static right()
ReturnsVec3(1, 0, 0)
.static zero()
ReturnsVec3(0)
.static one()
ReturnsVec3(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 aVec2
toVec4
, adding the third and fourth component.Vec4(Vec3 v, float w)
Convert aVec3
toVec4
, adding the fourth component.Vec4(Quat q)
Convert aQuat
toVec4
.
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 theVec4
.MagnitudeSqrt()
Calculates the magnitude of theVec4
, squared.Magnitude()
Calculates the magnitude of theVec4
.Normalize()
Normalize theVec4
to a length of 1.Dot(Vec4 other)
Calculates the dot product of twoVec4
.Angle(Vec4 other)
Calculates the angle respective to otherVec4
.DistanceSqrt(Vec4 other)
Calculate the distance between twoVec4
, squared.Distance(Vec4 other)
Calculate the distance between twoVec4
.-
RGBA()
Return a newVec4
with same components divided by255.0f
, except the fourth which is divided by100.0f
. -
static zero()
ReturnsVec4(0)
. static one()
ReturnsVec4(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 threeVec3
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 twoMat3
.-
ToArray()
ConvertMat3
tofloat[9]
. -
static identity()
Returns an identityMat3
. static zero()
ReturnsMat3(0)
.static one()
ReturnsMat3(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 fourVec4
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 twoMat4
.ToArray()
ConvertMat4
tofloat[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 identityMat3
. static zero()
ReturnsMat3(0)
.static one()
ReturnsMat3(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 aVec4
and convert it toQuat
.Quat(Vec3 axis, float angle)
Take aVec3
and convert it toQuat
by providing an angle.Quat(Mat3 m)
Convert aMat3
toQuat
.
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 twoQuat
.*(Quat q, Vec3 v)
Operator overload, multiplyQuat
with aVec3
.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()
ConvertQuat
toMat3
.Mat4()
Mat4(Vec4 v)
-
Mat4(Vec3 v)
-
static identity()
Returns an identityMat3
. static zero()
ReturnsMat3(0)
.static one()
ReturnsMat3(1)
.
MathV
Math utilities to work with float
values.
Radians(float angle)
Convert an angle to radians.Clamp(float v, float l, float r)