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