make odd-number-division case more explicit when building quad tree

This commit is contained in:
2025-07-07 22:37:01 +01:00
parent 6b5c0d9ba1
commit ab9e1e23d0
2 changed files with 48 additions and 22 deletions

View File

@@ -29,15 +29,24 @@ struct
| NODE {x = ox, y = oy, ex = oex, ey = oey, data = oldData, left, right} =>
let
val dir =
if x < ox then LESS
else if x > ox then GREATER
else if y < oy then LESS
else if y > oy then GREATER
else if ex < oex then LESS
else if ex > oex then GREATER
else if ey < oey then LESS
else if ey > oey then GREATER
else EQUAL
if x < ox then
LESS
else if x > ox then
GREATER
else
(if y < oy then
LESS
else if y > oy then
GREATER
else
(if ex < oex then
LESS
else if ex > oex then
GREATER
else
(if ey < oey then LESS
else if ey > oey then GREATER
else EQUAL)))
in
case dir of
LESS =>
@@ -147,19 +156,36 @@ struct
end
end
else
let
(* handles odd-number divisions.
* For example, `7 div 2` is 3 because of integer division. *)
val halfSize =
if size = 1 orelse size mod 2 = 0 then size div 2
else (size + 1) div 2
val bintree = build (x, y, halfSize, grid, bintree)
val bintree = build (x + halfSize, y, halfSize, grid, bintree)
val bintree = build (x, y + halfSize, halfSize, grid, bintree)
in
build (x + halfSize, y + halfSize, halfSize, grid, bintree)
end
(if size mod 2 = 0 orelse size = 1 then
let
val halfSize = size div 2
val bintree = build (x, y, halfSize, grid, bintree)
val bintree = build (x + halfSize, y, halfSize, grid, bintree)
val bintree = build (x, y + halfSize, halfSize, grid, bintree)
in
build (x + halfSize, y + halfSize, halfSize, grid, bintree)
end
else
(* handles odd-number divisions.
* For example, `7 div 2` is 3 because of integer division.
* We would not cover every pixel unless we handle odd numbers specially. *)
let
val halfSizeBefore = size div 2
val halfSizeAfter = (size + 1) div 2
val bintree = build (x, y, halfSizeAfter, grid, bintree)
val bintree = build
(x + halfSizeBefore, y, halfSizeAfter, grid, bintree)
val bintree = build
(x, y + halfSizeBefore, halfSizeAfter, grid, bintree)
in
build
( x + halfSizeBefore
, y + halfSizeBefore
, halfSizeAfter
, grid
, bintree
)
end)
local
fun getClickPoint (clickPoints, pos) =