Skip to content

Commit 3db6e91

Browse files
committed
Add atan2 method
1 parent 05e54d0 commit 3db6e91

File tree

1 file changed

+20
-0
lines changed
  • tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua

1 file changed

+20
-0
lines changed

tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/MathLib.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.luaj.vm2.lib.OneArgFunction
1111
import org.luaj.vm2.lib.ThreeArgFunction
1212
import org.luaj.vm2.lib.TwoArgFunction
1313
import kotlin.math.abs
14+
import kotlin.math.atan2
1415
import kotlin.math.floor
1516
import kotlin.math.sqrt
1617
import kotlin.random.Random
@@ -30,6 +31,7 @@ class MathLib : org.luaj.vm2.lib.MathLib() {
3031
math["dst"] = dst()
3132
math["dst2"] = dst2()
3233
math["sign"] = sign()
34+
math["atan2"] = atan2()
3335
math["roverlap"] = roverlap()
3436
math["perlin"] = perlin(Random.nextLong())
3537
return math
@@ -49,6 +51,24 @@ class MathLib : org.luaj.vm2.lib.MathLib() {
4951
}
5052
}
5153

54+
@TinyFunction(
55+
"Calculate the angle in radians between the positive x-axis and the point (x, y).",
56+
)
57+
internal inner class atan2 : TwoArgFunction() {
58+
@TinyCall("Calculate the angle for the point (x, y). Please note the argument order: y then x.")
59+
override fun call(
60+
@TinyArg("y") arg1: LuaValue,
61+
@TinyArg("x") arg2: LuaValue,
62+
): LuaValue {
63+
val y = arg1.todouble()
64+
val x = arg2.todouble()
65+
66+
val result = atan2(y, x)
67+
68+
return valueOf(result)
69+
}
70+
}
71+
5272
@TinyFunction("Clamp the value between 2 values.", example = MATH_CLAMP_EXAMPLE)
5373
internal inner class clamp : ThreeArgFunction() {
5474
@TinyCall("Clamp the value between a and b. If a is greater than b, then b will be returned.")

0 commit comments

Comments
 (0)