-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector2.lua
159 lines (129 loc) · 3.19 KB
/
vector2.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
---
--- math.lua by RayStudio9236
--- See https://github.com/RayStudio36/math.lua for usage documentation.
--- Licensed under MIT.
--- See https://opensource.org/licenses/MIT for details.
---
---@class Vector2
local Vector2 = {}
Vector2.__index = Vector2
setmetatable(
Vector2,
{
__call = function(class, ...)
local instance = {}
setmetatable(instance, Vector2)
instance:new(...)
return instance
end
}
)
---@return Vector2
function Vector2.zero()
return Vector2(0, 0)
end
---@return Vector2
function Vector2.one()
return Vector2(1, 1)
end
---@return Vector2
function Vector2.up()
return Vector2(0, 1)
end
---@return Vector2
function Vector2.right()
return Vector2(1, 0)
end
---@return Vector2
function Vector2.down()
return Vector2(0, -1)
end
---@return Vector2
function Vector2.left()
return Vector2(-1, 0)
end
---@param angle number
---@return Vector2
function Vector2.fromAngle(angle)
local a = math.rad(angle + 90)
return Vector2(math.cos(a), math.sin(a))
end
function Vector2:new(x, y)
self.x = x or 0
self.y = y or 0
end
function Vector2:__tostring()
local x = math.floor(self.x) == self.x and string.format("%d", self.x) or string.format("%f", self.x)
local y = math.floor(self.y) == self.y and string.format("%d", self.y) or string.format("%f", self.y)
return string.format("Vector2(%s, %s)", x, y)
end
function Vector2:__eq(other)
return self.x == other.x and self.y == other.y
end
function Vector2:__add(other)
return Vector2(self.x + other.x, self.y + other.y)
end
function Vector2:__sub(other)
return Vector2(self.x - other.x, self.y - other.y)
end
function Vector2:__mul(value)
return Vector2(self.x * value, self.y * value)
end
function Vector2:__div(value)
return Vector2(self.x / value, self.y / value)
end
---@return number
function Vector2:len()
return math.sqrt((self.x ^ 2) + (self.y ^ 2))
end
---@return number
function Vector2:sqLen()
return (self.x ^ 2) + (self.y ^ 2)
end
---@return Vector2
function Vector2:normalize()
local ret = Vector2(self.x, self.y)
ret:normalized()
return ret
end
function Vector2:normalized()
local length = self:len()
if length > 0 then
self.x = self.x / length
self.y = self.y / length
end
end
---@param other Vector2
---@return number
function Vector2:dot(other)
return (self.x * other.x) + (self.y * other.y)
end
---@return Vector2
function Vector2:clone()
return Vector2(self.x, self.y)
end
---@return number
function Vector2:toAngle()
return math.deg(math.atan(self.y, self.x)) - 90
end
---@param angle number
---@return Vector2
function Vector2:rotate(angle)
local sin = math.sin(math.rad(angle))
local cos = math.cos(math.rad(angle))
local tx = self.x
local ty = self.y
local x = (cos * tx) - (sin * ty)
local y = (sin * tx) + (cos * ty)
return Vector2(x, y)
end
---@param angle number
function Vector2:rotated(angle)
local sin = math.sin(math.rad(angle))
local cos = math.cos(math.rad(angle))
local tx = self.x
local ty = self.y
self.x = (cos * tx) - (sin * ty)
self.y = (sin * tx) + (cos * ty)
end
return Vector2