Class: Laser::Cutter::Geometry::Tuple
- Inherits:
-
Object
- Object
- Laser::Cutter::Geometry::Tuple
- Defined in:
- lib/laser/cutter/geometry/tuple.rb
Overview
A fixed list of coordinates held as a Vector: the base of Point and Dimensions. Two tuples are equal when their coordinates are within PRECISION of each other.
Direct Known Subclasses
Constant Summary collapse
- PRECISION =
The squared distance below which two coordinates count as equal.
0.000001
Instance Attribute Summary collapse
-
#coords ⇒ Vector
The coordinates, as Floats.
Instance Method Summary collapse
-
#+(x, y = nil) ⇒ Tuple
(also: #plus)
Shifts the tuple.
-
#<(other) ⇒ Boolean
Whether this tuple comes first, by x and then by y.
-
#<=>(other) ⇒ Integer?
Orders by x, then by y.
-
#>(other) ⇒ Boolean
Whether this tuple comes last, by x and then by y.
-
#[](value) ⇒ Float?
The coordinate.
-
#clone ⇒ Tuple
A copy that does not share its coordinates.
-
#eql?(other) ⇒ Boolean
Whether every coordinate is within PRECISION of the other's.
-
#hash_keys ⇒ Array<Symbol>
The keys a Hash of coordinates uses, in order.
-
#initialize(*args) ⇒ Tuple
constructor
A new instance of Tuple.
-
#separator ⇒ String
The separator between the coordinates in a String.
-
#to_a ⇒ Array<Float>
The coordinates.
-
#to_s ⇒ String
The coordinates to five digits, as "11.00000,21.00000,2.00000".
- #valid? ⇒ true
-
#x ⇒ Float
The first coordinate.
-
#x=(value) ⇒ Float
The value.
-
#y ⇒ Float
The second coordinate.
-
#y=(value) ⇒ Float
The value.
Constructor Details
#initialize(*args) ⇒ Tuple
Returns a new instance of Tuple.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/laser/cutter/geometry/tuple.rb', line 19 def initialize(*args) x = args.first coordinates = case x when String parse_string(x) when Hash parse_hash(x) when Array x.clone when Tuple, Vector x.to_a else args.clone end coordinates.map!(&:to_f) self.coords = Vector.[](*coordinates) end |
Instance Attribute Details
#coords ⇒ Vector
Returns the coordinates, as Floats.
12 13 14 |
# File 'lib/laser/cutter/geometry/tuple.rb', line 12 def coords @coords end |
Instance Method Details
#+(x, y = nil) ⇒ Tuple Also known as: plus
Shifts the tuple.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/laser/cutter/geometry/tuple.rb', line 42 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(coords + shift) end |
#<(other) ⇒ Boolean
Returns whether this tuple comes first, by x and then by y.
142 143 144 |
# File 'lib/laser/cutter/geometry/tuple.rb', line 142 def <(other) x == other.x ? y < other.y : x < other.x end |
#<=>(other) ⇒ Integer?
Orders by x, then by y.
136 137 138 |
# File 'lib/laser/cutter/geometry/tuple.rb', line 136 def <=>(other) x == other.x ? y <=> other.y : x <=> other.x end |
#>(other) ⇒ Boolean
Returns whether this tuple comes last, by x and then by y.
148 149 150 |
# File 'lib/laser/cutter/geometry/tuple.rb', line 148 def >(other) x == other.x ? y > other.y : x > other.x end |
#[](value) ⇒ Float?
Returns the coordinate.
105 106 107 |
# File 'lib/laser/cutter/geometry/tuple.rb', line 105 def [](value) coords.[](value) end |
#clone ⇒ Tuple
Returns a copy that does not share its coordinates.
153 154 155 156 157 |
# File 'lib/laser/cutter/geometry/tuple.rb', line 153 def clone clone = super clone.coords = coords.clone clone end |
#eql?(other) ⇒ Boolean
Returns whether every coordinate is within PRECISION of the other's.
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/laser/cutter/geometry/tuple.rb', line 119 def eql?(other) return false unless other.respond_to?(:coords) equal = true 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 ⇒ Array<Symbol>
The keys a Hash of coordinates uses, in order. A subclass with other coordinates overrides it, and #separator with it.
113 114 115 |
# File 'lib/laser/cutter/geometry/tuple.rb', line 113 def hash_keys [:x, :y] end |
#separator ⇒ String
Returns the separator between the coordinates in a String.
99 100 101 |
# File 'lib/laser/cutter/geometry/tuple.rb', line 99 def separator ',' end |
#to_a ⇒ Array<Float>
Returns the coordinates.
59 60 61 |
# File 'lib/laser/cutter/geometry/tuple.rb', line 59 def to_a coords.to_a end |
#to_s ⇒ String
Returns the coordinates to five digits, as "Laser::Cutter::Geometry::Tuple.11.00000,21.00000,2.00000".
64 65 66 |
# File 'lib/laser/cutter/geometry/tuple.rb', line 64 def to_s "{#{coords.to_a.map { |a| sprintf('%.5f', a) }.join(separator)}}" end |
#valid? ⇒ true
70 71 72 73 74 |
# File 'lib/laser/cutter/geometry/tuple.rb', line 70 def valid? raise "Have nil value: #{inspect}" if coords.to_a.any?(&:nil?) true end |
#x ⇒ Float
Returns the first coordinate.
89 90 91 |
# File 'lib/laser/cutter/geometry/tuple.rb', line 89 def x coords.[](0) end |
#x=(value) ⇒ Float
Returns the value.
78 79 80 |
# File 'lib/laser/cutter/geometry/tuple.rb', line 78 def x=(value) self.coords = Vector.[](value, coords.[](1)) end |
#y ⇒ Float
Returns the second coordinate.
94 95 96 |
# File 'lib/laser/cutter/geometry/tuple.rb', line 94 def y coords.[](1) end |
#y=(value) ⇒ Float
Returns the value.
84 85 86 |
# File 'lib/laser/cutter/geometry/tuple.rb', line 84 def y=(value) self.coords = Vector.[](coords.[](0), value) end |