Remove named children #45
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The named children of the quadtree (i.e. northeast, northwest, southeast, southwest) are only ever used internally to the tree itself and anything done to one of the children is always done to all four of them; this adds clunkiness to it where you have repeated calls to the same function on each of the children.
For example, in the draw method you have...
By removing these and instead using an array called
children
it allows you to compress a lot of this repetition while not losing out on the readability.The above would become...
The query function becomes significantly simpler. The original code...
becomes...
I have also moved the creation of the sub-Rectangles into the
Rectangle
class so you can now dothis.boundary.ne()
to create the northEast boundary to pass into the child quadTree.I guess a lot of this is personal preference and I know this is an old repo but I've been really enjoying the series during the recent isolation and thought I'd contribute 😄
Thanks
N.B. There is one bit of this change that I don't like and that's the serialization into and out of JSON. The current method writes out to the
obj.ne
,obj.nw
and so on. I have kept this for backwards compatibility with people using this repo but it means that the order of the children being defined needs to be correct. The order I went with is the same order that the children were originally defined (ne, nw, se, sw). However, there is nothing (other than tests) to stop this.Perhaps this could be improved by either: