Class: GeoMagic::Radius

Inherits:
Object
  • Object
show all
Defined in:
lib/geo_magic/radius.rb

Constant Summary collapse

PI =

IN BAAAAD !!! need of refactoring and DRYing up!!!

3.14159265
PI_2 =
PI * 2
NORMALIZER =
100.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(center, distance) ⇒ Radius

Returns a new instance of Radius.



5
6
7
8
# File 'lib/geo_magic/radius.rb', line 5

def initialize center, distance
  @center = center
  @distance = distance
end

Instance Attribute Details

#centerObject

Returns the value of attribute center.



3
4
5
# File 'lib/geo_magic/radius.rb', line 3

def center
  @center
end

#distanceObject

Returns the value of attribute distance.



3
4
5
# File 'lib/geo_magic/radius.rb', line 3

def distance
  @distance
end

Class Method Details

.get_random_radiant(range) ⇒ Object



86
87
88
# File 'lib/geo_magic/radius.rb', line 86

def self.get_random_radiant range
  rand(range * 2) - range
end

Instance Method Details

#create_point_within(mode = :square) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
# File 'lib/geo_magic/radius.rb', line 15

def create_point_within mode = :square
  raise ArgumentError, "mode must be :circle or :square" if ![:circle, :square].include? mode
  send :"create_point_within_#{mode}"
end

#create_point_within_circleObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/geo_magic/radius.rb', line 70

def create_point_within_circle
  max_radius_rad = dist.distance
  range = normalize max_radius_rad

  q = rand(range) * PI_2
  r = Math.sqrt(rand(range))
  dlong = denormalize (range * r) * Math.cos(q)
  dlat = denormalize (range * r) * Math.sin(q)
  
  GeoMagic::Point.new @center.latitude + dlat, @center.longitude + dlong      
end

#create_point_within_squareObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/geo_magic/radius.rb', line 58

def create_point_within_square 
  conversion = GeoDistance.radians_ratio(distance.unit)      

  max_radius_rad = dist.distance
  range = (max_radius_rad * normalize).to_i          

  dlong = (get_random_radiant(range) / normalize)
  dlat  = (get_random_radiant(range) / normalize)
  
  GeoMagic::Point.new @center.latitude + dlat, @center.longitude + dlong
end

#create_points_in_circle(number) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/geo_magic/radius.rb', line 36

def create_points_in_circle number
  conversion = GeoDistance.radians_ratio(distance.unit)

  max_radius_rad = distance.distance
  range = normalize max_radius_rad

  max_radius_rad = distance.distance
  range = normalize max_radius_rad

  number.times.inject([]) do |res, n|
    q = rand(range) * PI_2
    r = Math.sqrt(rand(range))

    dlong = denormalize (range * r) * Math.cos(q)
    dlat  = denormalize (range * r) * Math.sin(q)

    point = GeoMagic::Point.new @center.latitude + dlat, @center.longitude + dlong
    res << point
    res
  end
end

#create_points_in_square(number) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/geo_magic/radius.rb', line 20

def create_points_in_square number
  conversion = GeoDistance.radians_ratio(distance.unit)

  max_radius_rad = distance.distance
  range = normalize max_radius_rad

  number.times.inject([]) do |res, n|
    dlong = denormalize get_random_radiant(range)
    dlat  = denormalize get_random_radiant(range)

    point = GeoMagic::Point.new @center.latitude + dlat, @center.longitude + dlong
    res << point
    res
  end
end

#denormalize(n) ⇒ Object



96
97
98
# File 'lib/geo_magic/radius.rb', line 96

def denormalize n
  n / NORMALIZER
end

#get_random_radiant(range) ⇒ Object



82
83
84
# File 'lib/geo_magic/radius.rb', line 82

def get_random_radiant range
  self.class.get_random_radiant range
end

#normalize(n) ⇒ Object



92
93
94
# File 'lib/geo_magic/radius.rb', line 92

def normalize n 
  (n * NORMALIZER).to_i
end