11local function
abs(value)
22--! value\_min, & \
text{
if } value \gt value\_min \\
23--! value\_max, & \
text{
if } value \lt value\_max \\
24--! value, & \
text{
if } value\_min \lt value \lt value\_max
27--! @
param[in] value The value to clamstd.math.
28--! @
param[in] value_min The minimum value that value can be clamped to.
29--! @
param[in] value_max The maximum value that value can be clamped to.
30local function
clamp(value, value_min, value_max)
31 if value < value_min then
33 elseif value > value_max then
41--! @note similar to @ref
clamp but cyclical.
44--! (value - value\_min) \mod (value\_max - value\_min + 1) + value\_min
46--! @
param[in] value The value to clamstd.math.
47--! @
param[in] value_min The minimum value that value can be clamped to.
48--! @
param[in] value_max The maximum value that value can be clamped to.
49local function
clamp2(value, value_min, value_max)
50 return (value - value_min) % (value_max - value_min + 1) + value_min
53--! @
short periodic
cycle
57--! \frac{passed \mod duration}{duration}, & \
text{
if } (passed \mod duration \neq 0) \\
58--! \frac{passed \mod (2 \times duration)}{duration}, & \
text{
if } (passed \mod duration = 0)
62--! @
param[in] duration
63--! @retval 0 start of period
64--! @retval 0.5 middle of period
65--! @retval 1 end of period
68--! local anim =
std.math.cycle(
game.millis, 1000) * 5
69--!
std.draw.text(x, y + anim,
'hello!')
71local function
cycle(passed, duration)
72 local endtime = (passed) % duration
73 return ((endtime == 0 and (passed % (duration * 2)) or endtime)) / duration
80--! -1, & \
text{
if } |value| \gt \alpha \land value \lt 0 \\
81--! 1, & \
text{
if } |value| \gt \alpha \land value \gt 0 \\
82--! 0, & \
text{
if } |value| \leq \alpha
86--! @
param[in] alpha @c
default=0
87--! @retval -1 less than alpha
88--! @retval 0 when in alpha
89--! @retval 1 greater than alpha
93--! [-1] =
game.spr_player_left,
94--! [1] =
game.spr_player_right,
95--! [0] =
game.player_sprite
97--!
game.player_sprite = sprites[
std.math.dir(
game.player_speed_x)]
99local function
dir(value, alpha)
101 if value < -alpha then
103 elseif value > alpha then
110--! @brief euclidean distance
113--! \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}
115--! @
param[in] x1 The x coordinate of the first point.
116--! @
param[in] y1 The y coordinate of the first point.
117--! @
param[in] x2 The x coordinate of the second point.
118--! @
param[in] y2 The y coordinate of the second point.
119--! @
return distance between the two points (x1, y1) and (x2, y2).
120local function
dis(x1,y1,x2,y2)
121 return ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) ^ 0.5
124--! @brief quadratic distance
125--! @note
this is an optimization of @ref
dis but it cannot be used to calculate collisions.
128--! (x_2 - x_1)^2 + (y_2 - y_1)^2
130--! @
param[in] x1 The x coordinate of the first point.
131--! @
param[in] y1 The y coordinate of the first point.
132--! @
param[in] x2 The x coordinate of the second point.
133--! @
param[in] y2 The y coordinate of the second point.
134--! @
return distance between the two points (x1, y1) and (x2, y2).
135local function
dis2(x1,y1,x2,y2)
136 return (x2 - x1) ^ 2 + (y2 - y1) ^ 2
139--! @brief linear interpolation
142--! a + \alpha \cdot (b - a)
144--! @
param[in] a The starting value
145--! @
param[in] b The ending value
146--! @
param[in] alpha The interpolation parameter, typically in the range [0, 1].
147--! @
return The interpolated value between
'a' and
'b' based on
'alpha'.
148local function
lerp(a, b, alpha)
149 return a + alpha * ( b - a )
157--! (value - in\_min) \cdot \frac{(out\_max - out\_min)}{(in\_max - in\_min)} + out\_min
159--! @
param[in] value The value to be mapped
from the input range to the output range.
160--! @
param[in] in_min The minimum value of the input range.
161--! @
param[in] in_max The maximum value of the input range.
162--! @
param[in] out_min The minimum value of the output range.
163--! @
param[in] out_max The maximum value of the output range.
164--! @
return The mapped value in the output range corresponding to
'value' in the input range.
165local function
map(value, in_min, in_max, out_min, out_max)
166 return (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
172--! \frac{N_1 + N_2 - | N_1 - N_2 |}{2}
174local function
max(...)
178 local max_value = nil
184 while index <= #args
do
186 if max_value == nil or value > max_value then
198--! \frac{N_1 + N_2 + | N_1 + N_2 |}{2}
200local function
min(...)
204 local min_value = nil
210 while index <= #args
do
212 if min_value == nil or value < min_value then
225--! value \times 4, & \
text{
if } 0 \leq value < 0.25 \\
226--! 1 - ((value - 0.25) \times 4), & \
text{
if } 0.25 \leq value < 0.50 \\
227--! ((value - 0.50) \times 4) \times (-1), & \
text{
if } 0.50 \leq value < 0.75 \\
228--! ((value - 0.75) \times 4) - 1, & \
text{
if } 0.75 \leq value \leq 1 \\
231local function
saw(value)
234 elseif value < 0.50 then
235 return 1 - ((value - 0.25) * 4)
236 elseif value < 0.75 then
237 return ((value - 0.50) * 4) * (-1)
239 return ((value - 0.75) * 4) - 1
local function from(host_args)
local function require(std, game, application)
local function install_clib_random(std)
local function install_clib(std)
local function install(std)
local function param(self, name, value)
local function lerp(a, b, alpha)
linear interpolation
local function dis2(x1, y1, x2, y2)
quadratic distance
local function dir(value, alpha)
direction
local function dis(x1, y1, x2, y2)
euclidean distance
local function clamp(value, value_min, value_max)
clamp
local function min(...)
minimum
local function abs(value)
abs module
local function max(...)
maximum
local function cycle(passed, duration)
periodic cycle
local function saw(value)
sawtooth
local function clamp2(value, value_min, value_max)
clamp
local function map(value, in_min, in_max, out_min, out_max)
re-maps
local function text(x, y, text)