27static int vectorL_add__( lua_State *L );
29static int vectorL_sub__( lua_State *L );
31static int vectorL_mul__( lua_State *L );
33static int vectorL_div__( lua_State *L );
35static int vectorL_unm( lua_State *L );
56 {
"add", vectorL_add__ },
58 {
"sub", vectorL_sub__ },
60 {
"mul", vectorL_mul__ },
62 {
"div", vectorL_div__ },
63 {
"__unm", vectorL_unm },
121 return (
vec2 *)lua_touserdata( L, ind );
133 return (
vec2 *)lua_touserdata( L, ind );
134 luaL_typerror( L, ind, VECTOR_METATABLE );
149 luaL_getmetatable( L, VECTOR_METATABLE );
150 lua_setmetatable( L, -2 );
165 if ( lua_getmetatable( L, ind ) == 0 )
167 lua_getfield( L, LUA_REGISTRYINDEX, VECTOR_METATABLE );
170 if ( lua_rawequal( L, -1, -2 ) )
193 if ( !lua_isnoneornil( L, 1 ) )
194 x = luaL_checknumber( L, 1 );
198 if ( !lua_isnoneornil( L, 2 ) )
199 y = luaL_checknumber( L, 2 );
203 vec2_cset( &v, x, y );
225 if ( !lua_isnoneornil( L, 1 ) )
226 m = luaL_checknumber( L, 1 );
230 if ( !lua_isnoneornil( L, 2 ) )
231 a = luaL_checknumber( L, 2 );
235 vec2_pset( &v, m, a );
263 char buf[STRMAX_SHORT];
265 snprintf( buf,
sizeof( buf ),
"vec2( %g, %g )", v->x, v->y );
266 lua_pushstring( L, buf );
291 if ( lua_isnumber( L, 1 ) ) {
292 x = y = lua_tonumber( L, 1 );
304 x = luaL_checknumber( L, 2 );
305 if ( !lua_isnoneornil( L, 3 ) )
306 y = luaL_checknumber( L, 3 );
313 vec2_cset( &vout, v1->
x + x, v1->y + y );
318static int vectorL_add__( lua_State *L )
332 x = luaL_checknumber( L, 2 );
333 if ( !lua_isnoneornil( L, 3 ) )
334 y = luaL_checknumber( L, 3 );
340 vec2_cset( v1, v1->x + x, v1->y + y );
377 x = luaL_checknumber( L, 2 );
378 if ( !lua_isnoneornil( L, 3 ) )
379 y = luaL_checknumber( L, 3 );
385 vec2_cset( &vout, v1->
x - x, v1->y - y );
389static int vectorL_sub__( lua_State *L )
403 x = luaL_checknumber( L, 2 );
404 if ( !lua_isnoneornil( L, 3 ) )
405 y = luaL_checknumber( L, 3 );
411 vec2_cset( v1, v1->x - x, v1->y - y );
431 if ( lua_isnumber( L, 1 ) ) {
432 double d = lua_tonumber( L, 1 );
434 vec2_cset( &vout, v->
x *
d, v->y *
d );
436 if ( lua_isnumber( L, 2 ) ) {
438 double d = lua_tonumber( L, 2 );
439 vec2_cset( &vout, v->
x *
d, v->y *
d );
443 vec2_cset( &vout, v1->
x * v2->x, v1->y * v2->y );
451static int vectorL_mul__( lua_State *L )
454 if ( lua_isnumber( L, 2 ) ) {
455 double mod = luaL_checknumber( L, 2 );
456 vec2_cset( v1, v1->x * mod, v1->y * mod );
459 vec2_cset( v1, v1->x * v2->x, v1->y * v2->y );
482 if ( lua_isnumber( L, 2 ) ) {
483 double mod = lua_tonumber( L, 2 );
484 vec2_cset( &vout, v1->
x / mod, v1->y / mod );
487 vec2_cset( &vout, v1->
x / v2->x, v1->y / v2->y );
493static int vectorL_div__( lua_State *L )
496 if ( lua_isnumber( L, 2 ) ) {
497 double mod = lua_tonumber( L, 2 );
498 vec2_cset( v1, v1->x / mod, v1->y / mod );
501 vec2_cset( v1, v1->x / v2->x, v1->y / v2->y );
507static int vectorL_unm( lua_State *L )
511 vec2_cset( &vout, -vin->
x, -vin->
y );
528 lua_pushnumber( L, a->x * b->x + a->y * b->y );
544 lua_pushnumber( L, a->x * b->y - a->y * b->x );
562 lua_pushnumber( L, v1->x );
563 lua_pushnumber( L, v1->y );
582 lua_pushnumber( L, VMOD( *v1 ) );
583 lua_pushnumber( L, VANGLE( *v1 ) );
604 x = luaL_checknumber( L, 2 );
605 y = luaL_checknumber( L, 3 );
607 vec2_cset( v1, x, y );
628 m = luaL_checknumber( L, 2 );
629 a = luaL_checknumber( L, 3 );
631 vec2_pset( v1, m, a );
654 if ( !lua_isnoneornil( L, 2 ) ) {
656 dist = vec2_dist( v1, v2 );
658 dist = vec2_odist( v1 );
661 lua_pushnumber( L, dist );
685 if ( !lua_isnoneornil( L, 2 ) ) {
687 dist2 = vec2_dist2( v1, v2 );
689 dist2 = vec2_odist2( v1 );
692 lua_pushnumber( L, dist2 );
705 lua_pushnumber( L, VMOD( *v ) );
718 lua_pushnumber( L, VANGLE( *v ) );
732 double n = luaL_optnumber( L, 2, 1. );
733 double m = n /
MAX( VMOD( *v ), DOUBLE_TOL );
760 lua_pushinteger( L, ret );
779 const vec2 *center, *p1, *p2;
784 radius = luaL_checknumber( L, 2 );
int CollideLineCircle(const vec2 *p1, const vec2 *p2, const vec2 *cc, double cr, vec2 crash[2])
Checks to see if a line collides with a circle.
int CollideLineLine(double s1x, double s1y, double e1x, double e1y, double s2x, double s2y, double e2x, double e2y, vec2 *crash)
Checks to see if two lines collide.
Header file with generic functions and naev-specifics.
static int vectorL_distance2(lua_State *L)
Gets the squared distance from the Vec2 (saves a sqrt())
static int vectorL_collideCircleLine(lua_State *L)
Computes the intersection of a line segment and a circle.
static int vectorL_collideLineLine(lua_State *L)
Sees if two line segments collide.
static const luaL_Reg vector_methods[]
static int vectorL_mod(lua_State *L)
Gets the modulus of the vector. Lua function parameter: Vec2 v Vector to get modulus of....
static int vectorL_dot(lua_State *L)
Dot product of two vectors.
static int vectorL_cross(lua_State *L)
Cross product of two vectors.
static int vectorL_set(lua_State *L)
Sets the vector by cartesian coordinates.
static int vectorL_div(lua_State *L)
Divides a vector by a number.
static int vectorL_setP(lua_State *L)
Sets the vector by polar coordinates.
static int vectorL_angle(lua_State *L)
Gets the angle of the vector. Lua function parameter: Vec2 v Vector to get angle of....
static int vectorL_normalize(lua_State *L)
Normalizes a vector. Lua function parameter: Vec2 v Vector to normalize. Lua function parameter:[opt=...
static int vectorL_mul(lua_State *L)
Multiplies a vector by a number.
int nlua_loadVector(nlua_env env)
Loads the vector metatable.
static int vectorL_copy(lua_State *L)
Copies a vector.
int lua_isvector(lua_State *L, int ind)
Checks to see if ind is a vector.
static int vectorL_add(lua_State *L)
Adds two vectors or a vector and some cartesian coordinates.
static int vectorL_newP(lua_State *L)
Creates a new vector using polar coordinates.
static int vectorL_tostring(lua_State *L)
Converts a vector to a string.
static int vectorL_get(lua_State *L)
Gets the cartesian positions of the vector.
vec2 * luaL_checkvector(lua_State *L, int ind)
Gets vector at index making sure type is valid.
static int vectorL_sub(lua_State *L)
Subtracts two vectors or a vector and some cartesian coordinates.
static int vectorL_distance(lua_State *L)
Gets the distance from the Vec2.
static int vectorL_polar(lua_State *L)
Gets polar coordinates of a vector.
vec2 * lua_tovector(lua_State *L, int ind)
Represents a 2D vector in Lua.
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
static int vectorL_new(lua_State *L)
Creates a new vector.