summaryrefslogtreecommitdiff
path: root/pos.lua
blob: 149783e0aa54151ebcc73cf90f5d759f4f8322dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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.vals(self) return self.x, self.y end
function Pos.__tostring(self)
	return string.format("(%.2f,%.2f)",self.x,self.y)
end

return Pos