Class: Vedeu::Geometry::GenericCoordinate

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

Overview

Crudely corrects out of range values.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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

Options Hash (attributes):

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


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

def initialize(attributes = {})
  @attributes = defaults.merge!(attributes)

  @attributes.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

Instance Attribute Details

#nameString (readonly, protected)



73
74
75
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 73

def name
  @name
end

#offsetFixnum (readonly, protected)



77
78
79
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 77

def offset
  @offset
end

#typeSymbol (readonly, protected)



81
82
83
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 81

def type
  @type
end

Instance Method Details

#bdFixnum (private)

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



100
101
102
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 100

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

#bdnFixnum (private)

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



107
108
109
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 107

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

#borderObject (private)

See Also:

  • Borders::Repository#by_name


86
87
88
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 86

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

#coordinate_typeFixnum (private)

Ascertain the correct methods to use for determining the coordinates.

Raises:



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

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.'
             end
end

#dFixnum (private)

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



93
94
95
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 93

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

#d_dnFixnum (private)

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



114
115
116
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 114

def d_dn
  border.send(coordinate_type[3])
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]


159
160
161
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 159

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

#defaultsHash (private)

The default values for a new instance of this class.



166
167
168
169
170
171
172
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 166

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

#dnFixnum Also known as: xn, yn

Returns the maximum coordinate for an area.

Examples:

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


32
33
34
35
36
37
38
39
40
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 32

def dn
  if d_dn <= 0
    0

  else
    d + d_dn

  end
end

#dn_indexFixnum (private)

Returns the maximum index for an area.

Examples:

# d_dn = 3
dn_index # => 2


140
141
142
143
144
145
146
147
148
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 140

def dn_index
  if d_dn < 1
    0

  else
    d_dn - 1

  end
end

#positionFixnum Also known as: x_position, y_position

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


54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/vedeu/geometry/generic_coordinate.rb', line 54

def position
  pos = case
        when offset <= 0       then d
        when offset > dn_index then dn
        else
          d_range[offset]
        end

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