Math Module

Math Module

The math module provides classes for linear algebra. Functions that are typically found in math libraries (e.g. trigonometry) are member functions on primitive classes like Integers and Reals.

Vec2

A Vec2 is a 2-component (Real values) vector class. Components can be accessed with the x and y shortcuts like var x = vector.x; or vector.y = 0.0;

Member Functions

Function Description
__init__() Constructor. When called without parameters, all components are set to 0.0. When called with 1 parameter, it is evaluated as a Real and assigned to all components. When called with 2 parameters, the components are set to the evaluated Reals.
__add__(vector) Add vector to object's value and return result. This member function is called when you use the add (+) operator.
__div__(vector) Divide object's value by vector and return result. This member function is called when you use the divide (/) operator.
__mod__(vector) Divide object's value by vector and return leftover. This member function is called when you use the modulo (%) operator.
__mul__(vector) Multiple vector with object's value and return result. This member function is called when you use the multiply (*) operator.
__sub__(vector) Subtract vector from object's value and return result. This member function is called when you use the subtract (-) operator.
abs() Returns a new vector with each component being the absolute value.
ceil() Returns a new vector with each component being the nearest integer that is greater than or equal.
clamp(min, max) Returns a new vector with each component constrained to the range min to max.
distance(point) Returns the distance between the 2 vectors (interpreted as points).
dot(vector) Returns the dot product of this vector and the provided vector.
floor() Returns a new vector with each component being the nearest integer that is less than or equal.
fract() Returns a new vector with each component being the fractional part of the original calculated as x - floor(x).
length() Returns the length of the vector.
max(vector) Returns a new vector with each component being the maximum of the two parameters.
min(vector) Returns a new vector with each component being the minimum of the two parameters.
mix(vector, interpolate) Returns a new vector with each component being linearly interpolated between this object and the provided vector using interpolate to weight between them.
normalize() Returns a new vector with the same direction but with length 1.
round() Returns a new vector with each component being the nearest integer.
sign() Returns a new vector with -1.0 if component is less than 0.0, 0.0 if component is equal to 0.0, and +1.0 if component is greater than 0.0.
smoothstep() Like step but it performs smooth Hermite interpolation between 0 and 1.
step(edge) Returns a new vector with each component being 0.0 if [i] < edge[i], and 1.0 otherwise.
trunc() Return a new vector with the value equal to the nearest integer to x whose absolute value is not larger than the absolute value of each component.

Overrides

The Vec2 class overrides the following member functions from the Object class:

  • __eq__
  • __ne__

Vec3

A Vec3 is a 3-component (Real values) vector class. Components can be accessed with the x, y and z shortcuts like var x = vector.x; or vector.z = 0.0;

Member Functions

Function Description
__init__() Constructor. When called without parameters, all components are set to 0.0. When called with 1 parameter, it is evaluated as a Real and assigned to all components. When called with 3 parameters, the components are set to the evaluated Reals.
__add__(vector) Add vector to object's value and return result. This member function is called when you use the add (+) operator.
__div__(vector) Divide object's value by vector and return result. This member function is called when you use the divide (/) operator.
__mod__(vector) Divide object's value by vector and return leftover. This member function is called when you use the modulo (%) operator.
__mul__(vector) Multiple vector with object's value and return result. This member function is called when you use the multiply (*) operator.
__sub__(vector) Subtract vector from object's value and return result. This member function is called when you use the subtract (-) operator.
abs() Returns a new vector with each component being the absolute value.
ceil() Returns a new vector with each component being the nearest integer that is greater than or equal.
clamp(min, max) Returns a new vector with each component constrained to the range min to max.
cross(vector) returns the cross product of this vector and the provided vector.
distance(point) Returns the distance between the 2 vectors (interpreted as points).
dot(vector) Returns the dot product of this vector and the provided vector.
floor() Returns a new vector with each component being the nearest integer that is less than or equal.
fract() Returns a new vector with each component being the fractional part of the original calculated as x - floor(x).
length() Returns the length of the vector.
max(vector) Returns a new vector with each component being the maximum of the two parameters.
min(vector) Returns a new vector with each component being the minimum of the two parameters.
mix(vector, interpolate) Returns a new vector with each component being linearly interpolated between this object and the provided vector using interpolate to weight between them.
normalize() Returns a new vector with the same direction but with length 1.
round() Returns a new vector with each component being the nearest integer.
sign() Returns a new vector with -1.0 if component is less than 0.0, 0.0 if component is equal to 0.0, and +1.0 if component is greater than 0.0.
smoothstep() Like step but it performs smooth Hermite interpolation between 0 and 1.
step(edge) Returns a new vector with each component being 0.0 if [i] < edge[i], and 1.0 otherwise.
trunc() Return a new vector with the value equal to the nearest integer to x whose absolute value is not larger than the absolute value of each component.

Overrides

The Vec3 class overrides the following member functions from the Object class:

  • __eq__
  • __ne__

Vec4

A Vec4 is a 4-component (Real values) vector class. Components can be accessed with the x, y, z and w shortcuts like var y = vector.y; or vector.w = 1.0;

Member Functions

Function Description
__init__() Constructor. When called without parameters, all components are set to 0.0. When called with 1 parameter, it is evaluated as a Real and assigned to all components. When called with 4 parameters, the components are set to the evaluated Reals.
__add__(vector) Add vector to object's value and return result. This member function is called when you use the add (+) operator.
__div__(vector) Divide object's value by vector and return result. This member function is called when you use the divide (/) operator.
__mod__(vector) Divide object's value by vector and return leftover. This member function is called when you use the modulo (%) operator.
__mul__(vector) Multiple vector with object's value and return result. This member function is called when you use the multiply (*) operator.
__sub__(vector) Subtract vector from object's value and return result. This member function is called when you use the subtract (-) operator.
abs() Returns a new vector with each component being the absolute value.
ceil() Returns a new vector with each component being the nearest integer that is greater than or equal.
clamp(min, max) Returns a new vector with each component constrained to the range min to max.
distance(point) Returns the distance between the 2 vectors (interpreted as points).
dot(vector) Returns the dot product of this vector and the provided vector.
floor() Returns a new vector with each component being the nearest integer that is less than or equal.
fract() Returns a new vector with each component being the fractional part of the original calculated as x - floor(x).
homogenize() Returns a new vector converting homogeneous coordinates to 3D Cartesian coordinates by dividing its x, y, and z components by its w component (also known as perspective division).
length() Returns the length of the vector.
max(vector) Returns a new vector with each component being the maximum of the two parameters.
min(vector) Returns a new vector with each component being the minimum of the two parameters.
mix(vector, interpolate) Returns a new vector with each component being linearly interpolated between this object and the provided vector using interpolate to weight between them.
normalize() Returns a new vector with the same direction but with length 1.
round() Returns a new vector with each component being the nearest integer.
sign() Returns a new vector with -1.0 if component is less than 0.0, 0.0 if component is equal to 0.0, and +1.0 if component is greater than 0.0.
smoothstep() Like step but it performs smooth Hermite interpolation between 0 and 1.
step(edge) Returns a new vector with each component being 0.0 if [i] < edge[i], and 1.0 otherwise.
trunc() Return a new vector with the value equal to the nearest integer to x whose absolute value is not larger than the absolute value of each component.

Overrides

The Vec4 class overrides the following member functions from the Object class:

  • __eq__
  • __ne__

Mat4

A Mat4 is a 4x4 floating-point matrix commonly used for 3D graphics transformations like scaling, rotation, translation, and projection. Matrix values are stored in column-major order and can be accessed using the valueCR shortcut where C is a zero-based column number and R is a zero-based row number (e.g. var cell = matrix.value12; or matrix.value30 = 0.1);

Rows and columns can be accessed with the rowR and columnC where R and C are the same zero-based numbers mentioned above. Rows and columns are Vec4 objects (e.g. var row = matrix.row2; or matrix.column3 = Vec4(0.0));

Member Functions

Function Description
__init__() Constructor. When called without parameters, the matrix is set to identity. When called with 1 parameter, it is evaluated as a Real and assigned to all cells. When called with 16 parameters, the cells are set to the evaluated Reals in column-major order (value00, value01, value02, value03, value10, etc).
__add__(matrix) Add matrix to object's value and return result. This member function is called when you use the add (+) operator.
__mul__(operand) Multiple operand with object's value and return result. Operand can be a Mat4 or a Vec4. This member function is called when you use the multiply (*) operator.
__sub__(matrix) Subtract matrix from object's value and return result. This member function is called when you use the subtract (-) operator.
inverse() Returns the inverse of the matrix.
rotate(vector) Create a rotation matrix based on provided vector, apply to object's value and return new matrix.
scale(vector) Create a scaling matrix based on provided vector, apply to object's value and return new matrix.
translate(vector) Create a translation matrix based on provided vector, apply to object's value and return new matrix.
transpose() Return a new matrix with rows and columns swapped.

Overrides

The Mat4 class overrides the following member functions from the Object class:

  • __eq__
  • __ne__