Sha256: 507f6448f7b88faffa7d60dbca5ca684e3442a95e74c3efee5ece612ec15e573

Contents?: true

Size: 997 Bytes

Versions: 1

Compression:

Stored size: 997 Bytes

Contents

require 'forwardable'

module Triangular
  class Vertex
    extend Forwardable
    
    def_delegator :@point, :x, :x
    def_delegator :@point, :y, :y
    def_delegator :@point, :z, :z
    
    attr_accessor :point
    
    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
    
    def to_s
      "vertex #{@point.to_s}"
    end
    
    def ==(other)
      return false unless other.is_a?(Vertex)
      self.x == other.x && self.y == other.y && self.z == other.z
    end
    
    def self.parse(string)
      string.strip!
      match_data = string.match(self.pattern)
      
      self.new(Point.parse(match_data[:point]))
    end
    
    def self.pattern
      /vertex\s+ (?<point>#{Point.pattern})/x
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
triangular-0.0.1 lib/triangular/vertex.rb