Author jorge-brito
Popularity
1 Star
Updated Last
3 Years Ago
Started In
April 2021

Implementation of Quadtrees using the julia programming language. This package is intended to be used with the Alexya.jl package and the Luxor.jl package, but it can be used independently.

Installation

`import Pkg; Pkg.add("Quadtrees")`

Or

``````julia> ] add Quadtrees
``````

How to use

First, import the `Quadtrees` module:

```using Quadtrees

# or

Then, define the data type you need to use with the quadtree. Remember that your type must have a 2D position.

```mutable struct Particle
x::Real
y::Real
end```

Before constructing the `quadtree` object, you need to define the `Quadtrees.position` method for your type:

`Quadtrees.position(p::Particle) = (p.x, p.y)`

The `Quadtrees.position` method is used to determine the position of each element in the quadtree, so it's important to define it, otherwise your code won't work.

Then, create your `quadtree`.

`qt = Quadtree{Particle}((0, 0), 400, 400, 4)`

The `Quadtree` constructor accept four parameters:

1. A tuple of two real numbers (x, y), that determines the `quadtree's` center position.
2. The `quadtree's` width.
3. The `quadtree's` height.
4. The `quadtree's` capacity (how many items it will have before dividing).

Then, create your objects (`Particles` in this case), and insert then in your `quadtree`.

```for i in 0:999
# two random numbers between 0 and 200
x, y = rand(Float64, 2) * 200
radius = rand(Float64) * 49 + 1
# insert the particle into the quadtree.
insert!(qt, p)
end```

Now you can `search` the `quadtree` for items (`Particles`, in this case), that are located within a circular range, located at `(x, y)` with radius `r`.

For example, searching for particles within a range located at `(5, 10)` with radius `50`:

```# s for search
sx, sy = 5, 10
sr = 50
result = search(qt, sx, sy, sr)```

The return is an array of the object type that the quadtree contains.

Note: If no object is within the range, the result will be an empty array.

Now you can do whatever you want with the objects that the search found:

For example, i'll make each particle object 20% bigger:

```for particle in result
end```

TODO

1. implement the `clear` method.