Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Apr 12, 2024
1 parent cf0da13 commit f2285d0
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,32 @@ kd_tree.nearest([1.0, 1.0], 2)

### Complex objects

`Kd::Tree(T)` can accept any object that responds to `#size` and `#[](i : Int)` methods.

```crystal
class GeoLocation
property name : String
property longitude : Float64
property latitude : Float64
def initialize(@name : String, @longitude : Float64, @latitude : Float64)
end
# Define an indexer to allow easy access by index for longitude and latitude
def [](index : Int32) : Float64
case index
when 0 then @longitude
when 1 then @latitude
else raise "Index out of bounds"
end
end
# Assuming all GeoLocation objects are 2-dimensional
def size
2
end
end
# Create an array of GeoLocation points
points = [
GeoLocation.new("New York", -73.935242, 40.730610),
Expand Down

0 comments on commit f2285d0

Please sign in to comment.