noita-mapcap/files/hilbert.lua
David Vogel 8cf46232de Several updates
- Add full map capture mode
- Show error on screen if screencapturing failed
- Add additional frame delay for jumps larger than the grid size
- Show progress for map capture mode
- Tweak streaming chunk target
- Update README.md
2019-11-02 21:37:10 +01:00

49 lines
873 B
Lua

-- Copyright (c) 2019 David Vogel
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT
local function hilbertRotate(n, x, y, rx, ry)
if not ry then
if rx then
x = n - 1 - x
y = n - 1 - y
end
x, y = y, x
end
return x, y
end
-- Maps a variable t to a hilbert curve with the side length of 2^potSize (Power of two size)
function mapHilbert(t, potSize)
local size = math.pow(2, potSize)
local x, y = 0, 0
if t < 0 or t >= size * size then
error("Variable t is outside of the range")
end
for i = 0, potSize - 1, 1 do
local iPOT = math.pow(2, i)
local rx = bit.band(t, 2) == 2
local ry = bit.band(t, 1) == 1
if rx then
ry = not ry
end
x, y = hilbertRotate(iPOT, x, y, rx, ry)
if rx then
x = x + iPOT
end
if ry then
y = y + iPOT
end
t = math.floor(t / 4)
end
return x, y
end