Engine 0.0.4
Game engine in lua
Loading...
Searching...
No Matches
math.lua
Go to the documentation of this file.
1--! @defgroup std
2--! @{
3--! @defgroup math
4--! @{
5
6--! @short abs module
7--! @par Equation
8--! @f$ |value| @f$
9--! @param[in] value
10--! @return number
11local function abs(value)
12 if value < 0 then
13 return -value
14 end
15 return value
16end
17
18--! @short clamp
19--! @par Equation
20--! @f$
21--! \begin{cases}
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
25--! \end{cases}
26--! @f$
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
32 return value_min
33 elseif value > value_max then
34 return value_max
35 else
36 return value
37 end
38end
39
40--! @short clamp
41--! @note similar to @ref clamp but cyclical.
42--! @par Equation
43--! @f$
44--! (value - value\_min) \mod (value\_max - value\_min + 1) + value\_min
45--! @f$
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
51end
52
53--! @short periodic cycle
54--! @par Equation
55--! @f$
56--! \begin{cases}
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)
59--! \end{cases}
60--! @f$
61--! @param[in] passed
62--! @param[in] duration
63--! @retval 0 start of period
64--! @retval 0.5 middle of period
65--! @retval 1 end of period
66--! @par Example
67--! @code
68--! local anim = std.math.cycle(game.millis, 1000) * 5
69--! std.draw.text(x, y + anim, 'hello!')
70--! @endcode
71local function cycle(passed, duration)
72 local endtime = (passed) % duration
73 return ((endtime == 0 and (passed % (duration * 2)) or endtime)) / duration
74end
75
76--! @short direction
77--! @par Equation
78--! @f$
79--! \begin{cases}
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
83--! \end{cases}
84--! @f$
85--! @param[in] value
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
90--! @par Example
91--! @code
92--! local sprites = {
93--! [-1] = game.spr_player_left,
94--! [1] = game.spr_player_right,
95--! [0] = game.player_sprite
96--! }
97--! game.player_sprite = sprites[std.math.dir(game.player_speed_x)]
98--! @endcode
99local function dir(value, alpha)
100 alpha = alpha or 0
101 if value < -alpha then
102 return -1
103 elseif value > alpha then
104 return 1
105 else
106 return 0
107 end
108end
109
110--! @brief euclidean distance
111--! @par Equation
112--! @f$
113--! \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}
114--! @f$
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
122end
123
124--! @brief quadratic distance
125--! @note this is an optimization of @ref dis but it cannot be used to calculate collisions.
126--! @par Equation
127--! @f$
128--! (x_2 - x_1)^2 + (y_2 - y_1)^2
129--! @f$
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
137end
138
139--! @brief linear interpolation
140--! @par Equation
141--! @f$
142--! a + \alpha \cdot (b - a)
143--! @f$
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 )
150end
151
152--! @brief re-maps
153--! @li <https://www.arduino.cc/reference/en/language/functions/math/map>
154--!
155--! @par Equation
156--! @f$
157--! (value - in\_min) \cdot \frac{(out\_max - out\_min)}{(in\_max - in\_min)} + out\_min
158--! @f$
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
167end
168
169--! @short maximum
170--! @par Equation
171--! @f$
172--! \frac{N_1 + N_2 - | N_1 - N_2 |}{2}
173--! @f$
174local function max(...)
175 local args = {...}
176 local index = 1
177 local value = nil
178 local max_value = nil
179
180 if #args == 1 then
181 args = args[1]
182 end
183
184 while index <= #args do
185 value = args[index]
186 if max_value == nil or value > max_value then
187 max_value = value
188 end
189 index = index + 1
190 end
191
192 return max_value
193end
194
195--! @short minimum
196--! @par Equation
197--! @f$
198--! \frac{N_1 + N_2 + | N_1 + N_2 |}{2}
199--! @f$
200local function min(...)
201 local args = {...}
202 local index = 1
203 local value = nil
204 local min_value = nil
205
206 if #args == 1 then
207 args = args[1]
208 end
209
210 while index <= #args do
211 value = args[index]
212 if min_value == nil or value < min_value then
213 min_value = value
214 end
215 index = index + 1
216 end
217
218 return min_value
219end
220
221--! @brief sawtooth
222--! @par Equation
223--! @f$
224--! \begin{cases}
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 \\
229--! \end{cases}
230--! @f$
231local function saw(value)
232 if value < 0.25 then
233 return value * 4
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)
238 end
239 return ((value - 0.75) * 4) - 1
240end
241
242--! @}
243--! @}
244
245local function install(std)
246 std = std or {}
247 std.math = std.math or {}
248 std.math.abs=abs
249 std.math.clamp=clamp
250 std.math.clamp2=clamp2
251 std.math.cycle=cycle
252 std.math.dir=dir
253 std.math.dis=dis
254 std.math.dis2=dis2
255 std.math.lerp=lerp
256 std.math.map=map
257 std.math.max=max
258 std.math.min=min
259 std.math.saw=saw
260 return std.math
261end
262
263local function install_clib(std)
264 local math = require('math')
265 std = std or {}
266 std.math = std.math or {}
267 std.math.acos=math.acos
268 std.math.asin=math.asin
269 std.math.atan=math.atan
270 std.math.atan2=math.atan2
271 std.math.ceil=math.ceil
272 std.math.cos=math.cos
273 std.math.cosh=math.cosh
274 std.math.deg=math.deg
275 std.math.exp=math.exp
276 std.math.floor=math.floor
277 std.math.fmod=math.fmod
278 std.math.frexp=math.frexp
279 std.math.huge=math.huge
280 std.math.ldexp=math.ldexp
281 std.math.log=math.log
282 std.math.log10=math.log10
283 std.math.modf=math.modf
284 std.math.pi=math.pi
285 std.math.pow=math.pow
286 std.math.rad=math.rad
287 std.math.sin=math.sin
288 std.math.sinh=math.sinh
289 std.math.sqrt=math.sqrt
290 std.math.tan=math.tan
291 std.math.tanh=math.tanh
292 return std.math
293end
294
295local function install_clib_random(std)
296 local math = require('math')
297 std = std or {}
298 std.math = std.math or {}
299 std.math.random = math.random
300 return std.math
301end
302
303local P = {
305 clib = {
307 },
308 clib_random = {
310 }
311}
312
313return P;
local function install_clib_random(std)
local function install_clib(std)
local function install(std)
local function require(std, game, application)
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 game
Definition main.lua:17
local std
Definition main.lua:18
local math
Definition draw.lua:1
local function text(x, y, text)