a bit of refactoring for quad tree's 'toTriangles' function

This commit is contained in:
2025-07-06 13:26:33 +01:00
parent 1ce3a36db4
commit 833005703b

View File

@@ -213,7 +213,7 @@ struct
NODE {tl = tl, bl = bl, tr = tr, br = br} NODE {tl = tl, bl = bl, tr = tr, br = br}
end end
fun foldWithDuplicates (f, tree, acc) = fun foldWithDuplicates (f, tree, acc) =
case tree of case tree of
LEAF item => f (item, acc) LEAF item => f (item, acc)
| NODE {tl, tr, bl, br} => | NODE {tl, tr, bl, br} =>
@@ -226,34 +226,37 @@ struct
end end
fun insertItemIntoTree (item, acc) = fun insertItemIntoTree (item, acc) =
if #data item = 0 then if #data item = 0 then
(* ignore specific data by not inserting it into tree. (* ignore specific data by not inserting it into tree.
* May later functorise this quad tree, * May later functorise this quad tree,
* and allow different types of data * and allow different types of data
* to be ignored/stored in #data field. *) * to be ignored/stored in #data field. *)
acc acc
else else
BinTree.insert (item, acc) BinTree.insert (item, acc)
fun toList qtree = fun toList qtree =
let let val tree = foldWithDuplicates (insertItemIntoTree, qtree, BinTree.empty)
val tree = foldWithDuplicates (insertItemIntoTree, qtree, BinTree.empty) in BinTree.toList (tree, [])
in
BinTree.toList (tree, [])
end end
fun toTriangles (windowWidth, windowHeight, squares, acc) = local
case squares of fun loop (windowWidth, windowHeight, squares, acc) =
{x, y, ex, ey, data = _} :: tl => case squares of
let {x, y, ex, ey, data = _} :: tl =>
val startX = Ndc.fromPixelX (x, windowWidth, windowHeight) let
val endX = Ndc.fromPixelX (ex, windowWidth, windowHeight) val startX = Ndc.fromPixelX (x, windowWidth, windowHeight)
val startY = Ndc.fromPixelY (y, windowWidth, windowHeight) val endX = Ndc.fromPixelX (ex, windowWidth, windowHeight)
val endY = Ndc.fromPixelY (ey, windowWidth, windowHeight) val startY = Ndc.fromPixelY (y, windowWidth, windowHeight)
val endY = Ndc.fromPixelY (ey, windowWidth, windowHeight)
val acc = Ndc.ltrbToVertex (startX, startY, endX, endY) :: acc val acc = Ndc.ltrbToVertex (startX, startY, endX, endY) :: acc
in in
toTriangles (windowWidth, windowHeight, tl, acc) loop (windowWidth, windowHeight, tl, acc)
end end
| [] => Vector.concat acc | [] => Vector.concat acc
in
fun toTriangles (windowWidth, windowHeight, squares) =
loop (windowWidth, windowHeight, squares, [])
end
end end