Skip to content

Commit

Permalink
Merge pull request #68 from billmakes/master
Browse files Browse the repository at this point in the history
vec2: add meta functions for mathematical operators
  • Loading branch information
1bardesign authored Dec 14, 2023
2 parents e4953b6 + 2d0abbd commit 90f46c2
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions vec2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,33 @@ function vec2:maxcomp()
return math.max(self.x, self.y)
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
function vec2:major_inplace()
if self.x > self.y then
Expand Down

0 comments on commit 90f46c2

Please sign in to comment.