2023-07-28 05:17:00 +00:00
|
|
|
--Shitty acceleration structure: get closest point in set.
|
|
|
|
--Assumed to be in world coordinates: ( -180, 180 ) x ( -100, 100 )
|
|
|
|
local t = {}
|
|
|
|
local hashes = {}
|
|
|
|
local math = math
|
|
|
|
|
|
|
|
local hash = function( x, y, debug )
|
|
|
|
local s = "h"..("%2d%2d"):format(math.floor( 0.5 + (180 + x) / 10 ), math.floor( 0.5 + (100 + y) / 10 ) )
|
|
|
|
if debug then print( "HASH: ", s, x, y) end
|
|
|
|
return s
|
|
|
|
end
|
|
|
|
|
|
|
|
function t.getClosestPoint( points, x, y )
|
|
|
|
local closePoints = hashes[hash( x, y )]
|
|
|
|
if not closePoints then return end
|
|
|
|
local distance = math.huge
|
|
|
|
local px, py, point
|
|
|
|
for k, v in pairs(closePoints) do
|
|
|
|
|
|
|
|
px, py = v.x, v.y
|
|
|
|
local d = (x - px) * (x - px) + (y - py) * (y - py)
|
|
|
|
if d < distance then
|
|
|
|
distance = d
|
|
|
|
point = v
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
print( #closePoints, distance )
|
|
|
|
|
|
|
|
return point
|
|
|
|
end
|
|
|
|
|
|
|
|
function t.Edit( points, point, x, y )
|
|
|
|
local h = hashes[hash( point.x, point.y )]
|
|
|
|
if h then
|
|
|
|
for i, p in pairs(h) do
|
|
|
|
if p == point then
|
|
|
|
table.remove( h, i )
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert( hashes[ hash( x, y ) ], point )
|
|
|
|
point.x = x
|
|
|
|
point.y = y
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2023-08-28 23:12:43 +00:00
|
|
|
function t.Add( points, x, y )
|
2023-07-28 05:17:00 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
function t.New( points )
|
|
|
|
for i = 1, #points do
|
|
|
|
local x, y = points[i].x, points[i].y
|
|
|
|
local h = hash( x, y )
|
|
|
|
hashes[h] = hashes[h] or {}
|
|
|
|
hashes[h][#hashes[h] + 1] = points[i]
|
|
|
|
end
|
|
|
|
return setmetatable( points, {__index = t } )
|
|
|
|
end
|
|
|
|
|
|
|
|
return t
|