This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Eric's profile picture

__eq and __div metamethods on vectors

Started by Eric, 16 December 2012 - 02:29 AM
Eric #1
Posted 16 December 2012 - 03:29 AM
This really should be in the builtin API. You can bodge it in like this at present:

local vmt = getmetatable(vector.new(0, 0, 0))
vmt.__eq = function(a, B)/> return a.x == b.x and a.y == b.y and a.z == b.z end
vmt.__div = function(v, k) return v * (1/k) end,
To put this in the builtin API, it's as simple as replacing

local vmetatable = {
    __index = vector,
    __add = vector.add,
    __sub = vector.sub,
    __mul = vector.mul,
    __unm = function( v ) return v:mul(-1) end,
    __tostring = vector.tostring,
}
with

local vmetatable = {
    __index = vector,
    __add = vector.add,
    __sub = vector.sub,
    __mul = vector.mul,
    __div = function(v, k) return v:mul(1/k) end,
    __unm = function(v) return v:mul(-1) end,
    __eq = function(a, B)/> return a.x == b.x and a.y == b.y and a.z == b.z end
    __tostring = vector.tostring,
}
Xtansia #2
Posted 16 December 2012 - 01:03 PM
I've asked Cloudy about improving the vector api, But he seemed reluctant not seeing a need for any more features,
<shameless_self_plug>My vector api has these features and more link</shameless_self_plug>
Cloudy #3
Posted 16 December 2012 - 04:34 PM
I've asked Cloudy about improving the vector api, But he seemed reluctant not seeing a need for any more features,
<shameless_self_plug>My vector api has these features and more link</shameless_self_plug>

No, I said I'd implement it. I only didn't because I was under the impression that we covered everything anyway.
Xtansia #4
Posted 16 December 2012 - 04:38 PM
I've asked Cloudy about improving the vector api, But he seemed reluctant not seeing a need for any more features,
<shameless_self_plug>My vector api has these features and more link</shameless_self_plug>

No, I said I'd implement it. I only didn't because I was under the impression that we covered everything anyway.

Not quite how I remember it, but I'm not going to start an argument about it. :D/>
Cloudy #5
Posted 16 December 2012 - 09:21 PM
I've asked Cloudy about improving the vector api, But he seemed reluctant not seeing a need for any more features,
<shameless_self_plug>My vector api has these features and more link</shameless_self_plug>

No, I said I'd implement it. I only didn't because I was under the impression that we covered everything anyway.

Not quite how I remember it, but I'm not going to start an argument about it. :D/>/>/>

I'll look into it and implement your changes if we don't cover everything. Perhaps we both got confused! (or just me - it happens :P/>)