Class: Laser::Cutter::Geometry::Tuple

Inherits:
Object
  • Object
show all
Defined in:
lib/laser-cutter/geometry/tuple.rb

Direct Known Subclasses

Dimensions, Point

Constant Summary collapse

PRECISION =
0.000001

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Tuple

Returns a new instance of Tuple.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/laser-cutter/geometry/tuple.rb', line 9

def initialize(*args)
  x = args.first
  coordinates = if x.is_a?(String)
    parse_string(x)
  elsif x.is_a?(Hash)
    parse_hash(x)
  elsif x.is_a?(Array)
    x.clone
  elsif x.is_a?(Tuple) or x.is_a?(Vector)
    x.to_a
  else
    args.clone
  end
  coordinates.map!(&:to_f)
  self.coords = Vector.[](*coordinates)
end

Instance Attribute Details

#coordsObject

Returns the value of attribute coords.



6
7
8
# File 'lib/laser-cutter/geometry/tuple.rb', line 6

def coords
  @coords
end

Instance Method Details

#+(x, y = nil) ⇒ Object Also known as: plus



26
27
28
29
30
31
32
33
34
35
# File 'lib/laser-cutter/geometry/tuple.rb', line 26

def + x, y = nil
  shift = if x.is_a?(Vector)
            x
          elsif x.is_a?(Tuple)
            x.coords
          elsif y
            Vector.[](x,y)
          end
  self.class.new(self.coords + shift)
end

#<(other) ⇒ Object



105
106
107
# File 'lib/laser-cutter/geometry/tuple.rb', line 105

def < (other)
  self.x == other.x ? self.y < other.y : self.x < other.x
end

#<=>(other) ⇒ Object



102
103
104
# File 'lib/laser-cutter/geometry/tuple.rb', line 102

def <=>(other)
  self.x == other.x ? self.y <=> other.y : self.x <=> other.x
end

#>(other) ⇒ Object



108
109
110
# File 'lib/laser-cutter/geometry/tuple.rb', line 108

def > (other)
  self.x == other.x ? self.y > other.y : self.x > other.x
end

#[](value) ⇒ Object



73
74
75
# File 'lib/laser-cutter/geometry/tuple.rb', line 73

def [] value
  coords.[](value)
end

#cloneObject



111
112
113
114
115
# File 'lib/laser-cutter/geometry/tuple.rb', line 111

def clone
  clone = super
  clone.coords = self.coords.clone
  clone
end

#eql?(other) ⇒ Boolean

Identity, cloning and sorting/ordering

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
# File 'lib/laser-cutter/geometry/tuple.rb', line 91

def eql?(other)
  return false unless other.respond_to?(:coords)
  equal = true
  self.coords.each_with_index do |c, i|
    if (c - other.coords.to_a[i])**2 > PRECISION
      equal = false
      break
    end
  end
  equal
end

#hash_keysObject

Override in subclasses, eg: def separator

';'

end

def hash_keys

[:x, :y, :z] or [:h, :w, :d]

end



86
87
88
# File 'lib/laser-cutter/geometry/tuple.rb', line 86

def hash_keys
  [:x, :y]
end

#separatorObject



69
70
71
# File 'lib/laser-cutter/geometry/tuple.rb', line 69

def separator
  ','
end

#to_aObject



40
41
42
# File 'lib/laser-cutter/geometry/tuple.rb', line 40

def to_a
  self.coords.to_a
end

#to_sObject



44
45
46
# File 'lib/laser-cutter/geometry/tuple.rb', line 44

def to_s
  "{#{coords.to_a.map { |a| sprintf("%.5f", a) }.join(separator)}}"
end

#valid?Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/laser-cutter/geometry/tuple.rb', line 48

def valid?
  raise "Have nil value: #{self.inspect}" if coords.to_a.any? { |c| c.nil? }
  true
end

#xObject



61
62
63
# File 'lib/laser-cutter/geometry/tuple.rb', line 61

def x
  coords.[](0)
end

#x=(value) ⇒ Object



53
54
55
# File 'lib/laser-cutter/geometry/tuple.rb', line 53

def x= value
  self.coords = Vector.[](value, coords.[](1))
end

#yObject



65
66
67
# File 'lib/laser-cutter/geometry/tuple.rb', line 65

def y
  coords.[](1)
end

#y=(value) ⇒ Object



57
58
59
# File 'lib/laser-cutter/geometry/tuple.rb', line 57

def y= value
  self.coords = Vector.[](coords.[](0), value)
end