Merge pull request #4 from TangentFoxy/TangentFoxy-patch-1

Close #3 export normalize function
This commit is contained in:
2026-07-18 05:23:46 +00:00
committed by GitHub
2 changed files with 20 additions and 14 deletions
+3
View File
@@ -39,6 +39,9 @@ map = heightmap.create(150, 300, -1, 1)
-- use the custom height function and normalize with a range of 10 to 20 -- use the custom height function and normalize with a range of 10 to 20
map = heightmap.create(200, 200, f, 10, 20) map = heightmap.create(200, 200, f, 10, 20)
-- re-normalize a map in-place at any time (example range of 0 to 1)
heightmap.normalize(map, 0, 1)
``` ```
How it Works How it Works
+17 -14
View File
@@ -24,6 +24,20 @@ local function max(t)
return r return r
end end
local function normalize(map, new_min, new_max)
local minimum = min(map)
local initialRange = max(map) - minimum
local finalRange = new_max - new_min
for i = 0, #map do
for j = 0, #map[0] do
map[i][j] = (map[i][j] - minimum) / initialRange * finalRange + new_min
end
end
map.min = min(map)
map.max = max(map)
return map -- superfluous
end
local function create(width, height, f_or_min, min_or_max, max_if_f) local function create(width, height, f_or_min, min_or_max, max_if_f)
local map, rangeMin, rangeMax local map, rangeMin, rangeMax
if type(f_or_min) == "function" then if type(f_or_min) == "function" then
@@ -36,22 +50,11 @@ local function create(width, height, f_or_min, min_or_max, max_if_f)
rangeMax = min_or_max rangeMax = min_or_max
end end
local minimum = min(map) return normalize(map, rangeMin, rangeMax)
local initialRange = max(map) - minimum
local finalRange = rangeMax - rangeMin
for i=0,#map do
for j=0,#map[0] do
map[i][j] = (map[i][j] - minimum) / initialRange * finalRange + rangeMin
end
end
map.min = min(map)
map.max = max(map)
return map
end end
return { return {
create = create, create = create,
defaultf = heightmap.defaultf defaultf = heightmap.defaultf,
normalize = normalize,
} }