вот продолжаю разбирать пример ...

; кликайте мышью в смежных квадрантах
;
;
Global p1x,p1y
Global p2x,p2y
Global id,level ,xx#,yy#
; константы детей
Const CHILD00 = 0
Const CHILD01 = 1
Const CHILD11 = 2
Const CHILD10 = 3
; QUADTREE
Type QUADTREE
Field Child.QUADTREE[3] ;четыре потомка
Field xmin,ymin ; начальные координаты квадранта
Field xmax,ymax ; оконечные координаты квадранта
Field id ,lev
End Type
; ============= создаём квадро-дерево =================
Function Quadtree.QUADTREE(xmin,ymin,xmax,ymax,depth)
this.QUADTREE = New QUADTREE
this\xmin = xmin
this\xmax = xmax
this\ymin = ymin
this\ymax = ymax
id = id + 1
this\id = id
this\lev = 0
If (depth > 0)
; деление квадранта на 4-ре квадранта
xmoy = (xmin+xmax) / 2
ymoy = (ymin+ymax) / 2
depth = depth - 1
this\Child[CHILD00] = Quadtree(xmin,ymin,xmoy,ymoy,depth)
this\Child[CHILD01] = Quadtree(xmin,ymoy,xmoy,ymax,depth)
this\Child[CHILD11] = Quadtree(xmoy,ymoy,xmax,ymax,depth)
this\Child[CHILD10] = Quadtree(xmoy,ymin,xmax,ymoy,depth)
EndIf
Return this
End Function
; ==================== Рендер квадро-дерева =====================
Function RenderQuadtree(this.QUADTREE,depth)
If RectsOverlap (xx#,yy#,1,1,this\xmin,this\ymin,this\xmax-this\xmin,this\ymax-this\ymin)
If (depth > 1)
Color 188,188,188
Line (this\xmin+this\xmax)/2,this\ymin,(this\xmin+this\xmax)/2,this\ymax
Line this\xmin,(this\ymin+this\ymax)/2,this\xmax,(this\ymin+this\ymax)/2
depth = depth - 1
RenderQuadtree(this\Child[CHILD00],depth)
RenderQuadtree(this\Child[CHILD01],depth)
RenderQuadtree(this\Child[CHILD11],depth)
RenderQuadtree(this\Child[CHILD10],depth)
If this\Child[CHILD00]\lev = 1 And this\Child[CHILD01]\lev = 1 And this\Child[CHILD11]\lev = 1 And this\Child[CHILD10]\lev = 1 Then
this\lev = 1
EndIf
Else
If MouseHit(1) Then this\lev = 1
If this\lev = 1 Then Color 255,0,0 : fill = True Else Color 66,66,66 : fill = False
Rect this\xmin+1,this\ymin+1,this\xmax-this\xmin-1,this\ymax-this\ymin-1,fill
Color 255,255,255
Text this\xmin+2,this\ymin+2, this\id +".id "+depth
EndIf
EndIf
If this\lev = 1 Then Color 255,0,0 : fill = True Else Color 66,66,66 : fill = False
Rect this\xmin+1,this\ymin+1,this\xmax-this\xmin-1,this\ymax-this\ymin-1,fill
Color 255,255,255
Text this\xmin+2,this\ymin+12, this\lev
End Function
;==============================================================================================
; Сама Программа :
;==============================================================================================
AppTitle "квадро-дерево "
Graphics 512,512,0,2
SetBuffer BackBuffer()
ClsColor 122,122,122
QuadDepth = 5;число вложений (глубина ) : Level 's
QuadSize = 512 ; размеры квадранта
; создание основного квадранта
root.QUADTREE = Quadtree(0,0,QuadSize,QuadSize,QuadDepth)
While Not KeyHit(1)
Cls
xx# = MouseX()
yy# = MouseY()
Select True
Case KeyDown(200) ; Up
Case KeyDown(208) ; Down
Case KeyDown(203) ; Left
Case KeyDown(205) ; Right
End Select
; Рендер квадро-дерева
Color 255,255,255
Rect root\xmin,root\ymin,root\xmax,root\ymax,0
RenderQuadtree(root,QuadDepth)
Flip
Wend
Delete Each QUADTREE
End