Class: Silicium::Geometry::Triangle

Inherits:
Figure
  • Object
show all
Defined in:
lib/geometry/figure.rb

Instance Method Summary collapse

Methods included from Silicium::Geometry

#brute_min, #clockwise, #counter_clockwise, #cut_by_eq, #distance_point_line2d, #distance_point_line_normalized2d, #distance_point_to_point2d, #divide_min, #insert_eq, #minimal_convex_hull_2d, #needed_variables_order?, #not_polygon?, #oriented_area, #process_cf, #process_free_member, #process_line_by_coordinates, #put_point_in_part, #sign, #vector_length, #vectors_product

Constructor Details

#initialize(p1, p2, p3) ⇒ Triangle

Returns a new instance of Triangle.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/geometry/figure.rb', line 10

def initialize(p1, p2, p3)
  s_p1p2 = distance_point_to_point2d(p1, p2)
  s_p1p3 = distance_point_to_point2d(p1, p3)
  s_p2p3 = distance_point_to_point2d(p2, p3)
  if s_p1p2 + s_p2p3 <= s_p1p3 || s_p1p2 + s_p1p3 <= s_p2p3 || s_p2p3 + s_p1p3 <= s_p1p2
    raise ArgumentError, 'Triangle does not exist'
  else
    @side_p1p2 = s_p1p2
    @side_p1p3 = s_p1p3
    @side_p2p3 = s_p2p3
  end
end

Instance Method Details

#areaObject



27
28
29
30
# File 'lib/geometry/figure.rb', line 27

def area
  half_perimeter = perimeter / 2.0
  Math.sqrt(half_perimeter * (half_perimeter - @side_p1p2) * (half_perimeter - @side_p2p3) * (half_perimeter - @side_p1p3))
end

#perimeterObject



23
24
25
# File 'lib/geometry/figure.rb', line 23

def perimeter
  @side_p1p2 + @side_p1p3 + @side_p2p3
end