dcsex.math

Math - extensions to lua math.

Contents

  1. Functions
    1. _t.addStdDev(val, sigma)
    2. _t.bit2num(bitpos)
    3. _t.bitset2num(bitset)
    4. _t.centroid2D(point, pcenter, n)
    5. _t.clamp(x, min, max)
    6. _t.lerp(a, b, t)
    7. _t.isBitSet(bit, value)
    8. _t.isPointInCircle(center, radius, point)
    9. _t.randomPointInCircle(center, maxRadius, minRadius)
    10. _t.round(num)
    11. _t.toBoolean(val)

Functions

_t.addStdDev(val, sigma)

Parameters

val
base value
sigma
random value between +/- sigma

Returns

  • val + random(-sigma,sigma)

_t.bit2num(bitpos)

So for us humans if you look at 32 in binary (0b0010 0000) its really the 6th position from the right.

Parameters

bitpos
the zero indexed nth bit position to convert to a number

Returns

  • 2 ^ bitpos

_t.bitset2num(bitset)

Since Lua 5.1 doesn’t support bit manipulation this is our poor man’s attempt to represent a binary number or bitfield/flagset.

Parameters

bitset
is a table where the keys are integers and any non-false value will result in the bit being counted.

Returns

  • numerical representation of the bitset

_t.centroid2D(point, pcenter, n)

Parameters

point
the next point to include in the center calculation
pcenter
the center of all previous points calculated, nil if just starting
n
count of all previous points calculated, nil is starting

Returns

  • two values: updated center, number of points calculated

_t.clamp(x, min, max)

Parameters

x
the value to clamp
min
minimum allowed value
max
maximum allowed value

Returns

  • clamped value

_t.lerp(a, b, t)

Parameters

a
starting value
b
ending value
t
ratio between a and b

Returns

  • interpolated value between a and b

_t.isBitSet(bit, value)

Parameters

bit
the bit we are looking for
value
the value we want to test

Returns

  • true when bit is set, false otherwise

_t.isPointInCircle(center, radius, point)

Parameters

center
The center of the circle, as a Vec2
radius
The radius of the circle
point
The point to test inside circle as a Vec2

Returns

  • True if point is inside the circle, false otherwise

_t.randomPointInCircle(center, maxRadius, minRadius)

Parameters

center
Center of the circle as a Vec2
maxRadius
Radius of the circle
minRadius
(optional) Minimum inner radius circle in which points should not be spawned

Returns

  • A Vec2

_t.round(num)

Parameters

num
a floating point number

Returns

  • num rounded to whole number

_t.toBoolean(val)

Parameters

val
Value to convert

Returns

  • A boolean, nil is considered false