Class: Laser::Cutter::Geometry::Tuple
- Inherits:
-
Object
- Object
- Laser::Cutter::Geometry::Tuple
- Defined in:
- lib/laser-cutter/geometry/tuple.rb
Direct Known Subclasses
Constant Summary collapse
- PRECISION =
0.000001
Instance Attribute Summary collapse
-
#coords ⇒ Object
Returns the value of attribute coords.
Instance Method Summary collapse
- #+(x, y = nil) ⇒ Object (also: #plus)
- #<(other) ⇒ Object
- #<=>(other) ⇒ Object
- #>(other) ⇒ Object
- #[](value) ⇒ Object
- #clone ⇒ Object
-
#eql?(other) ⇒ Boolean
Identity, cloning and sorting/ordering.
-
#hash_keys ⇒ Object
Override in subclasses, eg: def separator ‘;’ end.
-
#initialize(*args) ⇒ Tuple
constructor
A new instance of Tuple.
- #separator ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
- #x ⇒ Object
- #x=(value) ⇒ Object
- #y ⇒ Object
- #y=(value) ⇒ Object
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
#coords ⇒ Object
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 |
#clone ⇒ Object
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
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_keys ⇒ Object
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 |
#separator ⇒ Object
69 70 71 |
# File 'lib/laser-cutter/geometry/tuple.rb', line 69 def separator ',' end |
#to_a ⇒ Object
40 41 42 |
# File 'lib/laser-cutter/geometry/tuple.rb', line 40 def to_a self.coords.to_a end |
#to_s ⇒ Object
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
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 |
#x ⇒ Object
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 |
#y ⇒ Object
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 |