add vec2.angle(...)

This commit is contained in:
MihailRis 2024-07-29 16:54:57 +03:00
parent 2b7b68f3c2
commit 753fb6bf3b
4 changed files with 35 additions and 0 deletions

View File

@ -154,6 +154,12 @@ vec3.spherical_rand(radius: number)
-- writes a random vector whose coordinates are uniformly distributed on a sphere of a given radius in dst
vec3.spherical_rand(radius: number, dst: vec3)
-- returns the direction angle of the vector v in degrees [0, 360]
vec2.angle(v: vec2)
-- returns the direction angle of the vector {x, y} in degrees [0, 360]
vec2.angle(x: number, y: number)
```

View File

@ -154,6 +154,12 @@ vec3.spherical_rand(radius: number)
-- записывает случайный вектор, координаты которого равномерно распределены на сфере заданного радиуса в dst
vec3.spherical_rand(radius: number, dst: vec3)
-- возвращает угол направления вектора v в градусах [0, 360]
vec2.angle(v: vec2)
-- возвращает угол направления вектора {x, y} в градусах [0, 360]
vec2.angle(x: number, y: number)
```

View File

@ -58,6 +58,8 @@ namespace lua {
" expected)");
}
}
[[nodiscard]]
inline uint check_argc(lua::State* L, int a, int b) {
int argc = lua::gettop(L);
if (argc == a || argc == b) {

View File

@ -3,6 +3,16 @@
#include <sstream>
#include <glm/glm.hpp>
#include <glm/gtc/random.hpp>
#include <glm/gtx/vector_angle.hpp>
template<typename T>
inline T angle(glm::vec<2, T> vec) {
auto val = std::atan2(vec.y, vec.x);
if (val < 0.0) {
return val + glm::two_pi<double>();
}
return val;
}
template<int n, template<class> class Op>
static int l_binop(lua::State* L) {
@ -133,6 +143,16 @@ static int l_spherical_rand(lua::State* L) {
return 0;
}
static int l_vec2_angle(lua::State* L) {
uint argc = lua::check_argc(L, 1, 2);
if (argc == 1) {
return lua::pushnumber(L, glm::degrees(angle(lua::tovec2(L, 1))));
} else {
return lua::pushnumber(L, glm::degrees(angle(
glm::vec2(lua::tonumber(L, 1), lua::tonumber(L, 2)))));
}
}
template<int n>
static int l_tostring(lua::State* L) {
lua::check_argc(L, 1);
@ -162,6 +182,7 @@ const luaL_Reg vec2lib [] = {
{"inverse", lua::wrap<l_inverse<2>>},
{"pow", lua::wrap<l_pow<2>>},
{"dot", lua::wrap<l_dot<2>>},
{"angle", lua::wrap<l_vec2_angle>},
{NULL, NULL}
};