Class: Quadtree::Point
- Inherits:
-
Object
- Object
- Quadtree::Point
- Defined in:
- lib/quadtree/point.rb
Overview
Simple coordinate object to represent points in some space.
Instance Attribute Summary collapse
-
#data ⇒ Object
Optional payload attached to this instance.
-
#x ⇒ Float, Integer
The X coordinate of this instance.
-
#y ⇒ Float, Integer
The Y coordinate of this instance.
Class Method Summary collapse
-
.from_json(json_data) ⇒ Quadtree::Point
Construct a Point from a JSON String.
Instance Method Summary collapse
-
#distance_to(other) ⇒ Float
This will calculate distance to another Point, given that they are both in the same 2D space.
-
#haversine_distance_to(other) ⇒ Float
This will calculate distance to another Point using the Haversine formula.
-
#initialize(x, y, data = nil) ⇒ Point
constructor
A new instance of Point.
-
#to_h ⇒ Hash
Create a Hash for this Point.
-
#to_hash ⇒ Hash
Create a Hash for this Point.
-
#to_json(*_args) ⇒ String
Create a JSON String representation of this Point.
-
#to_s ⇒ String
Create a String for this Point.
Constructor Details
#initialize(x, y, data = nil) ⇒ Point
Returns a new instance of Point.
24 25 26 27 28 29 |
# File 'lib/quadtree/point.rb', line 24 def initialize(x, y, data = nil) self.x = get_typed_numeric(x) self.y = get_typed_numeric(y) self.data = data unless data.nil? end |
Instance Attribute Details
#data ⇒ Object
Optional payload attached to this instance.
16 17 18 |
# File 'lib/quadtree/point.rb', line 16 def data @data end |
#x ⇒ Float, Integer
The X coordinate of this instance.
8 9 10 |
# File 'lib/quadtree/point.rb', line 8 def x @x end |
#y ⇒ Float, Integer
The Y coordinate of this instance.
12 13 14 |
# File 'lib/quadtree/point.rb', line 12 def y @y end |
Class Method Details
.from_json(json_data) ⇒ Quadtree::Point
Construct a Quadtree::Point from a JSON String.
79 80 81 |
# File 'lib/quadtree/point.rb', line 79 def self.from_json(json_data) new(json_data['x'], json_data['y'], json_data['data']) end |
Instance Method Details
#distance_to(other) ⇒ Float
This will calculate distance to another Quadtree::Point, given that they are both in the same 2D space.
88 89 90 |
# File 'lib/quadtree/point.rb', line 88 def distance_to(other) Math.sqrt((other.x - x)**2 + (other.y - y)**2) end |
#haversine_distance_to(other) ⇒ Float
This will calculate distance to another Quadtree::Point using the Haversine formula. This means that it will treat #x as longitude and #y as latitude!
a = sin²(Δφ/2) + cos φ_1 ⋅ cos φ_2 ⋅ sin²(Δλ/2)
c = 2 ⋅ atan2( √a, √(1−a) )
d = R ⋅ c
where φ is latitude, λ is longitude, R is earth’s radius (mean radius = 6 371 km); note that angles need to be in radians to pass to trig functions!
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/quadtree/point.rb', line 108 def haversine_distance_to(other) lat1 = y * (Math::PI / 180.0) lat2 = other.y * (Math::PI / 180.0) dlat = (other.y - y) * (Math::PI / 180.0) dlon = (other.x - x) * (Math::PI / 180.0) # a = sin²(Δφ/2) + cos φ_1 ⋅ cos φ_2 ⋅ sin²(Δλ/2) a = calculate_haversine_a(lat1, lat2, dlat, dlon) # c = 2 ⋅ atan2( √a, √(1−a) ) c = 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)) # d = R ⋅ c 6371 * 1000.0 * c end |
#to_h ⇒ Hash
Create a Hash for this Quadtree::Point.
36 37 38 39 40 41 42 |
# File 'lib/quadtree/point.rb', line 36 def to_h { 'x': x, 'y': y, 'data': process_data(data) } end |
#to_hash ⇒ Hash
Create a Hash for this Quadtree::Point.
49 50 51 |
# File 'lib/quadtree/point.rb', line 49 def to_hash to_h end |
#to_json(*_args) ⇒ String
Create a JSON String representation of this Quadtree::Point.
58 59 60 61 |
# File 'lib/quadtree/point.rb', line 58 def to_json(*_args) require 'json' to_h.to_json end |
#to_s ⇒ String
Create a String for this Quadtree::Point.
68 69 70 |
# File 'lib/quadtree/point.rb', line 68 def to_s to_h.to_s end |