Class: Vedeu::Geometry::Coordinate

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/geometry/coordinate.rb

Overview

Crudely corrects out of range values.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Vedeu::Geometry::Coordinate

Return a new instance of Vedeu::Geometry::Coordinate.

Parameters:

  • attributes (Hash) (defaults to: {})

Options Hash (attributes):

  • name (String|Symbol)
  • type (Symbol)
  • offset (Fixnum)


16
17
18
19
20
# File 'lib/vedeu/geometry/coordinate.rb', line 16

def initialize(attributes = {})
  defaults.merge!(attributes).each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

Instance Attribute Details

#nameString (readonly, protected)

Returns:

  • (String)


71
72
73
# File 'lib/vedeu/geometry/coordinate.rb', line 71

def name
  @name
end

#offsetFixnum (readonly, protected)

Returns:

  • (Fixnum)


75
76
77
# File 'lib/vedeu/geometry/coordinate.rb', line 75

def offset
  @offset
end

#typeSymbol (readonly, protected)

Returns:

  • (Symbol)


79
80
81
# File 'lib/vedeu/geometry/coordinate.rb', line 79

def type
  @type
end

Instance Method Details

#bdFixnum (private)

Return the :bx or :by value from the border.

Returns:

  • (Fixnum)


98
99
100
# File 'lib/vedeu/geometry/coordinate.rb', line 98

def bd
  border.send(coordinate_type[1])
end

#bdnFixnum (private)

Return the :bxn or :byn value from the border.

Returns:

  • (Fixnum)


105
106
107
# File 'lib/vedeu/geometry/coordinate.rb', line 105

def bdn
  border.send(coordinate_type[2])
end

#borderObject (private)

See Also:

  • Borders::Repository#by_name


84
85
86
# File 'lib/vedeu/geometry/coordinate.rb', line 84

def border
  @border ||= Vedeu.borders.by_name(name)
end

#coordinate_typeFixnum (private)

Ascertain the correct methods to use for determining the coordinates.

Returns:

  • (Fixnum)

Raises:



122
123
124
125
126
127
128
129
130
# File 'lib/vedeu/geometry/coordinate.rb', line 122

def coordinate_type
  @_type ||= case type
             when :x then [:x, :bx, :bxn, :width]
             when :y then [:y, :by, :byn, :height]
             else
               fail Vedeu::Error::InvalidSyntax,
                    'Coordinate type not given, cannot continue.'.freeze
             end
end

#dFixnum (private)

Return the :x or :y value from the border.

Returns:

  • (Fixnum)


91
92
93
# File 'lib/vedeu/geometry/coordinate.rb', line 91

def d
  border.send(coordinate_type[0])
end

#d_dnFixnum (private)

Return the :width or :height value from the border.

Returns:

  • (Fixnum)


112
113
114
# File 'lib/vedeu/geometry/coordinate.rb', line 112

def d_dn
  border.send(coordinate_type[3])
end

#d_positionFixnum Also known as: x, y

Returns the coordinate for a given index.

Examples:

# d_range = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
position     # => 4
position(-2) # => 4
position(2)  # => 6
position(15) # => 13

Returns:

  • (Fixnum)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vedeu/geometry/coordinate.rb', line 48

def d_position
  pos = if offset <= 0
          d

        elsif offset > dn_index
          dn_position

        else
          d_range[offset]

        end

  pos = pos < bd ? bd : pos
  pos = pos > bdn ? bdn : pos
  pos
end

#d_rangeArray (private)

Returns an array with all coordinates from d to dn.

Examples:

# d_dn = 10
# d = 4
# dn = 14
d_range # => [4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

Returns:

  • (Array)


154
155
156
# File 'lib/vedeu/geometry/coordinate.rb', line 154

def d_range
  (d...dn_position).to_a
end

#defaultsHash (private)

The default values for a new instance of this class.

Returns:

  • (Hash)


161
162
163
164
165
166
167
# File 'lib/vedeu/geometry/coordinate.rb', line 161

def defaults
  {
    name:   '',
    offset: nil,
    type:   :x,
  }
end

#dn_indexFixnum (private)

Returns the maximum index for an area.

Examples:

# d_dn = 3
dn_index # => 2

Returns:

  • (Fixnum)


139
140
141
142
143
# File 'lib/vedeu/geometry/coordinate.rb', line 139

def dn_index
  return 0 if d_dn < 1

  d_dn - 1
end

#dn_positionFixnum Also known as: xn, yn

Returns the maximum coordinate for an area.

Examples:

# d = 2
# d_dn = 4 # represents width or height
dn # => 6

Returns:

  • (Fixnum)


30
31
32
33
34
# File 'lib/vedeu/geometry/coordinate.rb', line 30

def dn_position
  return 0 if d_dn <= 0

  d + d_dn
end