Class: Geo3d::Triangle

Inherits:
Object
  • Object
show all
Defined in:
lib/geo3d/triangle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Triangle

Returns a new instance of Triangle.


9
10
11
12
13
# File 'lib/geo3d/triangle.rb', line 9

def initialize *args
  @a = args.size > 0 ? args[0] : Vector.new
  @b = args.size > 1 ? args[1] : Vector.new
  @c = args.size > 2 ? args[2] : Vector.new
end

Instance Attribute Details

#aObject

Returns the value of attribute a.


3
4
5
# File 'lib/geo3d/triangle.rb', line 3

def a
  @a
end

#bObject

Returns the value of attribute b.


3
4
5
# File 'lib/geo3d/triangle.rb', line 3

def b
  @b
end

#cObject

Returns the value of attribute c.


3
4
5
# File 'lib/geo3d/triangle.rb', line 3

def c
  @c
end

Instance Method Details

#clockwise?(reference_normal = Vector.new(0,0,-1)) ⇒ Boolean Also known as: cw?

Returns:

  • (Boolean)

38
39
40
# File 'lib/geo3d/triangle.rb', line 38

def clockwise? reference_normal = Vector.new(0,0,-1)
  signed_area( reference_normal ) > 0
end

#counter_clockwise?(reference_normal = Vector.new(0,0,-1)) ⇒ Boolean Also known as: ccw?

Returns:

  • (Boolean)

42
43
44
# File 'lib/geo3d/triangle.rb', line 42

def counter_clockwise? reference_normal = Vector.new(0,0,-1)
  signed_area( reference_normal ) < 0
end

#flipObject


19
20
21
22
23
# File 'lib/geo3d/triangle.rb', line 19

def flip
  f = clone
  f.flip!
  f
end

#flip!Object


15
16
17
# File 'lib/geo3d/triangle.rb', line 15

def flip!
  @b, @c = @c, @b
end

#normalObject


25
26
27
# File 'lib/geo3d/triangle.rb', line 25

def normal
  (b - a).cross(c - a).normalize
end

#pointsObject


5
6
7
# File 'lib/geo3d/triangle.rb', line 5

def points
  [a, b, c]
end

#signed_area(reference_normal = Vector.new(0,0,-1)) ⇒ Object


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

def signed_area reference_normal = Vector.new(0,0,-1)
  sum = Vector.new 0, 0, 0, 0
  points.each_with_index do |current_point, i|
    next_point = points[(i == points.size - 1) ? 0 : i+1]
    sum += current_point.cross next_point
  end
  reference_normal.dot(sum) / 2.0
end