Class: CosSinCalc::Triangle

Inherits:
Object
  • Object
show all
Includes:
Calculator
Defined in:
lib/cossincalc/triangle.rb,
lib/cossincalc/triangle/drawing.rb,
lib/cossincalc/triangle/formatter.rb,
lib/cossincalc/triangle/validator.rb,
lib/cossincalc/triangle/calculator.rb,
lib/cossincalc/triangle/drawing/svg.rb,
lib/cossincalc/triangle/variable_hash.rb,
lib/cossincalc/triangle/formatter/latex.rb

Defined Under Namespace

Modules: Calculator Classes: Drawing, Formatter, Validator, VariableHash

Constant Summary collapse

VARIABLES =
[:a, :b, :c]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Calculator

#calculate_angle_by_sides, #calculate_side_and_angle, #calculate_variables, #equation

Constructor Details

#initialize(given_sides, given_angles, alternative = nil) ⇒ Triangle

Initializes a triangle object with the given sides and angles and an optional reference to an alternative triangle.

The sides and angles may be given either as a VariableHash object or an ordinary hash. The angle unit may be specified inside the given_angles hash (using the key :unit and value either :degree, :gon or :radian). If no angle unit is given it defaults to :degree. If a hash is used then value parsing and conversion will only occur if the values are provided as strings. Float angle values are expected to be radians no matter the given angle unit.



17
18
19
20
21
22
23
24
25
26
# File 'lib/cossincalc/triangle.rb', line 17

def initialize(given_sides, given_angles, alternative = nil)
  initialize_variables
  
  given_sides.each { |v, value| side[v] = Formatter.parse(value) }
  
  angles.unit = (given_angles.respond_to?(:unit) ? given_angles.unit : given_angles.delete(:unit)) || :degree
  given_angles.each { |v, value| angle[v] = Formatter.parse_angle(value, angles.unit) }
  
  @alt = alternative
end

Instance Attribute Details

#altObject (readonly)

Reference to alternative triangle at ambiguous case.



7
8
9
# File 'lib/cossincalc/triangle.rb', line 7

def alt
  @alt
end

#equationsObject (readonly)

Steps performed to calculate the result.



8
9
10
# File 'lib/cossincalc/triangle.rb', line 8

def equations
  @equations
end

Instance Method Details

#acute?(value) ⇒ Boolean

Returns whether the given value is acute or not.

Returns:

  • (Boolean)


104
105
106
# File 'lib/cossincalc/triangle.rb', line 104

def acute?(value)
  value < Math::PI / 2
end

#altitude(v) ⇒ Object

Returns the length of the line which starts at the corner and is perpendicular with the opposite side.



55
56
57
58
59
# File 'lib/cossincalc/triangle.rb', line 55

def altitude(v)
  require_calculation
  r1, r2 = *rest(v)
  Math.sin(angle(r1)) * side(r2)
end

#angle(v = nil) ⇒ Object Also known as: angles



44
45
46
# File 'lib/cossincalc/triangle.rb', line 44

def angle(v = nil)
  v.nil? ? @angles : @angles[v]
end

#angle_bisector(v) ⇒ Object

Returns the length of the line between a corner and the opposite side which bisects the angle at the corner.



68
69
70
71
72
# File 'lib/cossincalc/triangle.rb', line 68

def angle_bisector(v)
  require_calculation
  r1, r2 = *rest(v)
  Math.sin(angle(r1)) * side(r2) / Math.sin(angle(r2) + angle(v) / 2)
end

#areaObject

Returns the area of the triangle.



75
76
77
78
79
# File 'lib/cossincalc/triangle.rb', line 75

def area
  require_calculation
  v1, v2, v3 = *VARIABLES
  side(v1) * side(v2) * Math.sin(angle(v3)) / 2
end

#calculate!Object

Calculates the unknown variables in the triangle.



29
30
31
32
33
34
35
36
37
38
# File 'lib/cossincalc/triangle.rb', line 29

def calculate!
  Validator.new(self).validate
  calculate_variables
  Validator.new(self).validate_calculation
  @calculated = true
rescue Errno::EDOM
  Validator::ValidationError.new([Validator::INVALID_TRIANGLE])
rescue Validator::ValidationError => exception
  exception
end

#calculated?Boolean

Returns whether the missing values have been successfully calculated.

Returns:

  • (Boolean)


99
100
101
# File 'lib/cossincalc/triangle.rb', line 99

def calculated?
  @calculated
end

#circumferenceObject

Returns the circumference of the triangle.



82
83
84
85
# File 'lib/cossincalc/triangle.rb', line 82

def circumference
  require_calculation
  sides(VARIABLES).inject(&:+)
end

#each(array = VARIABLES, &block) ⇒ Object

Executes the given block for each variable symbol. If an array of variable names is given, only those variables will be iterated through.



89
90
91
# File 'lib/cossincalc/triangle.rb', line 89

def each(array = VARIABLES, &block)
  array.each { |v| block.arity == 2 ? yield(v, rest(v)) : yield(v) }
end

#humanize(precision = 2) ⇒ Object



40
41
42
# File 'lib/cossincalc/triangle.rb', line 40

def humanize(precision = 2)
  Formatter.new(self, precision)
end

#median(v) ⇒ Object

Returns the length of the line going from the corner to the middle of the opposite side.



62
63
64
65
# File 'lib/cossincalc/triangle.rb', line 62

def median(v)
  require_calculation
  Math.sqrt((2 * sq(sides(rest(v))).inject(&:+) - sq(side(v))) / 4)
end

#obtuse?(value) ⇒ Boolean

Returns whether the given value is obtuse or not.

Returns:

  • (Boolean)


109
110
111
# File 'lib/cossincalc/triangle.rb', line 109

def obtuse?(value)
  value > Math::PI / 2
end

#rest(*vars) ⇒ Object

Returns all the variable symbols except those given.



94
95
96
# File 'lib/cossincalc/triangle.rb', line 94

def rest(*vars)
  VARIABLES - vars
end

#side(v = nil) ⇒ Object Also known as: sides



49
50
51
# File 'lib/cossincalc/triangle.rb', line 49

def side(v = nil)
  v.nil? ? @sides : @sides[v]
end