Class: Coordinate

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

Constructor Details

#initialize(*scalars) ⇒ Object

Returns a new Coordinate at the given scalars.

Parameters:

  • scalars ({Integer})

    the dimension coordinate values



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.

Parameters:

  • other (Coordinate)

    the coordinate to compare

Returns:

  • (Boolean)

    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.

Parameters:

  • other (Coordinate)

    the coordinate to compare

Returns:

  • (Boolean)

    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.

Examples:

Coordinate.new(2, 1) < Coordinate.new(1, 2) #=> true

Parameters:

  • other (Coordinate)

    the coordinate to compare

Returns:

  • (Integer)

    the high-to-low dimension comparison

Raises:

  • (ArgumentError)

    if this Coordinate dimension size Coordinate differs from that of the other Dimension or any of the dimension values are nil

  • (TypeError)

    if other is not a Coordinate



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.

Parameters:

  • other (Coordinate)

    the coordinate to compare

Returns:

  • (Boolean)

    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.

Parameters:

  • other (Coordinate)

    the coordinate to compare

Returns:

  • (Boolean)

    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.

Parameters:

  • other (Coordinate)

    the coordinate to compare

Returns:

  • (Boolean)

    the comparison result



70
71
72
# File 'lib/caruby/helpers/coordinate.rb', line 70

def >=(other)
  (self <=> other) >= 0
end

#to_sObject



99
100
101
# File 'lib/caruby/helpers/coordinate.rb', line 99

def to_s
  inspect
end

#xInteger

Returns the first dimension.

Returns:

  • (Integer)

    the first dimension



15
16
17
# File 'lib/caruby/helpers/coordinate.rb', line 15

def x
  self[0]
end

#x=(value) ⇒ Object

Parameters:

  • the (Integer)

    first dimension value



20
21
22
# File 'lib/caruby/helpers/coordinate.rb', line 20

def x=(value)
  self[0] = value
end

#yInteger?

Returns the second dimension.

Returns:

  • (Integer, nil)

    the second dimension



25
26
27
# File 'lib/caruby/helpers/coordinate.rb', line 25

def y
  self[1]
end

#y=(value) ⇒ Object

Parameters:

  • the (Integer)

    second dimension value



30
31
32
# File 'lib/caruby/helpers/coordinate.rb', line 30

def y=(value)
  self[1] = value
end

#zInteger?

Returns the third dimension.

Returns:

  • (Integer, nil)

    the third dimension



35
36
37
# File 'lib/caruby/helpers/coordinate.rb', line 35

def z
  self[2]
end

#z=(value) ⇒ Object

Parameters:

  • the (Integer)

    third dimension value



40
41
42
# File 'lib/caruby/helpers/coordinate.rb', line 40

def z=(value)
  self[2] = value
end