Split this quadtree. .. caution:: A call to split will always split the tree or raise an error. Use :py:meth:`.think` if you want to ensure the quadtree is operating efficiently. .. caution::
(self)
| 161 | child.think(True) |
| 162 | |
| 163 | def split(self): |
| 164 | """ |
| 165 | Split this quadtree. |
| 166 | |
| 167 | .. caution:: |
| 168 | |
| 169 | A call to split will always split the tree or raise an error. Use |
| 170 | :py:meth:`.think` if you want to ensure the quadtree is operating |
| 171 | efficiently. |
| 172 | |
| 173 | .. caution:: |
| 174 | |
| 175 | This function will not respect :py:attr:`.bucket_size` or |
| 176 | :py:attr:`.max_depth`. |
| 177 | |
| 178 | :raises ValueError: if :py:attr:`.children` is not empty |
| 179 | """ |
| 180 | if self.children: |
| 181 | raise ValueError("cannot split twice") |
| 182 | |
| 183 | _cls = type(self) |
| 184 | def _cstr(r): |
| 185 | return _cls(self.bucket_size, self.max_depth, r, self.depth + 1) |
| 186 | |
| 187 | _halfwidth = self.location.width / 2 |
| 188 | _halfheight = self.location.height / 2 |
| 189 | _x = self.location.mincorner.x |
| 190 | _y = self.location.mincorner.y |
| 191 | |
| 192 | self.children = [ |
| 193 | _cstr(rect2.Rect2(_halfwidth, _halfheight, vector2.Vector2(_x, _y))), |
| 194 | _cstr(rect2.Rect2(_halfwidth, _halfheight, vector2.Vector2(_x + _halfwidth, _y))), |
| 195 | _cstr(rect2.Rect2(_halfwidth, _halfheight, vector2.Vector2(_x + _halfwidth, _y + _halfheight))), |
| 196 | _cstr(rect2.Rect2(_halfwidth, _halfheight, vector2.Vector2(_x, _y + _halfheight))) ] |
| 197 | |
| 198 | _newents = [] |
| 199 | for ent in self.entities: |
| 200 | quad = self.get_quadrant(ent) |
| 201 | |
| 202 | if quad < 0: |
| 203 | _newents.append(ent) |
| 204 | else: |
| 205 | self.children[quad].entities.append(ent) |
| 206 | self.entities = _newents |
| 207 | |
| 208 | |
| 209 |