plot_statistics 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
@@ -1,71 +1,53 @@
1
1
  class PlotStatistics
2
2
  class Circle
3
- attr_accessor :clam, :radius, :sample_points, :proportion_inside_plot
4
-
5
- DEGREES = [15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345, 360]
6
- RADIANS_TO_DEGREES = 360.0 / (2.0 * Math::PI)
3
+ attr_accessor :clam, :radius, :distance_from_bound, :proportion_inside_plot
7
4
 
8
5
  def initialize(params={})
9
6
  @clam = params[:clam]
10
- @radius = params[:radius]
11
- @sample_points = { :out_of_bounds => 0.0, :in_bounds => 0.0 }
12
- @proportion_inside_plot = out_of_bounds? ? estimate_proportion_inside_plot : 1.0
13
- end
14
-
15
- def create_samples
16
- sample_circles = radius * 2
17
- sample_radius = 0.5
18
- sample_circles.times do
19
- DEGREES.each do |degrees|
20
- x = clam.x + sample_radius * Math.cos(degrees / RADIANS_TO_DEGREES)
21
- y = clam.y + sample_radius * Math.sin(degrees / RADIANS_TO_DEGREES)
22
-
23
- if point_out_of_bounds?(x, y)
24
- sample_points[:out_of_bounds] += 1
25
- else
26
- sample_points[:in_bounds] += 1
27
- end
28
- end
29
-
30
- sample_radius += 0.5
31
- end
7
+ @radius = params[:radius].to_f
8
+ @distance_from_bound = find_distance_from_bounds
9
+ @proportion_inside_plot = estimate_proportion_inside_plot
32
10
  end
33
11
 
34
12
  def estimate_proportion_inside_plot
35
- create_samples
36
- sample_points[:in_bounds] / (sample_points[:in_bounds] + sample_points[:out_of_bounds])
37
- end
38
-
39
- def point_out_of_bounds?(x, y)
40
- [x, y].each do |coordinate|
41
- return true unless coordinate > 0 && coordinate < 100
13
+ distance_from_bound.reject! {|distance| distance > radius}
14
+ case distance_from_bound.size
15
+ when 1
16
+ proportion_for_one_side_out
17
+ when 2
18
+ proportion_for_two_sides_out
19
+ else
20
+ 1.0
42
21
  end
43
- false
44
- end
45
-
46
- def out_of_bounds?
47
- right_out_of_bounds? ||
48
- left_out_of_bounds? ||
49
- top_out_of_bounds? ||
50
- bottom_out_of_bounds?
51
22
  end
52
23
 
53
- def right_out_of_bounds?
54
- clam.x + radius > 100
24
+ def proportion_for_one_side_out
25
+ 1 - Math.acos( distance_from_bound.pop / radius ) / Math::PI
55
26
  end
56
27
 
57
- def left_out_of_bounds?
58
- clam.x - radius < 0
28
+ def proportion_for_two_sides_out
29
+ if corners_outside_radius.size == 3
30
+ 1 - ( Math.acos( distance_from_bound.pop / radius ) + Math.acos( distance_from_bound.pop / radius ) + Math::PI / 2 ) / (Math::PI * 2)
31
+ else
32
+ 1 - ( 2 * Math.acos( distance_from_bound.pop / radius ) + 2 * Math.acos( distance_from_bound.pop / radius ) ) / (Math::PI * 2)
33
+ end
59
34
  end
60
35
 
61
- def top_out_of_bounds?
62
- clam.y + radius > 100
36
+ def find_distance_from_bounds
37
+ [
38
+ (clam.y - 100).abs, # distance from top
39
+ (clam.x - 100).abs, # distance from right
40
+ clam.x, # distance from left
41
+ clam.y # distance from bottom
42
+ ]
63
43
  end
64
44
 
65
- def bottom_out_of_bounds?
66
- clam.y - radius < 0
45
+ def corners_outside_radius
46
+ ClamPlot::PLOT_CORNERS.map do |corner|
47
+ distance = Math.sqrt((corner.first - clam.x) ** 2 + (corner.last - clam.y) ** 2)
48
+ next if distance < radius
49
+ distance
50
+ end.compact
67
51
  end
68
-
69
-
70
52
  end
71
53
  end
@@ -4,6 +4,7 @@ class PlotStatistics
4
4
 
5
5
  AREA_OF_PLOT = 10_000
6
6
  MAX_RADIUS = 50
7
+ PLOT_CORNERS = [[0,0], [0,100], [100,0], [100,100]]
7
8
 
8
9
  def initialize(clams)
9
10
  @clams = clams
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{plot_statistics}
8
- s.version = "0.2.1"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Asa Wilson"]
12
- s.date = %q{2010-03-16}
12
+ s.date = %q{2010-03-18}
13
13
  s.default_executable = %q{plot_statistics}
14
14
  s.description = %q{This is a gem to do a Ripley's K analysis}
15
15
  s.email = %q{acvwilson@gmail.com}
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- - 1
9
- version: 0.2.1
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Asa Wilson
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-16 00:00:00 -04:00
17
+ date: 2010-03-18 00:00:00 -04:00
18
18
  default_executable: plot_statistics
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency