2020-01-29 03:26:28 +00:00
|
|
|
--[[
|
|
|
|
2d vector type
|
|
|
|
]]
|
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
local path = (...):gsub("vec2", "")
|
|
|
|
local class = require(path .. "class")
|
|
|
|
local math = require(path .. "mathx") --shadow global math module
|
2021-07-15 06:09:08 +00:00
|
|
|
local make_pooled = require(path .. "make_pooled")
|
2020-01-29 03:26:28 +00:00
|
|
|
|
2021-07-15 06:09:08 +00:00
|
|
|
local vec2 = class({
|
|
|
|
name = "vec2",
|
|
|
|
})
|
2020-01-29 03:26:28 +00:00
|
|
|
|
2020-04-08 11:10:03 +00:00
|
|
|
--stringification
|
2021-07-15 06:09:08 +00:00
|
|
|
function vec2:__tostring()
|
2020-04-08 11:10:03 +00:00
|
|
|
return ("(%.2f, %.2f)"):format(self.x, self.y)
|
|
|
|
end
|
|
|
|
|
2021-07-23 00:58:21 +00:00
|
|
|
--ctor
|
2020-01-29 03:26:28 +00:00
|
|
|
function vec2:new(x, y)
|
2021-07-23 00:58:21 +00:00
|
|
|
if type(x) == "number" then
|
|
|
|
self:scalar_set(x, y)
|
2021-07-15 06:09:08 +00:00
|
|
|
elseif type(x) == "table" then
|
2021-07-16 09:23:59 +00:00
|
|
|
if type(x.type) == "function" and x:type() == "vec2" then
|
2021-07-23 00:58:21 +00:00
|
|
|
self:vector_set(x)
|
2021-07-15 06:09:08 +00:00
|
|
|
elseif x[1] then
|
2021-07-23 00:58:21 +00:00
|
|
|
self:scalar_set(x[1], x[2])
|
2021-07-15 06:09:08 +00:00
|
|
|
else
|
2021-07-23 00:58:21 +00:00
|
|
|
self:scalar_set(x.x, x.y)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
2021-07-23 04:38:14 +00:00
|
|
|
else
|
|
|
|
self:scalar_set(0)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:38:14 +00:00
|
|
|
--explicit ctors; mostly vestigial at this point
|
2020-01-29 03:26:28 +00:00
|
|
|
function vec2:copy()
|
2021-07-15 06:09:08 +00:00
|
|
|
return vec2(self.x, self.y)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function vec2:xy(x, y)
|
2021-07-15 06:09:08 +00:00
|
|
|
return vec2(x, y)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2021-07-23 00:58:21 +00:00
|
|
|
function vec2:polar(length, angle)
|
2021-07-23 04:15:30 +00:00
|
|
|
return vec2(length, 0):rotate_inplace(angle)
|
2021-07-23 00:58:21 +00:00
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
function vec2:filled(v)
|
2021-07-23 00:58:21 +00:00
|
|
|
return vec2(v, v)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function vec2:zero()
|
2021-07-15 06:09:08 +00:00
|
|
|
return vec2(0)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--unpack for multi-args
|
|
|
|
function vec2:unpack()
|
|
|
|
return self.x, self.y
|
|
|
|
end
|
|
|
|
|
|
|
|
--pack when a sequence is needed
|
|
|
|
function vec2:pack()
|
|
|
|
return {self:unpack()}
|
|
|
|
end
|
|
|
|
|
2021-07-15 06:09:08 +00:00
|
|
|
--shared pooled storage
|
|
|
|
make_pooled(vec2, 128)
|
|
|
|
|
|
|
|
--get a pooled copy of an existing vector
|
|
|
|
function vec2:pooled_copy()
|
2021-07-23 04:15:30 +00:00
|
|
|
return vec2:pooled(self)
|
2021-07-15 06:09:08 +00:00
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
--modify
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:vector_set(v)
|
|
|
|
self.x = v.x
|
|
|
|
self.y = v.y
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-07-23 00:58:21 +00:00
|
|
|
function vec2:scalar_set(x, y)
|
2020-01-29 03:26:28 +00:00
|
|
|
if not y then y = x end
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
function vec2:swap(v)
|
|
|
|
local sx, sy = self.x, self.y
|
2021-07-23 04:15:30 +00:00
|
|
|
self.x, self.y = v.x, v.y
|
|
|
|
v.x, v.y = sx, sy
|
2020-01-29 03:26:28 +00:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------
|
|
|
|
--equality comparison
|
|
|
|
-----------------------------------------------------------
|
|
|
|
|
|
|
|
--threshold for equality in each dimension
|
|
|
|
local EQUALS_EPSILON = 1e-9
|
|
|
|
|
|
|
|
--true if a and b are functionally equivalent
|
|
|
|
function vec2.equals(a, b)
|
|
|
|
return (
|
|
|
|
math.abs(a.x - b.x) <= EQUALS_EPSILON and
|
|
|
|
math.abs(a.y - b.y) <= EQUALS_EPSILON
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
--true if a and b are not functionally equivalent
|
|
|
|
--(very slightly faster than `not vec2.equals(a, b)`)
|
|
|
|
function vec2.nequals(a, b)
|
|
|
|
return (
|
|
|
|
math.abs(a.x - b.x) > EQUALS_EPSILON or
|
|
|
|
math.abs(a.y - b.y) > EQUALS_EPSILON
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------
|
|
|
|
--arithmetic
|
|
|
|
-----------------------------------------------------------
|
|
|
|
|
|
|
|
--vector
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:vector_add_inplace(v)
|
2020-01-29 03:26:28 +00:00
|
|
|
self.x = self.x + v.x
|
|
|
|
self.y = self.y + v.y
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:vector_sub_inplace(v)
|
2020-01-29 03:26:28 +00:00
|
|
|
self.x = self.x - v.x
|
|
|
|
self.y = self.y - v.y
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:vector_mul_inplace(v)
|
2020-01-29 03:26:28 +00:00
|
|
|
self.x = self.x * v.x
|
|
|
|
self.y = self.y * v.y
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:vector_div_inplace(v)
|
2020-01-29 03:26:28 +00:00
|
|
|
self.x = self.x / v.x
|
|
|
|
self.y = self.y / v.y
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-08-09 09:07:03 +00:00
|
|
|
--(a + (b * t))
|
|
|
|
--useful for integrating physics and adding directional offsets
|
|
|
|
function vec2:fused_multiply_add_inplace(v, t)
|
|
|
|
self.x = self.x + (v.x * t)
|
|
|
|
self.y = self.y + (v.y * t)
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
--scalar
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:scalar_add_inplace(x, y)
|
2020-01-29 10:12:14 +00:00
|
|
|
if not y then y = x end
|
|
|
|
self.x = self.x + x
|
|
|
|
self.y = self.y + y
|
2020-01-29 03:26:28 +00:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:scalar_sub_inplace(x, y)
|
2020-01-29 10:12:14 +00:00
|
|
|
if not y then y = x end
|
|
|
|
self.x = self.x - x
|
|
|
|
self.y = self.y - y
|
2020-01-29 03:26:28 +00:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:scalar_mul_inplace(x, y)
|
2020-01-29 10:12:14 +00:00
|
|
|
if not y then y = x end
|
|
|
|
self.x = self.x * x
|
|
|
|
self.y = self.y * y
|
2020-01-29 03:26:28 +00:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:scalar_div_inplace(x, y)
|
2020-01-29 10:12:14 +00:00
|
|
|
if not y then y = x end
|
|
|
|
self.x = self.x / x
|
|
|
|
self.y = self.y / y
|
2020-01-29 03:26:28 +00:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------
|
|
|
|
-- geometric methods
|
|
|
|
-----------------------------------------------------------
|
|
|
|
|
|
|
|
function vec2:length_squared()
|
|
|
|
return self.x * self.x + self.y * self.y
|
|
|
|
end
|
|
|
|
|
|
|
|
function vec2:length()
|
|
|
|
return math.sqrt(self:length_squared())
|
|
|
|
end
|
|
|
|
|
|
|
|
function vec2:distance_squared(other)
|
|
|
|
local dx = self.x - other.x
|
|
|
|
local dy = self.y - other.y
|
|
|
|
return dx * dx + dy * dy
|
|
|
|
end
|
|
|
|
|
|
|
|
function vec2:distance(other)
|
|
|
|
return math.sqrt(self:distance_squared(other))
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:normalise_both_inplace()
|
2020-01-29 03:26:28 +00:00
|
|
|
local len = self:length()
|
|
|
|
if len == 0 then
|
|
|
|
return self, 0
|
|
|
|
end
|
2021-07-23 04:15:30 +00:00
|
|
|
return self:scalar_div_inplace(len), len
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:normalise_inplace()
|
|
|
|
local v, len = self:normalise_both_inplace()
|
2020-01-29 03:26:28 +00:00
|
|
|
return v
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:normalise_len_inplace()
|
|
|
|
local v, len = self:normalise_both_inplace()
|
2020-01-29 03:26:28 +00:00
|
|
|
return len
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:inverse_inplace()
|
|
|
|
return self:scalar_mul_inplace(-1)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2021-07-23 00:58:21 +00:00
|
|
|
-- angle/direction specific
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:rotate_inplace(angle)
|
2020-01-29 03:26:28 +00:00
|
|
|
local s = math.sin(angle)
|
|
|
|
local c = math.cos(angle)
|
|
|
|
local ox = self.x
|
|
|
|
local oy = self.y
|
|
|
|
self.x = c * ox - s * oy
|
|
|
|
self.y = s * ox + c * oy
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:rotate_around_inplace(angle, pivot)
|
|
|
|
self:vector_sub_inplace(pivot)
|
|
|
|
self:rotate_inplace(angle)
|
|
|
|
self:vector_add_inplace(pivot)
|
2021-07-23 00:58:21 +00:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
--fast quarter/half rotations
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:rot90r_inplace()
|
2020-01-29 03:26:28 +00:00
|
|
|
local ox = self.x
|
|
|
|
local oy = self.y
|
|
|
|
self.x = -oy
|
|
|
|
self.y = ox
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:rot90l_inplace()
|
2020-01-29 03:26:28 +00:00
|
|
|
local ox = self.x
|
|
|
|
local oy = self.y
|
|
|
|
self.x = oy
|
|
|
|
self.y = -ox
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
vec2.rot180_inplace = vec2.inverse_inplace --alias
|
2020-01-29 03:26:28 +00:00
|
|
|
|
2021-05-23 10:43:51 +00:00
|
|
|
--get the angle of this vector relative to (1, 0)
|
2020-01-29 03:26:28 +00:00
|
|
|
function vec2:angle()
|
|
|
|
return math.atan2(self.y, self.x)
|
|
|
|
end
|
|
|
|
|
2021-05-23 10:43:51 +00:00
|
|
|
--get the normalised difference in angle between two vectors
|
|
|
|
function vec2:angle_difference(v)
|
|
|
|
return math.angle_difference(self:angle(), v:angle())
|
|
|
|
end
|
|
|
|
|
|
|
|
--lerp towards the direction of a provided vector
|
|
|
|
--(length unchanged)
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:lerp_direction_inplace(v, t)
|
|
|
|
return self:rotate_inplace(self:angle_difference(v) * t)
|
2021-05-23 10:43:51 +00:00
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
-----------------------------------------------------------
|
|
|
|
-- per-component clamping ops
|
|
|
|
-----------------------------------------------------------
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:min_inplace(v)
|
2020-01-29 03:26:28 +00:00
|
|
|
self.x = math.min(self.x, v.x)
|
|
|
|
self.y = math.min(self.y, v.y)
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:max_inplace(v)
|
2020-01-29 03:26:28 +00:00
|
|
|
self.x = math.max(self.x, v.x)
|
|
|
|
self.y = math.max(self.y, v.y)
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:clamp_inplace(min, max)
|
2020-01-29 03:26:28 +00:00
|
|
|
self.x = math.clamp(self.x, min.x, max.x)
|
|
|
|
self.y = math.clamp(self.y, min.y, max.y)
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------
|
|
|
|
-- absolute value
|
|
|
|
-----------------------------------------------------------
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:abs_inplace()
|
2020-01-29 03:26:28 +00:00
|
|
|
self.x = math.abs(self.x)
|
|
|
|
self.y = math.abs(self.y)
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:49:29 +00:00
|
|
|
-----------------------------------------------------------
|
|
|
|
-- sign
|
|
|
|
-----------------------------------------------------------
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:sign_inplace()
|
2021-06-08 01:49:29 +00:00
|
|
|
self.x = math.sign(self.x)
|
|
|
|
self.y = math.sign(self.y)
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
-----------------------------------------------------------
|
|
|
|
-- truncation/rounding
|
|
|
|
-----------------------------------------------------------
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:floor_inplace()
|
2020-01-29 03:26:28 +00:00
|
|
|
self.x = math.floor(self.x)
|
|
|
|
self.y = math.floor(self.y)
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:ceil_inplace()
|
2020-01-29 03:26:28 +00:00
|
|
|
self.x = math.ceil(self.x)
|
|
|
|
self.y = math.ceil(self.y)
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:round_inplace()
|
2020-01-29 03:26:28 +00:00
|
|
|
self.x = math.round(self.x)
|
|
|
|
self.y = math.round(self.y)
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------
|
|
|
|
-- interpolation
|
|
|
|
-----------------------------------------------------------
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:lerp_inplace(other, amount)
|
2020-01-29 03:26:28 +00:00
|
|
|
self.x = math.lerp(self.x, other.x, amount)
|
|
|
|
self.y = math.lerp(self.y, other.y, amount)
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:lerp_eps_inplace(other, amount, eps)
|
2020-04-01 09:58:18 +00:00
|
|
|
self.x = math.lerp_eps(self.x, other.x, amount, eps)
|
|
|
|
self.y = math.lerp_eps(self.y, other.y, amount, eps)
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
-----------------------------------------------------------
|
|
|
|
-- vector products and projections
|
|
|
|
-----------------------------------------------------------
|
|
|
|
|
2021-07-23 00:58:21 +00:00
|
|
|
function vec2:dot(other)
|
|
|
|
return self.x * other.x + self.y * other.y
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--"fake", but useful - also called the wedge product apparently
|
2021-07-23 00:58:21 +00:00
|
|
|
function vec2:cross(other)
|
|
|
|
return self.x * other.y - self.y * other.x
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2021-07-23 00:58:21 +00:00
|
|
|
function vec2:scalar_projection(other)
|
|
|
|
local len = other:length()
|
2020-01-29 03:26:28 +00:00
|
|
|
if len == 0 then
|
|
|
|
return 0
|
|
|
|
end
|
2021-07-23 00:58:21 +00:00
|
|
|
return self:dot(other) / len
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:vector_projection_inplace(other)
|
2021-07-23 00:58:21 +00:00
|
|
|
local div = other:dot(other)
|
2020-01-29 03:26:28 +00:00
|
|
|
if div == 0 then
|
2021-07-23 00:58:21 +00:00
|
|
|
return self:scalar_set(0)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
2021-07-23 00:58:21 +00:00
|
|
|
local fac = self:dot(other) / div
|
2021-07-23 04:15:30 +00:00
|
|
|
return self:vector_set(other):scalar_mul_inplace(fac)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:vector_rejection_inplace(other)
|
2021-07-23 00:58:21 +00:00
|
|
|
local tx, ty = self.x, self.y
|
2021-07-23 04:15:30 +00:00
|
|
|
self:vector_projection_inplace(other)
|
2021-07-23 00:58:21 +00:00
|
|
|
self:scalar_set(tx - self.x, ty - self.y)
|
|
|
|
return self
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2020-04-01 08:28:22 +00:00
|
|
|
--get the winding side of p, relative to the line a-b
|
|
|
|
-- (this is based on the signed area of the triangle a-b-p)
|
|
|
|
-- return value:
|
|
|
|
-- >0 when p left of line
|
|
|
|
-- =0 when p on line
|
|
|
|
-- <0 when p right of line
|
|
|
|
function vec2.winding_side(a, b, p)
|
|
|
|
return (b.x - a.x) * (p.y - a.y)
|
|
|
|
- (p.x - a.x) * (b.y - a.y)
|
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
-----------------------------------------------------------
|
|
|
|
-- vector extension methods for special purposes
|
|
|
|
-- (any common vector ops worth naming)
|
|
|
|
-----------------------------------------------------------
|
|
|
|
|
|
|
|
--"physical" friction
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:apply_friction_inplace(mu, dt)
|
|
|
|
local friction = self:pooled_copy():scalar_mul_inplace(mu * dt)
|
|
|
|
if friction:length_squared() > self:length_squared() then
|
2021-07-23 00:58:21 +00:00
|
|
|
self:scalar_set(0, 0)
|
2020-01-29 03:26:28 +00:00
|
|
|
else
|
2021-07-23 04:15:30 +00:00
|
|
|
self:vector_sub_inplace(friction)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
2021-07-23 04:15:30 +00:00
|
|
|
friction:release()
|
2020-01-29 03:26:28 +00:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
--"gamey" friction in one dimension
|
2021-07-23 00:58:21 +00:00
|
|
|
local function _friction_1d(v, mu, dt)
|
2020-01-29 03:26:28 +00:00
|
|
|
local friction = mu * v * dt
|
|
|
|
if math.abs(friction) > math.abs(v) then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return v - friction
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--"gamey" friction in both dimensions
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:apply_friction_xy_inplace(mu_x, mu_y, dt)
|
2021-07-23 00:58:21 +00:00
|
|
|
self.x = _friction_1d(self.x, mu_x, dt)
|
|
|
|
self.y = _friction_1d(self.y, mu_y, dt)
|
2020-01-29 03:26:28 +00:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:50:54 +00:00
|
|
|
--minimum/maximum components
|
|
|
|
function vec2:mincomp()
|
|
|
|
return math.min(self.x, self.y)
|
|
|
|
end
|
|
|
|
|
|
|
|
function vec2:maxcomp()
|
|
|
|
return math.max(self.x, self.y)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- mask out min component, with preference to keep x
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:major_inplace()
|
2021-05-10 15:52:08 +00:00
|
|
|
if self.x > self.y then
|
|
|
|
self.y = 0
|
2021-06-08 01:50:54 +00:00
|
|
|
else
|
2021-05-10 15:52:08 +00:00
|
|
|
self.x = 0
|
|
|
|
end
|
|
|
|
return self
|
|
|
|
end
|
2021-06-08 01:50:54 +00:00
|
|
|
-- mask out max component, with preference to keep x
|
2021-07-23 04:15:30 +00:00
|
|
|
function vec2:minor_inplace()
|
2021-06-08 01:50:54 +00:00
|
|
|
if self.x < self.y then
|
2021-05-10 15:52:08 +00:00
|
|
|
self.y = 0
|
2021-06-08 01:50:54 +00:00
|
|
|
else
|
2021-05-10 15:52:08 +00:00
|
|
|
self.x = 0
|
|
|
|
end
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
--vector_ free alias; we're a vector library, so semantics should default to vector
|
|
|
|
vec2.add_inplace = vec2.vector_add_inplace
|
|
|
|
vec2.sub_inplace = vec2.vector_sub_inplace
|
|
|
|
vec2.mul_inplace = vec2.vector_mul_inplace
|
|
|
|
vec2.div_inplace = vec2.vector_div_inplace
|
|
|
|
vec2.set = vec2.vector_set
|
2021-06-08 01:50:54 +00:00
|
|
|
|
2021-08-09 09:27:29 +00:00
|
|
|
--american spelling alias
|
|
|
|
vec2.normalize_both_inplace = vec2.normalise_both_inplace
|
|
|
|
vec2.normalize_inplace = vec2.normalise_inplace
|
|
|
|
vec2.normalize_len_inplace = vec2.normalise_len_inplace
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
--garbage generating functions that return a new vector rather than modifying self
|
|
|
|
for _, inplace_name in ipairs({
|
|
|
|
"vector_add_inplace",
|
|
|
|
"vector_sub_inplace",
|
|
|
|
"vector_mul_inplace",
|
|
|
|
"vector_div_inplace",
|
2021-08-09 09:07:03 +00:00
|
|
|
"fused_multiply_add_inplace",
|
|
|
|
"add_inplace",
|
|
|
|
"sub_inplace",
|
|
|
|
"mul_inplace",
|
|
|
|
"div_inplace",
|
2021-07-23 04:15:30 +00:00
|
|
|
"scalar_add_inplace",
|
|
|
|
"scalar_sub_inplace",
|
|
|
|
"scalar_mul_inplace",
|
|
|
|
"scalar_div_inplace",
|
|
|
|
"normalise_both_inplace",
|
|
|
|
"normalise_inplace",
|
|
|
|
"normalise_len_inplace",
|
2021-08-09 09:27:29 +00:00
|
|
|
"normalize_both_inplace",
|
|
|
|
"normalize_inplace",
|
|
|
|
"normalize_len_inplace",
|
2021-07-23 04:15:30 +00:00
|
|
|
"inverse_inplace",
|
|
|
|
"rotate_inplace",
|
|
|
|
"rotate_around_inplace",
|
|
|
|
"rot90r_inplace",
|
|
|
|
"rot90l_inplace",
|
|
|
|
"lerp_direction_inplace",
|
|
|
|
"min_inplace",
|
|
|
|
"max_inplace",
|
|
|
|
"clamp_inplace",
|
|
|
|
"abs_inplace",
|
|
|
|
"sign_inplace",
|
|
|
|
"floor_inplace",
|
|
|
|
"ceil_inplace",
|
|
|
|
"round_inplace",
|
|
|
|
"lerp_inplace",
|
|
|
|
"lerp_eps_inplace",
|
|
|
|
"vector_projection_inplace",
|
|
|
|
"vector_rejection_inplace",
|
|
|
|
"apply_friction_inplace",
|
|
|
|
"apply_friction_xy_inplace",
|
|
|
|
"major_inplace",
|
|
|
|
"minor_inplace",
|
2021-07-23 00:58:21 +00:00
|
|
|
}) do
|
2021-07-23 04:15:30 +00:00
|
|
|
local garbage_name = inplace_name:gsub("_inplace", "")
|
|
|
|
vec2[garbage_name] = function(self, ...)
|
2021-07-23 00:58:21 +00:00
|
|
|
self = self:copy()
|
2021-07-23 04:15:30 +00:00
|
|
|
return self[inplace_name](self, ...)
|
2021-07-23 00:58:21 +00:00
|
|
|
end
|
2021-05-10 15:52:08 +00:00
|
|
|
end
|
|
|
|
|
2021-07-23 04:15:30 +00:00
|
|
|
--"hungarian" shorthand aliases for compatibility and short names
|
|
|
|
--
|
|
|
|
--i do encourage using the longer versions above as it makes code easier
|
|
|
|
--to understand when you come back, but i also appreciate wanting short code
|
2021-07-23 00:58:21 +00:00
|
|
|
for _, v in ipairs({
|
2021-07-23 04:15:30 +00:00
|
|
|
{"sset", "scalar_set"},
|
|
|
|
{"sadd", "scalar_add"},
|
|
|
|
{"ssub", "scalar_sub"},
|
|
|
|
{"smul", "scalar_mul"},
|
|
|
|
{"sdiv", "scalar_div"},
|
2021-08-09 09:07:03 +00:00
|
|
|
{"vset", "vector_set"},
|
2021-07-23 04:15:30 +00:00
|
|
|
{"vadd", "vector_add"},
|
|
|
|
{"vsub", "vector_sub"},
|
|
|
|
{"vmul", "vector_mul"},
|
|
|
|
{"vdiv", "vector_div"},
|
|
|
|
--(no plain addi etc, imo it's worth differentiating vaddi vs saddi)
|
|
|
|
{"fma", "fused_multiply_add"},
|
|
|
|
{"vproj", "vector_projection"},
|
|
|
|
{"vrej", "vector_rejection"},
|
|
|
|
--just for the _inplace -> i shorthand, mostly for backwards compatibility
|
|
|
|
{"min", "min"},
|
|
|
|
{"max", "max"},
|
|
|
|
{"clamp", "clamp"},
|
|
|
|
{"abs", "abs"},
|
|
|
|
{"sign", "sign"},
|
|
|
|
{"floor", "floor"},
|
|
|
|
{"ceil", "ceil"},
|
|
|
|
{"round", "round"},
|
|
|
|
{"lerp", "lerp"},
|
|
|
|
{"rotate", "rotate"},
|
|
|
|
{"normalise", "normalise"},
|
2021-08-09 09:27:29 +00:00
|
|
|
{"normalize", "normalize"},
|
2021-07-23 00:58:21 +00:00
|
|
|
}) do
|
|
|
|
local shorthand, original = v[1], v[2]
|
2021-07-23 04:15:30 +00:00
|
|
|
if vec2[shorthand] == nil then
|
|
|
|
vec2[shorthand] = vec2[original]
|
|
|
|
end
|
|
|
|
--and inplace version
|
|
|
|
shorthand = shorthand .. "i"
|
|
|
|
original = original .. "_inplace"
|
|
|
|
if vec2[shorthand] == nil then
|
|
|
|
vec2[shorthand] = vec2[original]
|
|
|
|
end
|
2021-05-10 15:52:08 +00:00
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
return vec2
|