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.



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

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.



12
13
14
# File 'lib/triangular/vertex.rb', line 12

def point
  @point
end

Class Method Details

.parse(string) ⇒ Object



33
34
35
36
37
38
# File 'lib/triangular/vertex.rb', line 33

def self.parse(string)
  string.strip!
  match_data = string.match(self.pattern)
  
  self.new(Point.parse(match_data[:point]))
end

.patternObject



40
41
42
# File 'lib/triangular/vertex.rb', line 40

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

Instance Method Details

#==(other) ⇒ Object



28
29
30
31
# File 'lib/triangular/vertex.rb', line 28

def ==(other)
  return false unless other.is_a?(Vertex)
  self.x == other.x && self.y == other.y && self.z == other.z
end

#to_sObject



24
25
26
# File 'lib/triangular/vertex.rb', line 24

def to_s
  "vertex #{@point.to_s}"
end