Page 1 of 2
Lua Vector
Posted: Wed May 01, 2019 1:42 pm
by damv
hello, I made a small vector library for love2d, I hope you like it
https://github.com/DeybisMelendez/lua-vector
Release v0.1.0:
https://github.com/DeybisMelendez/lua-v ... tag/v0.1.0
Example:
Code: Select all
myVector = vector(5, 4)
print(myVector:string()) --> vector(5, 4)
other = vector(3, 2)
if myVector.x > other.x then print(other.y) --> 2
this = myVector + other
print(this:string()) --> vector(8, 6)
Re: Lua Vector
Posted: Wed May 01, 2019 7:53 pm
by raidho36
It generates new strings every time it uses assert, that will be slow. Otherwise, looks like vector library from CPML.
Here's what I use. It's a bit more involved and explicit, but it runs fast.
Re: Lua Vector
Posted: Thu May 02, 2019 8:34 am
by Przemator
How is it better than hump.vector? or hump.vector-light?
Re: Lua Vector
Posted: Thu May 02, 2019 6:23 pm
by monolifed
It is not. Also it is GPL.
Re: Lua Vector
Posted: Thu May 02, 2019 7:09 pm
by damv
Przemator wrote: ↑Thu May 02, 2019 8:34 am
How is it better than hump.vector? or hump.vector-light?
I did not know the existence of that, I have seen it now and I see that there is not much difference, only that there are some methods that my library does not have but also there are some things that library does not have that mine if, like the round() and dot() for example.
In addition, my library returns a new vector in the operations and methods, for example vecA + vecB = vecC where vecC has all the methods, instead, the other library only returns its properties x, y.
I apologize for my bad English, I used the google translator.
Re: Lua Vector
Posted: Thu May 02, 2019 7:15 pm
by Przemator
hump.vector also has dot product. it's done using the __mul operator (vector * vector). it's not only returning the x, y, it creates a new vector (or you can explicitly use methods, which modify the vector itself). There is no round, but there is normalized and trimmed. The way I see it, you've done a lot of effort for nothing
https://hump.readthedocs.io/en/latest/vector.html
Re: Lua Vector
Posted: Thu May 02, 2019 7:22 pm
by damv
raidho36 wrote: ↑Wed May 01, 2019 7:53 pm
It generates new strings every time it uses assert, that will be slow. Otherwise, looks like vector library from CPML.
Here's what I use. It's a bit more involved and explicit, but it runs fast.
the vector table does not inherit the checkError () method, it is a proprietary method that instantiates in the "require" once nothing else, like the constants vector.RIGHT, vector.LEFT, etc.
Re: Lua Vector
Posted: Thu May 02, 2019 7:51 pm
by damv
Przemator wrote: ↑Thu May 02, 2019 7:15 pm
hump.vector also has dot product. it's done using the __mul operator (vector * vector). it's not only returning the x, y, it creates a new vector (or you can explicitly use methods, which modify the vector itself). There is no round, but there is normalized and trimmed. The way I see it, you've done a lot of effort for nothing
https://hump.readthedocs.io/en/latest/vector.html
I only share it to receive a critique, they mentioned that the assert () loads strings that make the vector slow, it's a bit exaggerated but in my case, it is not, the checkError () method is not inherited, just like some pre-generated variables like vector.RIGHT, vector.LEFT, etc. it's just a reference, on the other hand in that library there's assert everywhere and it's instantiated in the vector, it strikes me.
Re: Lua Vector
Posted: Thu May 02, 2019 7:53 pm
by raidho36
damv wrote: ↑Thu May 02, 2019 7:22 pm
the vector table does not inherit the checkError () method, it is a proprietary method that instantiates in the "require" once nothing else, like the constants vector.RIGHT, vector.LEFT, etc.
Code: Select all
function vector.checkError(case, func, a)
print ( 'checkerror', case, func, a )
...
Code: Select all
local vec = require ( 'vector' )
local a = vec ( 1, 2 )
local b = vec ( 3, 4 )
print ( 'adding' )
local c = a + b
print ( 'done' )
Code: Select all
adding
checkerror math add: vector(1, 2)
checkerror math add: vector(3, 4)
checkerror type add: vector(1, 2)
checkerror type add: vector(3, 4)
done
Well it clearly does and it clearly runs a lot of these all the time.
Re: Lua Vector
Posted: Thu May 02, 2019 8:14 pm
by damv
raidho36 wrote: ↑Thu May 02, 2019 7:53 pm
damv wrote: ↑Thu May 02, 2019 7:22 pm
the vector table does not inherit the checkError () method, it is a proprietary method that instantiates in the "require" once nothing else, like the constants vector.RIGHT, vector.LEFT, etc.
Code: Select all
function vector.checkError(case, func, a)
print ( 'checkerror', case, func, a )
...
Code: Select all
local vec = require ( 'vector' )
local a = vec ( 1, 2 )
local b = vec ( 3, 4 )
print ( 'adding' )
local c = a + b
print ( 'done' )
Code: Select all
adding
checkerror math add: vector(1, 2)
checkerror math add: vector(3, 4)
checkerror type add: vector(1, 2)
checkerror type add: vector(3, 4)
done
Well it clearly does and it clearly runs a lot of these all the time.
print 4 times because it checks if in operation "a" and "b" are numbers or vectors, so you can make vector (0, 0) + 4 or vector (0,0) + vector (4,4), but do you think I should omit those assert? I thought it referred to the assert string.