-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector2.lua
More file actions
157 lines (127 loc) · 2.53 KB
/
vector2.lua
File metadata and controls
157 lines (127 loc) · 2.53 KB
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
--[[ Vector2 by Angelo Yazar ]]--
local math = math
local setmetatable = setmetatable
local assert = assert
local Vector2 = {}
setfenv(1, Vector2)
mt = {}
mt.__index = Vector2
function new( i, j )
local v = { i, j }
setmetatable( v, mt )
return v
end
function get( a )
return a[1], a[2]
end
function set( v, a, b )
v[1], v[2] = a, b or v[2]
end
function add( a, b )
return new( a[1]+b[1], a[2]+b[2] )
end
function sub( a, b )
return new( a[1]-b[1], a[2]-b[2] )
end
function mul( a, b )
return new( a[1]*b, a[2]*b )
end
function div( a, b )
assert( b, "Vector2: Can't divide by 0" )
return new( a[1]/b, a[2]/b )
end
function copy( a )
return new( a[1], a[2] )
end
function dot( a, b )
return a[1]*b[1] + a[2]*b[2]
end
function magnitude( a )
return math.sqrt(a[1]^2 + a[2]^2)
end
function sqrMagnitude( a )
return a[1]^2 + a[2]^2
end
function project( a, b )
return b:mul( a:dot(b)/b:sqrMagnitude() )
end
function normalized( a )
local m = a:magnitude()
if m and m > 0 then return new(a[1]/m,a[2]/m)
else return new(0,0,0) end
end
function normalize( a )
local m = a:magnitude()
if m then
a[1] = a[1] / m
a[2] = a[2] / m
end
return a
end
function clampMagnitude( a, maxLength )
local m = a:magnitude()
if m > maxLength then
return ( a:normalized() ):mul( maxLength )
else return a:copy() end
end
function scale( a, s )
a[1] = a[1] * s
a[2] = a[2] * s
return a
end
function angle( a, b )
return math.deg( math.atan2(b[2]-a[2],b[1]-a[1]) )
end
function reflect( a, n )
local nu = n:normalized()
local anu = a:dot(nu) * 2
nu:scale(anu)
return new( -a[1]+nu[1], -a[2]+nu[2] )
end
function distance( a, b )
return math.sqrt((b[1]-a[1])^2+(b[2]-a[2])^2)
end
function lerp( a, b, t )
local t1 = 1-t
return new( a[1]*t1+b[1]*t, a[2]*t1+b[2]*t )
end
function min( a, b )
local i,j = a[1], a[2]
if i > b[1] then i = b[1] end
if j > b[2] then j = b[2] end
return new(i,j)
end
function max( a, b )
local i,j = a[1], a[2]
if i < b[1] then i = b[1] end
if j < b[2] then j = b[2] end
return new(i,j)
end
local function sign( x )
if x < 0 then
return -1
else
return 1
end
end
function quadrant( a )
return new(sign(a[1]),sign(a[2]))
end
function tostring ( a )
return "Vector2: " .. a[1] .. ", " .. a[2]
end
mt.__add = add
mt.__sub = sub
mt.__mul = mul
mt.__div = div
mt.__tostring = tostring
mt.__le = function( a, b )
return a:magnitude() <= b:magnitude()
end
mt.__lt = function( a, b )
return a:magnitude() < b:magnitude()
end
mt.__eq = function( a, b )
return ( a[1] == b[1] and a[2] == b[2] )
end
return Vector2