class Geometry::Triangle

Attributes

a[RW]
b[RW]
c[RW]

Public Class Methods

new(a,b,c) click to toggle source
# File lib/geometry-bgw.rb, line 7
def initialize(a,b,c)
  @a = a
  @b = b
  @c = c
end

Public Instance Methods

angles() click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/geometry-bgw.rb, line 21
def perimeter
  if valid?
    @a + @b + @c
  else
    puts "Not a valid triangle"
  end
end
valid?() click to toggle source
# File lib/geometry-bgw.rb, line 13
def valid?
  if (@a + @b > @c) && (@b + @c > @a) && (@c + @a > @b)
    true
  else
    false
  end
end