Class: Coordinate
- Inherits:
-
Array
- Object
- Array
- Coordinate
- Includes:
- Comparable
- Defined in:
- lib/caruby/helpers/coordinate.rb
Overview
A Coordinate is a convenience Array wrapper class with aliased #x, #y and #z dimensions.
Instance Method Summary collapse
-
#<(other) ⇒ Boolean
The comparison result.
-
#<=(other) ⇒ Boolean
The comparison result.
-
#<=>(other) ⇒ Integer
Returns the comparison of the highest dimension which differs from the other coordinate, or zero if all dimensions are the same.
-
#==(other) ⇒ Boolean
Whether other is a Coordinate and has the same content as this Coordinate.
-
#>(other) ⇒ Boolean
The comparison result.
-
#>=(other) ⇒ Boolean
The comparison result.
-
#initialize(*scalars) ⇒ Object
constructor
A new Coordinate at the given scalars.
- #to_s ⇒ Object
-
#x ⇒ Integer
The first dimension.
- #x=(value) ⇒ Object
-
#y ⇒ Integer?
The second dimension.
- #y=(value) ⇒ Object
-
#z ⇒ Integer?
The third dimension.
- #z=(value) ⇒ Object
Constructor Details
#initialize(*scalars) ⇒ Object
Returns a new Coordinate at the given scalars.
10 11 12 |
# File 'lib/caruby/helpers/coordinate.rb', line 10 def initialize(*scalars) super(scalars) end |
Instance Method Details
#<(other) ⇒ Boolean
Returns the comparison result.
52 53 54 |
# File 'lib/caruby/helpers/coordinate.rb', line 52 def <(other) (self <=> other) < 0 end |
#<=(other) ⇒ Boolean
Returns the comparison result.
58 59 60 |
# File 'lib/caruby/helpers/coordinate.rb', line 58 def <=(other) (self <=> other) <= 0 end |
#<=>(other) ⇒ Integer
Returns the comparison of the highest dimension which differs from the other coordinate, or zero if all dimensions are the same. This comparator sorts coordinates in z-y-x order.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/caruby/helpers/coordinate.rb', line 84 def <=>(other) return 0 if equal?(other) raise TypeError.new("Can't compare #{self} with #{other} since it is not a Coordinate") unless Coordinate === other raise ArgumentError.new("Can't compare #{self} with #{other} since it has a different dimension count") unless size == other.size REXML::SyncEnumerator.new(self.reverse, other.reverse).each_with_index do |pair, index| dim = pair.first odim = pair.last raise ArgumentError.new("Can't compare #{self} with missing dimension #{index} to #{other}") unless dim raise ArgumentError.new("Can't compare #{self} to #{other} with missing dimension #{index}") unless odim cmp = dim <=> odim return cmp unless cmp.zero? end 0 end |
#==(other) ⇒ Boolean
Returns whether other is a Coordinate and has the same content as this Coordinate.
46 47 48 |
# File 'lib/caruby/helpers/coordinate.rb', line 46 def ==(other) super rescue false end |
#>(other) ⇒ Boolean
Returns the comparison result.
64 65 66 |
# File 'lib/caruby/helpers/coordinate.rb', line 64 def >(other) (self <=> other) > 0 end |
#>=(other) ⇒ Boolean
Returns the comparison result.
70 71 72 |
# File 'lib/caruby/helpers/coordinate.rb', line 70 def >=(other) (self <=> other) >= 0 end |
#to_s ⇒ Object
99 100 101 |
# File 'lib/caruby/helpers/coordinate.rb', line 99 def to_s inspect end |
#x ⇒ Integer
Returns the first dimension.
15 16 17 |
# File 'lib/caruby/helpers/coordinate.rb', line 15 def x self[0] end |
#x=(value) ⇒ Object
20 21 22 |
# File 'lib/caruby/helpers/coordinate.rb', line 20 def x=(value) self[0] = value end |
#y ⇒ Integer?
Returns the second dimension.
25 26 27 |
# File 'lib/caruby/helpers/coordinate.rb', line 25 def y self[1] end |
#y=(value) ⇒ Object
30 31 32 |
# File 'lib/caruby/helpers/coordinate.rb', line 30 def y=(value) self[1] = value end |
#z ⇒ Integer?
Returns the third dimension.
35 36 37 |
# File 'lib/caruby/helpers/coordinate.rb', line 35 def z self[2] end |
#z=(value) ⇒ Object
40 41 42 |
# File 'lib/caruby/helpers/coordinate.rb', line 40 def z=(value) self[2] = value end |