class Geometry::Triangle

Attributes

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

Public Class Methods

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

Public Instance Methods

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