Class: Geometry::Triangle
- Inherits:
-
Object
- Object
- Geometry::Triangle
- Defined in:
- lib/geometry-bgw.rb
Instance Attribute Summary collapse
-
#a ⇒ Object
Returns the value of attribute a.
-
#b ⇒ Object
Returns the value of attribute b.
-
#c ⇒ Object
Returns the value of attribute c.
Instance Method Summary collapse
- #angles ⇒ Object
- #area ⇒ Object
-
#initialize(a, b, c) ⇒ Triangle
constructor
A new instance of Triangle.
- #law_of_cosines(a, b, c) ⇒ Object
- #perimeter ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(a, b, c) ⇒ Triangle
Returns a new instance of Triangle.
7 8 9 10 11 |
# File 'lib/geometry-bgw.rb', line 7 def initialize(a,b,c) @a = a @b = b @c = c end |
Instance Attribute Details
#a ⇒ Object
Returns the value of attribute a.
5 6 7 |
# File 'lib/geometry-bgw.rb', line 5 def a @a end |
#b ⇒ Object
Returns the value of attribute b.
5 6 7 |
# File 'lib/geometry-bgw.rb', line 5 def b @b end |
#c ⇒ Object
Returns the value of attribute c.
5 6 7 |
# File 'lib/geometry-bgw.rb', line 5 def c @c end |
Instance Method Details
#angles ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/geometry-bgw.rb', line 40 def angles angles = [] def law_of_cosines(a, b, c) cos = ((a ** 2) + (b ** 2) - (c ** 2)).to_f / (2 * a * b).to_f (Math.acos(cos) / (Math::PI / 180)).round(2) end angles << law_of_cosines(@b, @c, @a) angles << law_of_cosines(@c, @a, @b) angles << law_of_cosines(@a, @b, @c) return angles end |
#area ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/geometry-bgw.rb', line 29 def area if valid? semi_perimeter = self.perimeter.to_f / 2 num_to_sqrt = semi_perimeter * (semi_perimeter - @a) * (semi_perimeter - @b) * (semi_perimeter - @c) area = Math.sqrt(num_to_sqrt) return area.round(2) else puts "Not a valid triangle" end end |
#law_of_cosines(a, b, c) ⇒ Object
43 44 45 46 |
# File 'lib/geometry-bgw.rb', line 43 def law_of_cosines(a, b, c) cos = ((a ** 2) + (b ** 2) - (c ** 2)).to_f / (2 * a * b).to_f (Math.acos(cos) / (Math::PI / 180)).round(2) end |
#perimeter ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/geometry-bgw.rb', line 21 def perimeter if valid? @a + @b + @c else puts "Not a valid triangle" end end |
#valid? ⇒ Boolean
13 14 15 16 17 18 19 |
# File 'lib/geometry-bgw.rb', line 13 def valid? if (@a + @b > @c) && (@b + @c > @a) && (@c + @a > @b) true else false end end |