Class: Quadtree::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/quadtree/point.rb

Overview

Simple coordinate object to represent points in some space.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, data = nil) ⇒ Point

Returns a new instance of Point.

Parameters:

  • x (Float, Integer)

    X coordinate.

  • y (Float, Integer)

    Y coordinate.

  • data (Object) (defaults to: nil)

    payload payload attached to this instance (optional).

Raises:

  • (UnknownTypeError)

    if one or more input parameters (x and y) has the wrong type.



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

#dataObject

Optional payload attached to this instance.

Returns:

  • (Object)

    payload attached to this instance.



16
17
18
# File 'lib/quadtree/point.rb', line 16

def data
  @data
end

#xFloat, Integer

The X coordinate of this instance.

Returns:

  • (Float, Integer)

    X coordinate.



8
9
10
# File 'lib/quadtree/point.rb', line 8

def x
  @x
end

#yFloat, Integer

The Y coordinate of this instance.

Returns:

  • (Float, Integer)

    Y coordinate.



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.

Parameters:

  • json_data (String)

    input JSON String.

Returns:



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.

Parameters:

Returns:



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!

Parameters:

Returns:



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_hHash

Create a Hash for this Quadtree::Point.

Returns:



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_hashHash

Create a Hash for this Quadtree::Point.

Returns:



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.

Returns:



58
59
60
61
# File 'lib/quadtree/point.rb', line 58

def to_json(*_args)
  require 'json'
  to_h.to_json
end

#to_sString

Create a String for this Quadtree::Point.

Returns:



68
69
70
# File 'lib/quadtree/point.rb', line 68

def to_s
  to_h.to_s
end