Class: PlotStatistics::Circle

Inherits:
Object
  • Object
show all
Defined in:
lib/plot_statistics/circle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Circle

Returns a new instance of Circle.



5
6
7
8
9
10
# File 'lib/plot_statistics/circle.rb', line 5

def initialize(params={})
  @clam = params[:clam]
  @radius = params[:radius].to_f
  @distance_from_bound = find_distance_from_bounds
  @proportion_inside_plot = estimate_proportion_inside_plot
end

Instance Attribute Details

#clamObject

Returns the value of attribute clam.



3
4
5
# File 'lib/plot_statistics/circle.rb', line 3

def clam
  @clam
end

#distance_from_boundObject

Returns the value of attribute distance_from_bound.



3
4
5
# File 'lib/plot_statistics/circle.rb', line 3

def distance_from_bound
  @distance_from_bound
end

#proportion_inside_plotObject

Returns the value of attribute proportion_inside_plot.



3
4
5
# File 'lib/plot_statistics/circle.rb', line 3

def proportion_inside_plot
  @proportion_inside_plot
end

#radiusObject

Returns the value of attribute radius.



3
4
5
# File 'lib/plot_statistics/circle.rb', line 3

def radius
  @radius
end

Instance Method Details

#corners_outside_circleObject



45
46
47
48
49
50
51
# File 'lib/plot_statistics/circle.rb', line 45

def corners_outside_circle
  ClamPlot::PLOT_CORNERS.map do |corner|
    distance = Math.sqrt((corner.first - clam.x) ** 2 + (corner.last - clam.y) ** 2)
    next if distance < radius
    distance
  end.compact
end

#estimate_proportion_inside_plotObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/plot_statistics/circle.rb', line 12

def estimate_proportion_inside_plot
  distance_from_bound.reject! {|distance| distance > radius}
  case distance_from_bound.size
  when 1
    proportion_for_one_side_out
  when 2
    proportion_for_two_sides_out
  else
    1.0
  end
end

#find_distance_from_boundsObject



36
37
38
39
40
41
42
43
# File 'lib/plot_statistics/circle.rb', line 36

def find_distance_from_bounds
  [
    100 - clam.y, # distance from top
    100 - clam.x, # distance from right
    clam.x,       # distance from left
    clam.y        # distance from bottom
  ]
end

#proportion_for_one_side_outObject



24
25
26
# File 'lib/plot_statistics/circle.rb', line 24

def proportion_for_one_side_out
  1 - Math.acos( distance_from_bound.pop / radius ) / Math::PI
end

#proportion_for_two_sides_outObject



28
29
30
31
32
33
34
# File 'lib/plot_statistics/circle.rb', line 28

def proportion_for_two_sides_out
  if corners_outside_circle.size == 3
    1 - ( Math.acos( distance_from_bound.pop / radius ) + Math.acos( distance_from_bound.pop / radius ) + Math::PI / 2 ) / (Math::PI * 2)
  else
    1 - ( 2 * Math.acos( distance_from_bound.pop / radius ) + 2 * Math.acos( distance_from_bound.pop / radius ) ) / (Math::PI * 2)
  end
end