Move hilbert curve lib into library directory

This commit is contained in:
David Vogel 2022-07-23 10:55:44 +02:00
parent 8841a57185
commit 2acc4e7e93
2 changed files with 25 additions and 5 deletions

View File

@ -6,6 +6,9 @@
---@type NoitaEntityAPI ---@type NoitaEntityAPI
local EntityAPI = dofile_once("mods/noita-mapcap/files/libraries/noita-api/entity.lua") local EntityAPI = dofile_once("mods/noita-mapcap/files/libraries/noita-api/entity.lua")
---@type HilbertLib
local Hilbert = dofile_once("mods/noita-mapcap/files/libraries/hilbert-curve.lua")
---@type JSONLib ---@type JSONLib
local json = dofile_once("mods/noita-mapcap/files/libraries/json.lua") local json = dofile_once("mods/noita-mapcap/files/libraries/json.lua")
@ -354,7 +357,7 @@ function startCapturingHilbert(area)
async( async(
function() function()
while t < tLimit do while t < tLimit do
local hx, hy = mapHilbert(t, gridPOTSize) local hx, hy = Hilbert.Map(t, gridPOTSize)
if hx < gridWidth and hy < gridHeight then if hx < gridWidth and hy < gridHeight then
local x, y = (hx + gridLeft) * CAPTURE_GRID_SIZE, (hy + gridTop) * CAPTURE_GRID_SIZE local x, y = (hx + gridLeft) * CAPTURE_GRID_SIZE, (hy + gridTop) * CAPTURE_GRID_SIZE
x, y = x + 256, y + 256 -- Align screen with ingame chunk grid that is 512x512. x, y = x + 256, y + 256 -- Align screen with ingame chunk grid that is 512x512.

View File

@ -1,8 +1,19 @@
-- Copyright (c) 2019-2020 David Vogel -- Copyright (c) 2019-2022 David Vogel
-- --
-- This software is released under the MIT License. -- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT -- https://opensource.org/licenses/MIT
---@class HilbertLib
local Hilbert = {}
---Rotate/flip quadrant.
---@param n integer
---@param x integer
---@param y integer
---@param rx boolean
---@param ry boolean
---@return integer
---@return integer
local function hilbertRotate(n, x, y, rx, ry) local function hilbertRotate(n, x, y, rx, ry)
if not ry then if not ry then
if rx then if rx then
@ -15,13 +26,17 @@ local function hilbertRotate(n, x, y, rx, ry)
return x, y return x, y
end end
-- Maps a variable t to a hilbert curve with the side length of 2^potSize (Power of two size) ---Maps t in the range of [0, (2^potSize)^2-1] to a position on the hilbert curve with the side length of 2^potSize (Power of two size).
function mapHilbert(t, potSize) ---@param t integer
---@param potSize integer
---@return integer
---@return integer
function Hilbert.Map(t, potSize)
local size = math.pow(2, potSize) local size = math.pow(2, potSize)
local x, y = 0, 0 local x, y = 0, 0
if t < 0 or t >= size * size then if t < 0 or t >= size * size then
error("Variable t is outside of the range") error("variable t is outside of the range")
end end
for i = 0, potSize - 1, 1 do for i = 0, potSize - 1, 1 do
@ -46,3 +61,5 @@ function mapHilbert(t, potSize)
return x, y return x, y
end end
return Hilbert