cossincalc 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010 Molte Emil Strange Andersen
1
+ Copyright (c) 2010-2011 Molte Emil Strange Andersen
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
data/README.markdown ADDED
@@ -0,0 +1,112 @@
1
+ CosSinCalc
2
+ ==========
3
+
4
+ CosSinCalc is a [web application](http://cossincalc.com/) able to calculate the variables of a triangle and even draw it for you!
5
+
6
+ This is an offline version of the calculator available as a Ruby gem.
7
+ You can use the included command line utility to generate a PDF page containing all the results, formulae and a drawing of the triangle, *or* you can include it as a library in your Ruby application and use just the features you care about.
8
+
9
+ The main features are:
10
+
11
+ * Calculating the missing variables of the triangle
12
+ * Calculating additional variables (ie. altitudes, medians, angle bisectors, area, circumference)
13
+ * Generating an SVG or PNG (via [ImageMagick](http://www.imagemagick.org/)) drawing of the triangle
14
+ * Generating a LaTeX or PDF (via `pdflatex`) document containing the calculations, the steps performed and a drawing.
15
+
16
+ Usage
17
+ -----
18
+
19
+ For instructions on how to use the command line utility, please run `cossincalc --help` after installation.
20
+
21
+ Basic usage through an `irb` console:
22
+
23
+ >> require 'rubygems'
24
+ => true
25
+ >> require 'cossincalc'
26
+ => true
27
+
28
+ # Pass the known side and angle values to the initialization method as two
29
+ # hashes. The valid keys (variable names) are :a, :b and :c.
30
+ # It is important that you provide the values as strings as they wouldn't
31
+ # be parsed and converted properly, otherwise.
32
+ >> triangle = CosSinCalc::Triangle.new({ :a => "3.0", :c => "5" }, { :c => "90" })
33
+ => #<CosSinCalc::Triangle:0x25058e8>
34
+
35
+ >> triangle.calculate!
36
+ => true
37
+
38
+ # Fetch angle A, but in radians and unrounded.
39
+ >> triangle.angle(:a)
40
+ => 0.643501108793284
41
+
42
+ # Fetch angle A in degrees and rounded.
43
+ >> triangle.humanize.angle(:a)
44
+ => "36.87"
45
+
46
+ # Change the output precision.
47
+ >> triangle.humanize(3).angle(:a)
48
+ => "36.870"
49
+
50
+ # Fetch another variable (side b).
51
+ >> triangle.humanize.side(:b)
52
+ => "4.00"
53
+
54
+ # There are many available variables:
55
+ # altitude(:c), median(:a), angle_bisector(:a), area(), circumference()
56
+
57
+ ### Change angle unit ###
58
+ The default unit used for angles is degrees, however both radians and gon may be used.
59
+
60
+ >> triangle = CosSinCalc::Triangle.new({ :a => "3.0", :c => "5" }, { :c => (Math::PI/2).to_s, :unit => :radian })
61
+ => #<CosSinCalc::Triangle:0x25058e8>
62
+
63
+ >> triangle = CosSinCalc::Triangle.new({ :a => "3.0", :c => "5" }, { :c => "100", :unit => :gon })
64
+ => #<CosSinCalc::Triangle:0x25058e8>
65
+
66
+ Note that no unit is used for side values as it doesn't matter to the calculation.
67
+
68
+ ### Generate an SVG or PNG drawing ###
69
+ >> triangle = ...; triangle.calculate!
70
+ => true
71
+
72
+ # Save an SVG version of the drawing by initializing a new Drawing
73
+ # instance and passing the name of the file to be created to the save_svg
74
+ # method.
75
+ >> CosSinCalc::Triangle::Drawing.new(triangle.humanize).save_svg('my-vector-drawing')
76
+ => true
77
+
78
+ # Save a PNG version of the drawing by initializing a new Drawing
79
+ # instance and passing the name of the file to be created to the save_png
80
+ # method.
81
+ # In fact, an SVG file is created first and then converted to a PNG image
82
+ # by ImageMagick. Make sure to have ImageMagick installed if you want to
83
+ # use this feature.
84
+ >> CosSinCalc::Triangle::Drawing.new(triangle.humanize).save_png('my-png-drawing')
85
+ => true
86
+
87
+ ### Generate a PDF document ###
88
+ >> triangle = ...; triangle.calculate!
89
+ => true
90
+
91
+ # Pass the wanted filename to the save_pdf method of the Latex instance.
92
+ # Make sure to have a LaTeX distribution installed (as well as the
93
+ # amsmath, amsfonts and graphicx packages) if you want to make use
94
+ # of this feature.
95
+ >> CosSinCalc::Triangle::Formatter::Latex.new(triangle.humanize).save_pdf('my-result-document')
96
+ => true
97
+
98
+ Installation
99
+ ------------
100
+
101
+ To install it as a Ruby gem, please run
102
+
103
+ gem install cossincalc
104
+
105
+ In order for this to work, you must have [Ruby](http://www.ruby-lang.org/) and [RubyGems](http://rubygems.org) installed.
106
+
107
+ - - -
108
+
109
+ Feature requests, ideas, questions etc. is recieved at <http://getsatisfaction.com/cossincalc>.
110
+ Bugs should be submitted to the [issue tracker](http://github.com/molte/cossincalc_offline/issues).
111
+
112
+ Copyright (c) 2010-2011 Molte Emil Strange Andersen, released under the MIT license.
@@ -29,8 +29,9 @@ module CosSinCalc
29
29
  end
30
30
 
31
31
  private
32
- def each(*args, &block)
33
- @triangle.each(*args, &block)
32
+ # Returns the first variable in the given array and the rest of the variables separately.
33
+ def first(vars)
34
+ return vars.first, rest(vars.first)
34
35
  end
35
36
 
36
37
  def sq(value)
@@ -39,73 +40,54 @@ module CosSinCalc
39
40
 
40
41
  # Calculates all three angles when all three sides are known.
41
42
  def calculate_three_angles
42
- each do |v, r|
43
- unless angle(v)
44
- angle[v] = calculate_angle_by_sides(v, r)
45
- equation('@1=\arccos\left(\frac{$2^2+$3^2-$1^2}{2 * $2 * $3}\right)', v, *r)
46
- end
43
+ each(angles.unknown) do |v, r|
44
+ angle[v] = calculate_angle_by_sides(v, r)
45
+ equation('@1=\arccos\left(\frac{$2^2+$3^2-$1^2}{2 * $2 * $3}\right)', v, *r)
47
46
  end
48
47
  end
49
48
 
50
49
  # Calculates two unknown angles when two sides and one angle are known.
51
50
  def calculate_two_angles
52
- each do |v, r|
53
- if angle(v)
54
- unless side(v)
55
- side[v] = sqrt sq(sides(r)).inject(&:+) -
56
- 2 * sides(r).inject(&:*) * cos(angle(v))
57
- equation('$1=\sqrt{$2^2+$3^2-2 * $2 * $3 * \cos(@1)}', v, *r)
58
- calculate_three_angles
59
- break
60
- end
61
-
62
- each(r) do |v2|
63
- if side(v2)
64
- angle[v2] = asin sin(angle(v)) * side(v2) / side(v)
65
- equation('@2=\arcsin\left(\frac{\sin(@1) * $2}{$1}\right)', v, v2)
66
-
67
- if ambiguous_case?(v, v2)
68
- @alt = CosSinCalc::Triangle.new(sides, angles, self)
69
- @alt.angle[v2] = PI - angle(v2)
70
- @alt.equation('@2=@pi-\arcsin\left(\frac{\sin(@1) * $2}{$1}\right)', v, v2)
71
- @alt.calculate_side_and_angle
72
- end
73
-
74
- calculate_two_sides
75
- break
76
- end
77
- end
78
- break
79
- end
51
+ v, r = first(angles.known)
52
+
53
+ unless side(v)
54
+ side[v] = sqrt sq(sides(r)).inject(&:+) -
55
+ 2 * sides(r).inject(&:*) * cos(angle(v))
56
+ equation('$1=\sqrt{$2^2+$3^2-2 * $2 * $3 * \cos(@1)}', v, *r)
57
+ calculate_three_angles
58
+ return
59
+ end
60
+
61
+ v2, r2 = first sides.known(r)
62
+ angle[v2] = asin sin(angle(v)) * side(v2) / side(v)
63
+ equation('@2=\arcsin\left(\frac{\sin(@1) * $2}{$1}\right)', v, v2)
64
+
65
+ if ambiguous_case?(v, v2)
66
+ @alt = CosSinCalc::Triangle.new(sides, angles, self)
67
+ @alt.angle[v2] = PI - angle(v2)
68
+ @alt.equation('@2=@pi-\arcsin\left(\frac{\sin(@1) * $2}{$1}\right)', v, v2)
69
+ @alt.calculate_side_and_angle
80
70
  end
71
+
72
+ calculate_two_sides
81
73
  end
82
74
 
83
75
  # Calculates up to two unknown sides when at least one side and two angles are known.
84
76
  def calculate_two_sides
85
77
  calculate_last_angle
86
-
87
- each do |v, r|
88
- if side(v)
89
- each(r) do |v2|
90
- unless side(v2)
91
- side[v2] = sin(angle(v2)) * side(v) / sin(angle(v))
92
- equation('$2=\frac{\sin(@2) * $1}{\sin(@1)}', v, v2)
93
- end
94
- end
95
- break
96
- end
78
+ v, r = first(sides.known)
79
+
80
+ each sides.unknown(r) do |v2|
81
+ side[v2] = sin(angle(v2)) * side(v) / sin(angle(v))
82
+ equation('$2=\frac{\sin(@2) * $1}{\sin(@1)}', v, v2)
97
83
  end
98
84
  end
99
85
 
100
86
  # Calculates the last unknown angle.
101
87
  def calculate_last_angle
102
- each do |v, r|
103
- unless angle(v)
104
- angle[v] = PI - angles(r).inject(&:+)
105
- equation('@1=@pi-@2-@3', v, *r)
106
- break
107
- end
108
- end
88
+ v, r = first(angles.unknown)
89
+ angle[v] = PI - angles(r).inject(&:+)
90
+ equation('@1=@pi-@2-@3', v, *r)
109
91
  end
110
92
 
111
93
  # Calculates and returns whether the triangle has multiple solutions.
@@ -19,6 +19,16 @@ module CosSinCalc
19
19
  def amount
20
20
  self.values.compact.size
21
21
  end
22
+
23
+ # Returns an array of variables whose values are known.
24
+ def known(vars = CosSinCalc::Triangle::VARIABLES)
25
+ vars.reject { |v| !self[v] }
26
+ end
27
+
28
+ # Returns an array of variables whose values are unknown.
29
+ def unknown(vars = CosSinCalc::Triangle::VARIABLES)
30
+ vars.reject { |v| self[v] }
31
+ end
22
32
  end
23
33
  end
24
34
  end
@@ -54,8 +54,8 @@ module CosSinCalc
54
54
  # Returns the length of the line which starts at the corner and is perpendicular with the opposite side.
55
55
  def altitude(v)
56
56
  require_calculation
57
- r = rest(v)
58
- Math.sin(angle(r[0])) * side(r[1])
57
+ r1, r2 = *rest(v)
58
+ Math.sin(angle(r1)) * side(r2)
59
59
  end
60
60
 
61
61
  # Returns the length of the line going from the corner to the middle of the opposite side.
@@ -67,14 +67,15 @@ module CosSinCalc
67
67
  # Returns the length of the line between a corner and the opposite side which bisects the angle at the corner.
68
68
  def angle_bisector(v)
69
69
  require_calculation
70
- r = rest(v)
71
- Math.sin(angle(r[0])) * side(r[1]) / Math.sin(angle(r[1]) + angle(v) / 2)
70
+ r1, r2 = *rest(v)
71
+ Math.sin(angle(r1)) * side(r2) / Math.sin(angle(r2) + angle(v) / 2)
72
72
  end
73
73
 
74
74
  # Returns the area of the triangle.
75
75
  def area
76
76
  require_calculation
77
- side(VARIABLES[0]) * side(VARIABLES[1]) * Math.sin(angle(VARIABLES[2])) / 2
77
+ v1, v2, v3 = *VARIABLES
78
+ side(v1) * side(v2) * Math.sin(angle(v3)) / 2
78
79
  end
79
80
 
80
81
  # Returns the circumference of the triangle.
@@ -1,3 +1,3 @@
1
1
  module CosSinCalc
2
- VERSION = "1.0.6"
2
+ VERSION = "1.0.7"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cossincalc
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease: false
4
+ hash: 25
5
+ prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 6
10
- version: 1.0.6
9
+ - 7
10
+ version: 1.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Molte Emil Strange Andersen
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-14 00:00:00 +01:00
18
+ date: 2011-04-20 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -32,6 +32,7 @@ extensions: []
32
32
  extra_rdoc_files: []
33
33
 
34
34
  files:
35
+ - README.markdown
35
36
  - MIT-LICENSE
36
37
  - Rakefile
37
38
  - lib/core_ext.rb
@@ -77,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
78
  requirements: []
78
79
 
79
80
  rubyforge_project:
80
- rubygems_version: 1.3.7
81
+ rubygems_version: 1.4.1
81
82
  signing_key:
82
83
  specification_version: 3
83
84
  summary: Triangle calculator