class Laser::Cutter::Geometry::Tuple
Constants
- PRECISION
Attributes
coords[RW]
Public Class Methods
new(*args)
click to toggle source
# 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
Public Instance Methods
+(x, y = nil)
click to toggle source
# 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
Also aliased as: plus
<(other)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/laser-cutter/geometry/tuple.rb, line 73 def [] value coords.[](value) end
clone()
click to toggle source
Calls superclass method
# File lib/laser-cutter/geometry/tuple.rb, line 111 def clone clone = super clone.coords = self.coords.clone clone end
eql?(other)
click to toggle source
Identity, cloning and sorting/ordering
# 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()
click to toggle source
Override in subclasses, eg: def separator
';'
end
def hash_keys
[:x, :y, :z] or [:h, :w, :d]
end
# File lib/laser-cutter/geometry/tuple.rb, line 86 def hash_keys [:x, :y] end
separator()
click to toggle source
# File lib/laser-cutter/geometry/tuple.rb, line 69 def separator ',' end
to_a()
click to toggle source
# File lib/laser-cutter/geometry/tuple.rb, line 40 def to_a self.coords.to_a end
to_s()
click to toggle source
# File lib/laser-cutter/geometry/tuple.rb, line 44 def to_s "{#{coords.to_a.map { |a| sprintf("%.5f", a) }.join(separator)}}" end
valid?()
click to toggle source
# 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()
click to toggle source
# File lib/laser-cutter/geometry/tuple.rb, line 61 def x coords.[](0) end
x=(value)
click to toggle source
# File lib/laser-cutter/geometry/tuple.rb, line 53 def x= value self.coords = Vector.[](value, coords.[](1)) end
y()
click to toggle source
# File lib/laser-cutter/geometry/tuple.rb, line 65 def y coords.[](1) end
y=(value)
click to toggle source
# File lib/laser-cutter/geometry/tuple.rb, line 57 def y= value self.coords = Vector.[](coords.[](0), value) end
Private Instance Methods
parse_hash(hash)
click to toggle source
Return array of coordinates
# File lib/laser-cutter/geometry/tuple.rb, line 126 def parse_hash hash hash_keys.map{ |k,v| hash[k] } end
parse_string(string)
click to toggle source
Convert from, eg “100,50” to [100.0, 50.0],
# File lib/laser-cutter/geometry/tuple.rb, line 121 def parse_string string string.split(separator).map(&:to_f) end