Class: SolidRuby::Helpers::Triangle

Inherits:
Primitives::Polygon show all
Defined in:
lib/solidruby/helpers/triangle.rb

Overview

Helper class for creating triangles, given three inputs (at least 1 side + angles) The triangle can then either be used as a Polygon, or it can be thrown away and used just for calcuation of the triangle order is assumed as follows: Angles go clockwise with @alpha at the origin Sides go clockwise with @a along the X axis, and @c along Y

Instance Attribute Summary collapse

Attributes inherited from SolidRubyObject

#attributes, #children, #siblings, #transformations

Instance Method Summary collapse

Methods inherited from Primitives::Polygon

#to_rubyscad

Methods inherited from SolidRubyObject

#&, alias_attr, #debug, #debug?, #mirror, #place, #rotate, #rotate_around, #save, #scale, #to_rubyscad, #translate, #union, #walk_tree, #walk_tree_classes

Constructor Details

#initialize(args = {}) ⇒ Triangle

Returns a new instance of Triangle.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/solidruby/helpers/triangle.rb', line 27

def initialize(args = {})
  use_alt_solution = args[:alt_solution] || false

  if args.reject{|k| k == :alt_solution}.count != 3
    raise "Triangle requires exactly 3 inputs"
  elsif args[:a].nil? && args[:b].nil? && args[:c].nil?
    raise "Triangle requires at least 1 side length"
  end

  #try to solve twice to see if we have two solutions
  sol_count = 0
  solution = solve(args, false)
  sol_count += 1 unless solution.nil?
  solution_alt = solve(args, true)
  sol_count += 1 unless solution_alt.nil?

  raise "Could not solve triangle." if sol_count == 0

  @has_alt_solution = sol_count == 2 ? true : false

  if use_alt_solution
    update(solution_alt)
  else
    update(solution)
  end

  #consturct triangle polygon
  args = {points: [
    [0.0, 0.0],
    [@b, 0.0],
    [@c * Math.cos(radians(@alpha)), height(:beta)],
    [0.0, 0.0]]}

  super(args)
end

Instance Attribute Details

#aObject

Returns the value of attribute a.



26
27
28
# File 'lib/solidruby/helpers/triangle.rb', line 26

def a
  @a
end

#alphaObject

Returns the value of attribute alpha.



26
27
28
# File 'lib/solidruby/helpers/triangle.rb', line 26

def alpha
  @alpha
end

#bObject

Returns the value of attribute b.



26
27
28
# File 'lib/solidruby/helpers/triangle.rb', line 26

def b
  @b
end

#betaObject

Returns the value of attribute beta.



26
27
28
# File 'lib/solidruby/helpers/triangle.rb', line 26

def beta
  @beta
end

#cObject

Returns the value of attribute c.



26
27
28
# File 'lib/solidruby/helpers/triangle.rb', line 26

def c
  @c
end

#gammaObject

Returns the value of attribute gamma.



26
27
28
# File 'lib/solidruby/helpers/triangle.rb', line 26

def gamma
  @gamma
end

#has_alt_solutionObject

Returns the value of attribute has_alt_solution.



26
27
28
# File 'lib/solidruby/helpers/triangle.rb', line 26

def has_alt_solution
  @has_alt_solution
end

Instance Method Details

#height(angle) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/solidruby/helpers/triangle.rb', line 63

def height(angle)
  case angle
  when :alpha
    ang = @beta
    side = @c
  when :beta
    ang = @alpha
    side = @c
  else
    ang = @alpha
    side = @b
  end
  Math.sin(radians(ang)) * side.to_f
end