Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a more compact version. It uses the same data structure as the original version. The functions <code>splitBox</code> and <code>insideBox</code> are essentially the same as well (just written in a slightly different way). </p> <p>Instead of adding points one-by-one, the initial box contains all the points at the beginning so there is no need for the <code>qtInsert</code> routines. In each recursion step, the boxes containing more than one point are split and the points are distributed over the sub-boxes. This means that all nodes with more than one point are leafs so there is no need to check for that either.</p> <pre><code>qtMakeNode[bb_, pts_] := {{}, {}, {}, {}, qtbb @@ bb, pts} splitBox[bx_] := splitBox[{min_, max_}] := {min + #, max + #}/2 &amp; /@ Tuples[Transpose[{min, max}]] insideBox[pt_, bb_] := bb[[1, 1]] &lt;= pt[[1]] &lt;= bb[[2, 1]] &amp;&amp; bb[[1, 2]] &lt;= pt[[2]] &lt;= bb[[2, 2]] distribute[qtree_] := Which[ Length[qtree[[6]]] == 1, (* no points in node -&gt; return node unchanged *) qtree, Length[qtree[[6]]] == 1, (* one point in node -&gt; replace head of point with qtpt and return node *) ReplacePart[qtree, 6 -&gt; qtpt @@ qtree[[6, 1]]], Length[qtree[[6]]] &gt; 1, (* multiple points in node -&gt; create sub-nodes and distribute points *) (* apply distribute to sub-nodes *) Module[{spl = splitBox[qtree[[5]]], div, newtreelist}, div = Cases[qtree[[6]], a_ /; insideBox[a, #], 1] &amp; /@ spl; ReplacePart[qtree, Join[Table[i -&gt; distribute[qtMakeNode[spl[[i]], div[[i]]]], {i, 4}], {6 -&gt; {}}]]]] </code></pre> <p>Example (using the original version of <code>qtDraw</code>):</p> <pre><code>len = 50; pts = RandomReal[{0, 2}, {len, 2}]; qt = makeTree[qtMakeNode[{{0.0, 0.0}, {2.0, 2.0}}, pts]]; qtDraw[qt] </code></pre> <p>Result:</p> <p><img src="https://i.stack.imgur.com/kNyrg.jpg" alt="Quadtree example"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload