SHARE
TWEET

quadtrees

a guest Sep 17th, 2015 77 Never
  1. if ( mouse_check_button_pressed ( mb_left ) )
  2. {
  3.     // remove point
  4.     var i = 0; var rebuild = -1;
  5.     for ( i = 0; i < instance_number ( obj_point ); i++ )
  6.     {
  7.         var o = instance_find ( obj_point, i );
  8.         if ( point_distance ( mouse_x, mouse_y, o.x, o.y ) < 3 )
  9.         {
  10.             rebuild = o.parent;
  11.             o.parent.children[ o.childid ] = 0;
  12.             with ( o ) { instance_destroy ( ); }; break;
  13.         }
  14.     }
  15.     // nothing to remove - add point
  16.     if ( rebuild < 0 )
  17.     {
  18.         var o = instance_create ( mouse_x, mouse_y, obj_point );
  19.         rebuild = o;
  20.     }
  21.    
  22.     // add new point
  23.     if ( rebuild.object_index != obj_quadtree )
  24.     {
  25.         var quad = root_quad;
  26.         var child = 0;
  27.         while ( true )
  28.         {
  29.             if      ( rebuild.x <= quad.x && rebuild.y <= quad.y ) child = 0;
  30.             else if ( rebuild.x >  quad.x && rebuild.y <= quad.y ) child = 1;
  31.             else if ( rebuild.x <= quad.x && rebuild.y >  quad.y ) child = 2;
  32.             else if ( rebuild.x >  quad.x && rebuild.y >  quad.y ) child = 3;
  33.            
  34.             // the child is empty = reached the bottom - insert point, return
  35.             if ( !instance_exists ( quad.children [ child ] ) )
  36.             {
  37.                 quad.children[ child ] = rebuild;
  38.                 rebuild.parent = quad;
  39.                 rebuild.childid = child;
  40.                 break;
  41.             }
  42.             // the child is a quad - go deeper
  43.             else if ( quad.children[ child ].object_index == obj_quadtree )
  44.                 quad = quad.children[ child ];
  45.             // the child is a point - replace with quad, go deeper
  46.             else
  47.             {
  48.                 // save current point
  49.                 var o = quad.children[ child ];
  50.                 // create new quad in place of point
  51.                 var xq = quad.xs / 2; if ( child == 0 || child == 2 ) xq = -xq;
  52.                 var yq = quad.ys / 2; if ( child == 0 || child == 1 ) yq = -yq;
  53.                 var q = instance_create ( quad.x + xq, quad.y + yq, obj_quadtree );
  54.                 q.xs = abs ( xq ); q.ys = abs ( yq ); q.parent = quad; q.childid = child;
  55.                 quad.children[ child ] = q;
  56.                
  57.                 // put point into new quad
  58.                 var c = 0;
  59.                 if      ( o.x <= q.x && o.y <= q.y ) c = 0;
  60.                 else if ( o.x >  q.x && o.y <= q.y ) c = 1;
  61.                 else if ( o.x <= q.x && o.y >  q.y ) c = 2;
  62.                 else if ( o.x >  q.x && o.y >  q.y ) c = 3;
  63.                 o.parent = q; o.childid = c;
  64.                 q.children [ c ] = o;
  65.                 // go deeper
  66.                 quad = q;
  67.             }
  68.         }
  69.     }
  70.     // remove leaf
  71.     else
  72.     {
  73.         while ( true )
  74.         {
  75.             // root quad has to stay in place
  76.             if ( rebuild == root_quad )
  77.                 break;
  78.            
  79.             // the quad has no children - detroy it
  80.             if ( !instance_exists ( rebuild.children[ 0 ] ) &&
  81.                  !instance_exists ( rebuild.children[ 1 ] ) &&
  82.                  !instance_exists ( rebuild.children[ 2 ] ) &&
  83.                  !instance_exists ( rebuild.children[ 3 ] ) )
  84.             {
  85.                 var p = rebuild.parent;
  86.                 rebuild.parent.children[ rebuild.childid ] = 0;
  87.                 with ( rebuild ) { instance_destroy ( ); };
  88.                 // upper level quad now may have no children as well
  89.                 rebuild = p;
  90.             }
  91.             else
  92.                 break;
  93.         }
  94.     }
  95. }
RAW Paste Data
Top