-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathVector2.ttslua
executable file
·287 lines (238 loc) · 7.65 KB
/
Vector2.ttslua
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
require('ge_tts.License')
---
--- A 2D vector implementation.
---
--- Components can be accessed as x and y properties, or indexed by numbers [1] and [2].
---
---@class ge_tts__Vector2 : __ge_tts__NumCharVec2Shape<number>
---@shape ge_tts__CharVec2Shape
---@field x number
---@field y number
---@shape ge_tts__NumVec2Shape
---@field [1] number
---@field [2] number
---@alias ge_tts__Vec2Shape ge_tts__CharVec2Shape | ge_tts__NumVec2Shape
---@shape __ge_tts__NumCharVec2Shape<T>
---@field x T
---@field y T
---@field [1] T
---@field [2] T
---@shape ge_tts__NumCharVec2Shape : __ge_tts__NumCharVec2Shape<number>
---@param vector ge_tts__Vector2
---@param index any
local function numberedIndex(vector, index)
if type(index) == 'number' then
if index == 1 then
return vector.x
elseif index == 2 then
return vector.y
end
return nil
end
end
local DEGREES_RATIO = 180 / math.pi
local RADIANS_RATIO = math.pi / 180
---@class ge_tts__static_Vector2
---@overload fun(): ge_tts__Vector2
---@overload fun(x: number, y: number): ge_tts__Vector2
---@overload fun(source: ge_tts__Vec2Shape): ge_tts__Vector2
local Vector2 = {}
setmetatable(Vector2, {
---@param sourceXOrVector nil | __ge_tts__NumCharVec2Shape<nil | number>
---@param sourceY nil | number
---@return ge_tts__Vector2
__call = function(_, sourceXOrVector, sourceY)
local self = --[[---@type self]] {x = 0, y = 0}
setmetatable(self, {
__index = numberedIndex,
__tostring = function(_)
return self.toString()
end,
})
if sourceXOrVector then
if type(sourceXOrVector) == 'table' then
local source = --[[---@type __ge_tts__NumCharVec2Shape<nil | number>]] sourceXOrVector
self.x = source.x or source[1] or self.x
self.y = source.y or source[2] or self.y
else
self.x = --[[---@type number]] sourceXOrVector
self.y = --[[---@type number]] sourceY
end
end
---@return string
function self.toString()
return '{x = ' .. self.x .. ', y = ' .. self.y .. '}'
end
---@return ge_tts__CharVec2Shape
function self.toData()
return {x = self.x, y = self.y}
end
---@return number
function self.lengthSquared()
return Vector2.lengthSquared(self)
end
---@return number
function self.length()
return Vector2.length(self)
end
---Add a vector to self.
---@overload fun(v: ge_tts__Vec2Shape): self
---@param v ge_tts__NumCharVec2Shape
---@return self
function self.add(v)
self.x = self.x + (v.x or v[1])
self.y = self.y + (v.y or v[2])
return self
end
---Subtract a vector from self.
---@overload fun(v: ge_tts__Vec2Shape): self
---@param v ge_tts__NumCharVec2Shape
---@return self
function self.sub(v)
self.x = self.x - (v.x or v[1])
self.y = self.y - (v.y or v[2])
return self
end
---@param factor number | ge_tts__Vec2Shape
---@return self
function self.scale(factor)
if (type(factor) == 'number') then
self.x = self.x * factor
self.y = self.y * factor
else
self.x = self.x * ((--[[---@type ge_tts__CharVec2Shape]] factor).x or (--[[---@type ge_tts__NumVec2Shape]] factor)[1])
self.y = self.y * ((--[[---@type ge_tts__CharVec2Shape]] factor).y or (--[[---@type ge_tts__NumVec2Shape]] factor)[2])
end
return self
end
---@return self
function self.normalize()
return self.scale(1 / self.length())
end
---@param angle number @angle in degrees
---@return self
function self.rotate(angle)
angle = angle * RADIANS_RATIO
local x = self.x
self.x = x * math.cos(angle) - self.y * math.sin(angle)
self.y = x * math.sin(angle) + self.y * math.cos(angle)
return self
end
return self
end,
})
---@overload fun(v: ge_tts__Vec2Shape): number
---@param v ge_tts__NumCharVec2Shape
---@return number
function Vector2.x(v)
return v.x or v[1]
end
---@overload fun(v: ge_tts__Vec2Shape): number
---@param v ge_tts__NumCharVec2Shape
---@return number
function Vector2.y(v)
return v.y or v[2]
end
---@overload fun(v: ge_tts__Vec2Shape): number
---@param v ge_tts__NumCharVec2Shape
---@return number
function Vector2.lengthSquared(v)
local x = v.x or v[1]
local y = v.y or v[2]
return x * x + y * y
end
---@param v ge_tts__Vec2Shape
---@return number
function Vector2.length(v)
return math.sqrt(Vector2.lengthSquared(v))
end
---@param v1 ge_tts__Vec2Shape
---@param v2 ge_tts__Vec2Shape
---@return ge_tts__Vector2
function Vector2.add(v1, v2)
return Vector2(v1).add(v2)
end
---@param v1 ge_tts__Vec2Shape
---@param v2 ge_tts__Vec2Shape
---@return ge_tts__Vector2
function Vector2.sub(v1, v2)
return Vector2(v1).sub(v2)
end
---@param v ge_tts__Vec2Shape
---@param factor number | ge_tts__Vec2Shape
---@return ge_tts__Vector2
function Vector2.scale(v, factor)
return Vector2(v).scale(factor)
end
---@param v ge_tts__Vec2Shape
---@return ge_tts__Vector2
function Vector2.normalize(v)
return Vector2(v).normalize()
end
---@overload fun(v1: ge_tts__Vec2Shape, v2: ge_tts__Vec2Shape): number
---@param v1 ge_tts__NumCharVec2Shape
---@param v2 ge_tts__NumCharVec2Shape
---@return number
function Vector2.cross(v1, v2)
local x1 = v1.x or v1[1]
local y1 = v1.y or v1[2]
local x2 = v2.x or v2[1]
local y2 = v2.y or v2[2]
return x1 * y2 - y1 * x2
end
--- Returns the angle between v1 and v2 in degrees.
---@overload fun(v1: ge_tts__Vec2Shape, v2: ge_tts__Vec2Shape): number
---@param v1 ge_tts__NumCharVec2Shape
---@param v2 ge_tts__NumCharVec2Shape
---@return number
function Vector2.angle(v1, v2)
return DEGREES_RATIO * math.acos(Vector2.dot(v1, v2) / (Vector2.length(v1) * Vector2.length(v2)))
end
---@overload fun(v1: ge_tts__Vec2Shape, v2: ge_tts__Vec2Shape): number
---@param v1 ge_tts__NumCharVec2Shape
---@param v2 ge_tts__NumCharVec2Shape
---@return number
function Vector2.dot(v1, v2)
local x1 = v1.x or v1[1]
local y1 = v1.y or v1[2]
local x2 = v2.x or v2[1]
local y2 = v2.y or v2[2]
return x1 * x2 + y1 * y2
end
---@overload fun(v1: ge_tts__Vec2Shape, v2: ge_tts__Vec2Shape): number
---@param v1 ge_tts__NumCharVec2Shape
---@param v2 ge_tts__NumCharVec2Shape
---@return number
function Vector2.distanceSquared(v1, v2)
local x1 = v1.x or v1[1]
local y1 = v1.y or v1[2]
local x2 = v2.x or v2[1]
local y2 = v2.y or v2[2]
return Vector2.lengthSquared({x = x2 - x1, y = y2 - y1})
end
---@param v1 ge_tts__Vec2Shape
---@param v2 ge_tts__Vec2Shape
---@return number
function Vector2.distance(v1, v2)
return math.sqrt(Vector2.distanceSquared(v1, v2))
end
---@overload fun(v: ge_tts__Vec2Shape, angle: number): ge_tts__Vector2
---@param v ge_tts__NumCharVec2Shape
---@param angle number @angle in degrees
---@return ge_tts__Vector2
function Vector2.rotate(v, angle)
angle = angle * RADIANS_RATIO
local x = v.x or v[1]
local y = v.y or v[2]
return Vector2(
x * math.cos(angle) - y * math.sin(angle),
x * math.sin(angle) + y * math.cos(angle)
)
end
---@overload fun(v3: tts__VectorShape): ge_tts__Vector2
---@param v3 ge_tts__NumCharVec3
---@return ge_tts__Vector2
function Vector2.fromXZ(v3)
return Vector2(v3.x or v3[1], v3.z or v3[3])
end
return Vector2