Class: Math::Geometry::Triangle

Inherits:
PlaneFigure show all
Defined in:
lib/ruuuby/math/geometry/shape/triangle.rb

Overview

Instance Attribute Summary collapse

Attributes inherited from PlaneFigure

#num_sides

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PlaneFigure

#sum_of_interior_angles

Constructor Details

#initialize(side_a, side_b, side_c) ⇒ Triangle

angle_A is opposite of side_a angle_B is opposite of side_b angle_C is opposite of side_c

law of sines: ‘a/sin(A)` = `b/sin(B)` = `c/sin©`



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 97

def initialize(side_a, side_b, side_c)
  @side_a    = side_a
  @side_b    = side_b
  @side_c    = side_c
  @num_sides = 3
  if @side_a == @side_b && @side_a == @side_c
    @longest_val = @side_a
    @angle_a         = ::ThetaAngle.new_degree(60)
    @angle_b         = ::ThetaAngle.new_degree(60)
    @angle_c         = ::ThetaAngle.new_degree(60)
  else
    @longest_val = [@side_a, @side_b, @side_c].max
    @angle_a         = ::ThetaAngle.new_radian(::Math.acos(( ((@side_b ** 2) + (@side_c ** 2) - (@side_a ** 2)) / (2.0 * @side_b * @side_c) )))
    @angle_b         = ::ThetaAngle.new_radian(::Math.acos(( ((@side_c ** 2) + (@side_a ** 2) - (@side_b ** 2)) / (2.0 * @side_c * @side_a) )))
    #@angle_c        = θʳ(::Math.acos(( ((@side_a ** 2) + (@side_b ** 2) + (@side_c ** 2)) / (2.0 * @side_a * @side_b) )))
    @angle_c         = ::ThetaAngle.new_degree(180.0) - (@angle_a + @angle_b)
    #@angle_c        = θʳ(::Math.acos(((@side_a ** 2 + @side_b ** 2 + @side_c ** 2) / (2.0 * @side_a * @side_b))))
  end
  super()
end

Instance Attribute Details

#angle_aObject

Returns the value of attribute angle_a.



14
15
16
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 14

def angle_a
  @angle_a
end

#angle_bObject

Returns the value of attribute angle_b.



14
15
16
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 14

def angle_b
  @angle_b
end

#angle_cObject

Returns the value of attribute angle_c.



14
15
16
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 14

def angle_c
  @angle_c
end

#side_aObject (readonly)

Returns the value of attribute side_a.



15
16
17
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 15

def side_a
  @side_a
end

#side_bObject (readonly)

Returns the value of attribute side_b.



15
16
17
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 15

def side_b
  @side_b
end

#side_cObject (readonly)

Returns the value of attribute side_c.



15
16
17
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 15

def side_c
  @side_c
end

Class Method Details

.new_equilateral(side_length) ⇒ Math::Geometry::Triangle



18
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 18

def self.new_equilateral(side_length); ::Math::Geometry::Triangle.new(side_length, side_length, side_length); end

Instance Method Details

#acute?Boolean

Returns ‣ if ‘a² + b² > c²`, then the triangle is acute.

Returns:

  • (Boolean)

    ‣ if ‘a² + b² > c²`, then the triangle is acute



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 45

def acute?
  a²_b² = (@side_a ** 2) + (@side_c ** 2)
      = @side_c ** 2
  if a²_b² < 
    true
  else
    if @angle_a.° + @angle_b.° + @angle_c.° == 180.0
      return @angle_a.⦟? && @angle_b.⦟? && @angle_c.⦟?
    else
      return false
    end
  end
end

#areaObject



134
135
136
137
138
139
140
141
142
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 134

def area
  if self.equiangular?
    (::Math.sqrt(3.0) / 4.0) * (@side_a ** 2)
  elsif self.scalene?
    (@side_a + @side_b + @side_c) / 2.0
  else
    1337
  end
end

#equiangular?Boolean

Returns true, if this triangle has 3 sides equal in length.

Returns:

  • (Boolean)

    true, if this triangle has 3 sides equal in length



36
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 36

def equiangular?; @angle_a == @angle_b && @angle_b == @angle_c; end

#equilateral?Boolean

Returns true, if this triangle has 3 sides equal in length.

Returns:

  • (Boolean)

    true, if this triangle has 3 sides equal in length



33
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 33

def equilateral?; @side_a == @side_b && @side_b == @side_c; end

#golden?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 20

def golden?
  if self.isosceles?
    if @side_a == @side_b
      @side_a / @side_c == ::Float::RATIO_GOLDEN
    else
      @side_a / @side_b == ::Float::RATIO_GOLDEN
    end
  else
    false
  end
end

#heightObject



126
127
128
129
130
131
132
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 126

def height
  if self.scalene?
    # either
    #@side_b * ::Math.asin(@angle_c)
    @side_c * ::Math.sin(@angle_b)
  end
end

#isosceles?Boolean

Returns true, if this triangle has 2 sides equal in length.

Returns:

  • (Boolean)

    true, if this triangle has 2 sides equal in length



39
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 39

def isosceles?; !self.equilateral? && (@side_a == @side_b || @side_b == @side_c || @side_a == @side_c); end

#obtuse?Boolean

Returns ‣ if ‘a² + b² < c²`, then the triangle is obtuse.

Returns:

  • (Boolean)

    ‣ if ‘a² + b² < c²`, then the triangle is obtuse



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 60

def obtuse?
  a²_b² = (@side_a ** 2) + (@side_b ** 2)
      = @side_c ** 2
  if a²_b² > 
    true
  else
    if @angle_a.° + @angle_b.° + @angle_c.° == 180.0
      return @angle_a.⦦? || @angle_b.⦦? || @angle_c.⦦?
    else
      return false
    end
  end
end

#perimeterObject



118
119
120
121
122
123
124
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 118

def perimeter
  if self.equilateral?
    @longest_val * 3
  else
    @side_a + @side_b + @side_c
  end
end

#scalene?Boolean

Returns true, if this triangle has no sides equal in length.

Returns:

  • (Boolean)

    true, if this triangle has no sides equal in length



42
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 42

def scalene?; @side_a != @side_b && @side_b != @side_c && @side_a != @side_c; end

#◣?Boolean

Returns ‣ if ‘a² + b² = c²`, then the triangle is right.

Returns:

  • (Boolean)

    ‣ if ‘a² + b² = c²`, then the triangle is right



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ruuuby/math/geometry/shape/triangle.rb', line 75

def ◣?
  a²_b² = (@side_a ** 2) + (@side_c ** 2)
      = @side_c ** 2
  if a²_b² == 
    true
  else
    angle_a = @angle_a.°
    angle_b = @angle_b.°
    angle_c = @angle_c.°
    if angle_a + angle_b + angle_c == 180.0
      return (angle_a == 90.0 || angle_b == 90.0 || angle_c == 90.0)
    else
      return false
    end
  end
end