lubyk logo

Lubyk documentation

2D vector

Immutable 2D vector class.

The vectors are immutable when used through this API and it is highly discouraged to change a vector in place as this may break future code.

Simple usage example:

local v = lug.V2(3, 4)
print(v:norm())
--> 5

-- Access x component can be done with #x method or `[1]`.
print(v[1], v:x())
--> 2  2

-- Add two vectors
print(v + lug.V2(2, 1))
--> (5 5)

Constructor

.new (x, y)

Create a vector with the corresponding components.

-- Create new vector from two numbers.
local v = lug.V2(0.5, 12)

Conversion

With a single argument, lug.V2 returns a vector by copying the first two values of the argument ([] accessor). This enables easy transformation from other vectors such as lug.V3 or lug.V4.

-- Copy x and y values from a lug.V3.
local v2 = lug.V2(v3)

.polarUnit (theta)

A unit vector with \(\theta\) polar coordinate.

Constants

.zero ()

Vector (0, 0).

.ox ()

Unit vector (1, 0).

.oy ()

Unit vector (0, 1).

.huge ()

A vector whose components are math.huge.

.negHuge ()

A vector whose components are -math.huge.

Accessors

:x ()

Get x component. Also supported as v[1] which is faster.

:y ()

Get y component. Also supported as v[2] which is faster.

:tuple ()

Get x, y components as tuple.

:tostring ()

A textual representation of the vector.

Functions

Functions neg, add, sub and smul are also accessible through operators:

local v  = lug.V2(1,2)
local nv = -v
local w  = v + nv
local n  = 4 * v
local m  = v * 3

.neg (v)

Inverse vector \(-\mathbf{v}\).

.add (u, v)

Add two vectors: \(\mathbf{u} + \mathbf{v}\).

.sub (u, v)

Subtract two vectors: \(\mathbf{u} - \mathbf{v}\).

.mul (u, v)

Component wise multiplication: \((\ \mathbf{u}_x \mathbf{v}_x,\ \ \mathbf{u}_y \mathbf{v}_y\ )\).

.div (u, v)

Component wise division: \((\ \mathbf{u}_x / \mathbf{v}_x,\ \ \mathbf{u}_y / \mathbf{v}_y\ )\).

.smul (s, v)

Scalar multiplication: \(s\mathbf{v}\).

.half (v)

Half vector: \(\mathbf{v} / 2\).

.dot (u, v)

Dot product: \(\mathbf{u}\cdot\mathbf{v}\).

.norm (v)

Norm: \(|\mathbf{v}|\).

.norm2 (v)

Squared norm: \(|\mathbf{v}|^2\).

.unit (v)

Unit vector: \(\mathbf{v}/|\mathbf{v}|\).

.ortho (v)

Rotated vector by \(\pi/2\).

.mix (u, v, t)

Linear interpolation \(\mathbf{u} + t(\mathbf{v}-\mathbf{u})\).

Taversal

.map (v, f)

Map coordinates wifh f function: \((\ f(\mathbf{v}_x),\ \ f(\mathbf{v}_y)\ )\).

.mapi (v, f)

Map coordinates with f function passing i: \((\ f(1,\mathbf{v}_x),\ \ f(2,\mathbf{v}_y)\ )\).

.fold (v, acc, f)

Fold vector coordinates with accumulator acc: \(f(f(acc, \mathbf{v}_x), \mathbf{v}_y)\).

.foldi (v, acc, f)

Fold vector coordinates with accumulator acc and index: \(f(f(acc, 1, \mathbf{v}_x), 2, \mathbf{v}_y)\).

.iter (v, f)

Iterator against coordinate values: \(f(\mathbf{v}_x);\ f(\mathbf{v}_y)\). If no f function is passed, the iterator is expected to be used in a for loop:

for c in v:iter() do
  -- do something with coordinate value c
end

.iteri (v, f)

Iterator against coordinate values with index: \(f(1, \mathbf{v}_x);\ f(1, \mathbf{v}_y)\). If no f function is passed, the iterator is expected to be used in a for loop:

for i, c in v:iteri() do
  -- do something with index i and coordinate value c
end

Predicates

.forAll (v, p)

True if all \(p(v_i)\) are true: \(p(\mathbf{v}_x) \land p(\mathbf{v}_y)\).

.exists (v, p)

True if any \(p(v_i)\) is true: \(p(\mathbf{v}_x) \lor p(\mathbf{v}_y)\)

.eq (u, v, eq)

True if u and v are equal component wise: \(\mathbf{u}_x = \mathbf{v}_x \land \mathbf{u}_y = \mathbf{v}_y\)

If eq function is provided, it is used to evaluate equality.

.lt (u, v, lt)

True if u is lower then v component wise: \(\mathbf{u}_x < \mathbf{v}_x \land \mathbf{u}_y < \mathbf{v}_y\)

If lt function is provided, it is used as comparison function.

.le (u, v, le)

True if u is lower or equal to v component wise: \(\mathbf{u}_x \leq \mathbf{v}_x \land \mathbf{u}_y \leq \mathbf{v}_y\)

If le function is provided, it is used as comparison function.

.compare (u, v, cmp)

Compare two vectors and return:

-1if \(\mathbf{u}_x < \mathbf{v}_x \lor (\mathbf{u}_x = \mathbf{v}_x \land \mathbf{u}_y < \mathbf{v}_y)\)
0if \(\mathbf{u}_x = \mathbf{v}_x \land \mathbf{u}_y = \mathbf{v}_y\)
1if \(\mathbf{u}_x > \mathbf{v}_x \lor (\mathbf{u}_x = \mathbf{v}_x \land \mathbf{u}_y > \mathbf{v}_y)\)

If cmp function is provided, it is used as comparison function (should return -1, 0, 1).

Operators

.__unm = lib.neg

Inverse vector \(-\mathbf{v}\).

.__add = lib.add

Add two vectors: \(\mathbf{u} + \mathbf{v}\).

.__sub = lib.sub

Subtract two vectors: \(\mathbf{u} - \mathbf{v}\).

.__mul = lib.smul

Scalar multiplication: \(s\mathbf{v}\).

.__tostring = lib.tostring

A textual representation of the vector. Used in print(v) for example.

.__eq = lib.eq

Component wise equality

.__lt = lib.lt

Same as lt.

.__le = lib.le

Same as le.