Calculate the smallest size of the child element of a quadtree containing 2 points, without recursion

24 Views Asked by At

Is there a way to find the smallest child element that contains both points except recursion? I have implemented a recursive algorithm and it works well. But is there any other way to do this without recursion/loop? O(1)

For simplicity, I converted my code into a 1D example (see image) The pseudocode looks like this:

dist = getDistbetweenPoints(A, B)
level = findClosestCellLevelToDist(dist) // Use relation between cell levels: cellSize = minCellSize * 2^level
newLevel = recursivlyCheckIfPointsInsideCell(level, A, B) // if not - increase the cell level

In the image below You can see that A and B fit in the cell with the size 10 (level 1). But in X-axis You can see that there is an overlapping (point A is inside one cell and point B inside another) To solve this problem i used recursion and increased cell size until all points are inside (cell size = 40 , level = 3)

Im testing it in js. Is there a way to solve it without loop or recursion? Thanks! enter image description here

0

There are 0 best solutions below