Skip to content

Sandbox: Lua: Math: Matrix

Аниса edited this page Sep 15, 2022 · 1 revision

━ What's the Objective?

Matrices are collection of variables represented in tabular format (Rows & Columns). Often we've have to deal with multiple coordinates when either dealing with general or complex mathematics; Matrices do help a lot to make the workflow easier in such scenarios. Moreover they are also widely used in games for representing element's positional/rotational matrix.

If you still have no idea how useful they are or where to apply/utilize them in the right way, then make sure to read: https://personalpages.manchester.ac.uk/staff/ctdodson/matrices.pdf

━ Operations

Addition

local cMatrix1, cMatrix2 = math.matrix({1, 1, 1, 1}, {1, 1, 1, 1}), math.matrix({2, 2, 2, 2}, {2, 2, 2, 2})
local resultant = cMatrix1 + cMatrix2

Subtraction

local cMatrix1, cMatrix2 = math.matrix({1, 1, 1, 1}, {1, 1, 1, 1}), math.matrix({2, 2, 2, 2}, {2, 2, 2, 2})
local resultant = cMatrix1 - cMatrix2

Multiplication

local cMatrix1, cMatrix2 = math.matrix({1, 1, 1, 1}, {1, 1, 1, 1}), math.matrix({2, 2, 2, 2}, {2, 2, 2, 2})
local resultant = cMatrix1 * cMatrix2

Division

local cMatrix1, cMatrix2 = math.matrix({1, 1, 1, 1}, {1, 1, 1, 1}), math.matrix({2, 2, 2, 2}, {2, 2, 2, 2})
local resultant = cMatrix1 / cMatrix2

━ APIs

━ math.matrix:getType() (Shared)

@Objective: Retrieve's instance's type.
local string: type = self:getType()

━ math.matrix() (Shared)

@Objective: Creates a matrix instance.
local matrix: cMatrix = math.matrix(
  ~: ...{~float: ...values}
)

━ math.matrix:destroy() (Shared)

@Objective: Destroys an existing matrix.
local bool: result = self:destroy()

━ math.matrix:scale() (Shared)

@Objective: Scales matrix w/ specified scale.
local matrix: self = self:scale(
  float: scale
)

━ math.matrix:transform() (Shared)

@Objective: Transforms a valid location matrix.
--Note: All coordinates must be offsets, i.e, they must be relative
local matrix: cMatrix = self:transform(
  matrix: rotationMatrix,
  float: x,
  float: y,
  float: z
)
Clone this wiki locally