Skip to content

Basic Usage

Dickson Law edited this page Jul 28, 2025 · 3 revisions

Basic Usage

GMLinear 2 allows you to use plain arrays as vectors and make calculations on them. Here are some examples:

v1 = [-2, 4, 5];
v2 = [1, -3, 6];

v1_1 = v1[1]; //4
v1_plus_v2 = r3_add(v1, v2); //[-1, 1, 11]
v1_minus_v2 = r3_subtract(v1, v2); //[-3, 7, -1]
v1_dot_v2 = r3_dot(v1, v2); //16
v1_cross_v2 = r3_cross(v1, v2); //[39, 17, 2]

Matrices are represented as nested arrays (formerly known as 2D arrays). Here is some examples:

M1 = [
    [1, 2],
    [4, 5]
];
M2 = [
    [-4, -3],
    [2, 3]
];

v = [-1, 1];

M1_1_1 = M1[1][1]; //5
M1_plus_M2 = r22_add(M1, M2); //[[-3, -1], [6, 8]]
M1_minus_M2 = r22_subtract(M1, M2); //[[5, 5], [2, 2]]
M1_double = r22_scale(M1, 2); //[[2, 4], [8, 10]]
M1_times_M2 = r22_multiply(M1, M2); //[[0, 3], [-6, 3]]
M1_times_v = r22_transform(M1, v); //[1, 1]

Overwriting an Existing Vector/Matrix

GMLinear 2 functions that return a vector or matrix result have an optional [vout] or [Mout] parameter. If supplied, it will overwrite the provided vector or matrix, and return that directly instead.

Compatibility Note: There used to be separate copies of these functions suffixed with _to. As of GMLinear 2.3.0, the functions ending in _to are deprecated and left in place only as macro aliases.

v1 = [-2, 4, 5];
v2 = [1, -3, 6];

r3_add(v1, v2, v2);
show_message(v1); //[ -2, 4, 5 ]
show_message(v2); //[ -1, 1, 11 ]
M1 = [
    [1, 2],
    [4, 5]
];
M2 = [
    [-4, -3],
    [2, 3]
];
M3 = r22_zero();

show_message(M3); //[ [ 0, 0 ], [ 0, 0 ] ]
r22_multiply(M1, M2, M3);
show_message(M3); // [[ 0, 3 ], [ -6, 3 ] ]

Clone this wiki locally