-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.lua
executable file
·57 lines (41 loc) · 978 Bytes
/
vector.lua
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
47
48
49
50
51
52
53
54
55
require 'middleclass.lua'
Vector = class('Vector')
function Vector:initialize(x, y)
self.x = x
self.y = y
end
function Vector:set(x, y)
self.x, self.y = x, y
end
function Vector:get()
return self.x, self.y
end
function Vector.__add(a, b)
return Vector:new(a.x+b.x, a.y+b.y)
end
function Vector.__sub(a, b)
return Vector:new(a.x-b.x, a.y-b.y)
end
function Vector.__mul(a, b)
return Vector:new(a.x*b, a.y*b)
end
function Vector.__div(a, b)
return Vector:new(a.x/b, a.y/b)
end
function Vector:isZero()
return math.abs(self.x) < 0.0001 and math.abs(self.y) < 0.0001
end
function Vector:distance(v)
return math.sqrt( (self.x - v.x)^2 + (self.y - v.y)^2 )
end
function Vector:length()
return math.sqrt(self.x^2 + self.y^2)
end
function Vector:normalize()
local len = self:length()
return (self / len)
end
function Vector:clamp(minX, maxX, minY, maxY)
self.x = math.max(minX, math.min(self.x, maxX))
self.y = math.max(minY, math.min(self.y, maxY))
end