Class: Triangular::Vertex

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/triangular/vertex.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Vertex

Returns a new instance of Vertex.



16
17
18
19
20
21
22
23
24
# File 'lib/triangular/vertex.rb', line 16

def initialize(*args)
  if args.length == 1 && args.first.is_a?(Point)
    @point = args.first
  elsif args.length == 3
    @point = Point.new(args[0], args[1], args[2])
  else
    raise 'You must either supply the XYZ coordinates or a Point object to create a Vertex'
  end
end

Instance Attribute Details

#pointObject

Returns the value of attribute point.



14
15
16
# File 'lib/triangular/vertex.rb', line 14

def point
  @point
end

Class Method Details

.parse(string) ⇒ Object



36
37
38
39
40
# File 'lib/triangular/vertex.rb', line 36

def self.parse(string)
  match_data = string.strip.match(pattern)

  new(Point.parse(match_data[:point]))
end

.patternObject



42
43
44
# File 'lib/triangular/vertex.rb', line 42

def self.pattern
  /vertex\s+ (?<point>#{Point.pattern})/x
end

Instance Method Details

#==(other) ⇒ Object



30
31
32
33
34
# File 'lib/triangular/vertex.rb', line 30

def ==(other)
  return false unless other.is_a?(Vertex)

  x == other.x && y == other.y && z == other.z
end

#to_sObject



26
27
28
# File 'lib/triangular/vertex.rb', line 26

def to_s
  "vertex #{@point}"
end