Class: Geometry::Triangle
- Inherits:
-
Object
- Object
- Geometry::Triangle
- Defined in:
- lib/geometry-befr.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.
5 6 7 8 9 |
# File 'lib/geometry-befr.rb', line 5 def initialize(a,b,c) @a = a @b = b @c = c end |
Instance Attribute Details
#a ⇒ Object
Returns the value of attribute a.
3 4 5 |
# File 'lib/geometry-befr.rb', line 3 def a @a end |
#b ⇒ Object
Returns the value of attribute b.
3 4 5 |
# File 'lib/geometry-befr.rb', line 3 def b @b end |
#c ⇒ Object
Returns the value of attribute c.
3 4 5 |
# File 'lib/geometry-befr.rb', line 3 def c @c end |
Instance Method Details
#angles ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/geometry-befr.rb', line 38 def angles def law_of_cosines(a, b, c) cos = ((a ** 2) + (b ** 2) - (c ** 2)).to_f / (2 * a * b).to_f angle = Math.acos(cos) angle *= (180 / Math::PI) return angle.round(2) end angles = [] 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
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/geometry-befr.rb', line 27 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
39 40 41 42 43 44 |
# File 'lib/geometry-befr.rb', line 39 def law_of_cosines(a, b, c) cos = ((a ** 2) + (b ** 2) - (c ** 2)).to_f / (2 * a * b).to_f angle = Math.acos(cos) angle *= (180 / Math::PI) return angle.round(2) end |
#perimeter ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/geometry-befr.rb', line 19 def perimeter if valid? @a + @b + @c else puts "Not a valid triangle" end end |
#valid? ⇒ Boolean
11 12 13 14 15 16 17 |
# File 'lib/geometry-befr.rb', line 11 def valid? if (@a + @b > @c) && (@b + @c > @a) && (@c + @a > @b) true else false end end |