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

Inherits:
Object
  • Object
show all
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

Dimensions, Point

Constant Summary collapse

PRECISION =

The squared distance below which two coordinates count as equal.

0.000001

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Tuple

Returns a new instance of Tuple.

Parameters:

  • args (Array) —

    the coordinates, as numbers, or one String ("1,2"), Hash (1, y: 2), Array, Tuple or Vector



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.

Returns:

  • (Vector) —

    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.

Parameters:

  • x (Vector, Tuple, Numeric) —

    the shift, or its x

  • y (Numeric, nil) (defaults to: nil) —

    the y of the shift, when x is a number

Returns:

  • (Tuple) —

    a new tuple of the same class



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.

Parameters:

Returns:

  • (Boolean) —

    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.

Parameters:

Returns:

  • (Integer, nil)


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.

Parameters:

Returns:

  • (Boolean) —

    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.

Parameters:

  • value (Integer) —

    the index of a coordinate

Returns:

  • (Float, nil) —

    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.

Returns:

  • (Tuple) —

    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.

Parameters:

Returns:

  • (Boolean) —

    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.

Returns:

  • (Array<Symbol>)


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.

Returns:

  • (String) —

    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.

Returns:

  • (Array<Float>) —

    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".

Returns:

  • (String) —

    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

Returns:

  • (true)

Raises:

  • (RuntimeError) —

    when a coordinate is nil



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.

Returns:

  • (Float) —

    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.

Parameters:

  • value (Float) —

    the new x

Returns:

  • (Float) —

    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.

Returns:

  • (Float) —

    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.

Parameters:

  • value (Float) —

    the new y

Returns:

  • (Float) —

    the value



84
85
86
# File 'lib/laser/cutter/geometry/tuple.rb', line 84

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