Merge pull request #68 from billmakes/master

vec2: add meta functions for mathematical operators
This commit is contained in:
Max Cahill 2023-12-14 13:05:11 +11:00 committed by GitHub
commit 90f46c2c6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -453,6 +453,33 @@ function vec2:maxcomp()
return math.max(self.x, self.y) return math.max(self.x, self.y)
end end
-- meta functions for mathmatical operations
function vec2.__add(a, b)
return a:vector_add_inplace(b)
end
function vec2.__sub(a, b)
return a:vector_sub_inplace(b)
end
function vec2.__mul(a, b)
if type(a) == "number" then
return b:scalar_mul_inplace(a)
elseif type(b) == "number" then
return a:scalar_mul_inplace(b)
else
return a:vector_mul_inplace(b)
end
end
function vec2.__div(a, b)
if type(b) == "number" then
return a:scalar_div_inplace(b)
else
return a:vector_div_inplace(b)
end
end
-- mask out min component, with preference to keep x -- mask out min component, with preference to keep x
function vec2:major_inplace() function vec2:major_inplace()
if self.x > self.y then if self.x > self.y then