Lua Vector

Showcase your libraries, tools and other projects that help your fellow love users.
damv
Prole
Posts: 18
Joined: Thu Oct 11, 2018 9:09 pm

Lua Vector

Post by damv »

hello, I made a small vector library for love2d, I hope you like it :D

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)
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Lua Vector

Post 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.
Attachments
vec2.lua
(7.32 KiB) Downloaded 424 times
User avatar
Przemator
Party member
Posts: 107
Joined: Fri Sep 28, 2012 6:59 pm

Re: Lua Vector

Post by Przemator »

How is it better than hump.vector? or hump.vector-light?
monolifed
Party member
Posts: 188
Joined: Sat Feb 06, 2016 9:42 pm

Re: Lua Vector

Post by monolifed »

It is not. Also it is GPL.
damv
Prole
Posts: 18
Joined: Thu Oct 11, 2018 9:09 pm

Re: Lua Vector

Post 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.
User avatar
Przemator
Party member
Posts: 107
Joined: Fri Sep 28, 2012 6:59 pm

Re: Lua Vector

Post 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
damv
Prole
Posts: 18
Joined: Thu Oct 11, 2018 9:09 pm

Re: Lua Vector

Post 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.
damv
Prole
Posts: 18
Joined: Thu Oct 11, 2018 9:09 pm

Re: Lua Vector

Post 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.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Lua Vector

Post 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.
damv
Prole
Posts: 18
Joined: Thu Oct 11, 2018 9:09 pm

Re: Lua Vector

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests