diff options
author | ubq323 <ubq323@ubq323.website> | 2025-02-17 14:48:34 +0000 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2025-02-17 14:48:34 +0000 |
commit | f1492b52414b6f2ad6cfff45375c08677feab18c (patch) | |
tree | 2eac341c813f513b4acef7ff591f6f68936eb334 /pos.lua |
initial
Diffstat (limited to 'pos.lua')
-rw-r--r-- | pos.lua | 45 |
1 files changed, 45 insertions, 0 deletions
@@ -0,0 +1,45 @@ +local class = require'r.class' + +local Pos = class() +function Pos.new(cls) + return setmetatable({},cls) +end +function Pos.init(self,x,y) + self.x = x + self.y = y + return self +end +function Pos.make(cls,...) + return cls:new():init(...) +end +function Pos.__add(self,other) + return Pos:make(self.x+other.x,self.y+other.y) +end +function Pos.__sub(self,other) + return Pos:make(self.x-other.x,self.y-other.y) +end +function Pos.__mul(a,b) + if type(a) == "number" then + return Pos:make(a*b.x,a*b.y) + elseif type(b) == "number" then + return Pos:make(a.x*b,a.y*b) + else + error("can only multiply Pos by scalar") + end +end +function Pos.__div(a,b) + assert(type(b) == "number","can only divide Pos by scalar") + return a*(1/b) +end +function Pos.__eq(a,b) return a.x==b.x and a.y==b.y end +function Pos.lensq(self) return self.x^2 + self.y^2 end +function Pos.len(self) return math.sqrt(self:lensq()) end +function Pos.norm(self) return self/self:len() end +function Pos.dot(self,other) return self.x*other.x+self.y*other.y end +function Pos.__tostring(self) + return string.format("(%.2f,%.2f)",self.x,self.y) +end + +return Pos + + |