add functionality to change grid size for layer tree

This commit is contained in:
2025-08-09 08:04:22 +01:00
parent 318ca4fdf1
commit 8e475a3abe
2 changed files with 46 additions and 7 deletions

BIN
dotscape

Binary file not shown.

View File

@@ -1,6 +1,8 @@
structure LayerTree =
struct
type square = {r: int, g: int, b: int, a: int}
val emptyPixel = {r = 0, g = 0, b = 0, a = 0}
type grid = square vector vector
fun isBlank ({a, ...}: square) = a = 0
@@ -32,6 +34,29 @@ struct
else if searchKey > key then get (searchKey, right)
else SOME value
fun foldl (f, tree, acc) =
case tree of
LEAF => acc
| NODE {value, left, right, ...} =>
let
val acc = foldl (f, left, acc)
val acc = f (value, acc)
in
foldl (f, right, acc)
end
fun map (f, tree) =
case tree of
LEAF => LEAF
| NODE {key, value, left, right} =>
let
val left = map (f, left)
val right = map (f, right)
val newValue = f value
in
NODE {key = key, value = value, left = left, right = right}
end
(* copies non-blank pixels in value vector into acc *)
fun helpFlatten (value, acc) =
Vector.mapi
@@ -45,14 +70,28 @@ struct
else
valuePixel) valueYAxis) value
fun flatten (tree, acc) =
case tree of
LEAF => acc
| NODE {value, left, right, ...} =>
fun makeEmptyGrid maxSide =
Vector.tabulate (maxSide, fn _ =>
Vector.tabulate (maxSide, fn _ => emptyPixel))
fun flatten (maxSide, tree) =
foldl (helpFlatten, tree, makeEmptyGrid maxSide)
fun helpChangeGridSize maxSide squares =
Vector.tabulate (maxSide, fn i =>
if i < Vector.length squares then
let
val acc = flatten (left, acc)
val acc = helpFlatten (value, acc)
val yAxis = Vector.sub (squares, i)
in
flatten (right, acc)
Vector.tabulate (maxSide, fn ii =>
if ii < Vector.length yAxis then Vector.sub (yAxis, ii)
else emptyPixel)
end
else
Vector.tabulate (maxSide, fn _ => emptyPixel))
fun changeGridSize (maxSide, tree) =
let val f = helpChangeGridSize maxSide
in map (f, tree)
end
end