# 2D vectors

8 messages
Open this post in threaded view
|

## 2D vectors

 Hi,is there any library or facility in Lua for working with 2D euclidean vectors?I think of functions for getting (euclidean) distance, sum of two vectors, scalar multiplication, scalar product, transformation to and from polar coordinates and such. I know these are not particularly difficult to implement, but i'd rather not reinvent the wheel.Also, how would a 2D vector be defined most efficiently?As a table with two entries, "x" and "y" ? In my C code i have this kind of data structures:struct VECTOR { FLOAT x; FLOAT y; };and i need to pass them to Lua and back.These are part of larger data structures.Am i right that i would need to pass the vectors as tables (inside other tables, possibly inside other tables, ...)? regards,Lars R.
Open this post in threaded view
|

## Re: 2D vectors

 On Wed, Jun 11, 2014 at 9:32 AM, Lars Ruoff <[hidden email]> wrote: > is there any library or facility in Lua for working with 2D euclidean > vectors? There's a C extension module for 3D vectors, called lv3 http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/It's specifically intended as a 'learning aid', so this could be a useful starting point.
Open this post in threaded view
|

## Re: 2D vectors

 > On Wed, Jun 11, 2014 at 9:32 AM, Lars Ruoff <[hidden email]> wrote: > > is there any library or facility in Lua for working with 2D euclidean > > vectors? > > There's a C extension module for 3D vectors, called lv3 > > http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/> > It's specifically intended as a 'learning aid', so this could be a > useful starting point. To use it under 5.2, you need to remove v3.lua and change lv3.c:      diff orig/lv3.c ./lv3.c      18,19c18      <  if (luaL_checkudata(L,i,MYTYPE)==NULL) luaL_typerror(L,i,MYTYPE);      <  return lua_touserdata(L,i);      ---      >  return luaL_checkudata(L,i,MYTYPE);      76c75      < static const luaL_reg R[] =      ---      > static const luaL_Reg R[] =      87c86      <  luaL_openlib(L,NULL,R,0);      ---      >  luaL_setfuncs(L,R,0);
Open this post in threaded view
|

## Re: 2D vectors

 On Wed, Jun 11, 2014 at 1:53 PM, Luiz Henrique de Figueiredo <[hidden email]- > To use it under 5.2, you need to remove v3.lua and change lv3.c: Thanks, Luiz.  Although I note that it does not provide more than the basic indexing operations. Ideally, this kind of thing should be in C for speed, but a little Point class is described here: http://lua-users.org/wiki/PointAndComplex
Open this post in threaded view
|

## Re: 2D vectors

 In reply to this post by Lars Ruoff Hi Lars, I don't know if you are looking for a solution on the C-side, but I remember I have some implementations of vector classes in pure Lua. They might not be the best, but just quick modules I have written as part of some personal projects. See vec3.lua (used in Lua port of Smallpt raytracer) and vector.lua (2d vectors used in a simulation of steering behaviors). There is also this quite complete and clean module for 2d vectors, which is part of a larger set of utilities, named HUMP. See hump.vector. And last, lua-vec. I haven't tested this out, though,  but the API looks quite complete.
Open this post in threaded view
|

## Re: 2D vectors

 Thanks Roland and all,i have already a solution on the C(++) side. As i said, it's basicallystruct VECTOR { FLOAT x; FLOAT y;}; with some methods around it for utility functions.Would it be better to expose those functions to Lua or use a Lua-side library? The Lua scripts would need to get some vectors in tables, then do some calculation with it and return a bunch of other vectors. And concerning my other initial question, would it feel "right" for a Lua coder to handle a 2D vector as a Lua table with elements "x" and "y"? On Wed, Jun 11, 2014 at 8:18 PM, Roland wrote: Hi Lars, I don't know if you are looking for a solution on the C-side, but I remember I have some implementations of vector classes in pure Lua. They might not be the best, but just quick modules I have written as part of some personal projects. See  vec3.lua (used in Lua port of Smallpt raytracer) and  vector.lua (2d vectors used in a simulation of steering behaviors). There is also this quite complete and clean module for 2d vectors, which is part of a larger set of utilities, named HUMP. See  hump.vector  . And last,  lua-vec  . I haven't tested this out, though,  but the API looks quite complete. -- View this message in context: http://lua.2524044.n2.nabble.com/2D-vectors-tp7660586p7660623.html Sent from the Lua-l mailing list archive at Nabble.com.