Class: Vedeu::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(height, width, x, y) ⇒ Vedeu::Coordinate

Parameters:



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

def initialize(height, width, x, y)
  @height = height
  @width  = width
  @x      = x
  @y      = y
end

Instance Attribute Details

#heightObject (readonly, private)

Returns the value of attribute height.



7
8
9
# File 'lib/vedeu/geometry/coordinate.rb', line 7

def height
  @height
end

#widthObject (readonly, private)

Returns the value of attribute width.



7
8
9
# File 'lib/vedeu/geometry/coordinate.rb', line 7

def width
  @width
end

#xObject (readonly)

Returns the value of attribute x.



7
8
9
# File 'lib/vedeu/geometry/coordinate.rb', line 7

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



7
8
9
# File 'lib/vedeu/geometry/coordinate.rb', line 7

def y
  @y
end

Instance Method Details

#x_index(position = x) ⇒ Fixnum

Returns the index for a given x position.

Examples:

# x_range = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
# x = 8
x_index     # => 4 # because (x_range[4] = 8)
x_index(11) # => 7
x_index(2)  # => 0
x_index(15) # => 9

Parameters:

  • position (Fixnum) (defaults to: x)

Returns:



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/vedeu/geometry/coordinate.rb', line 97

def x_index(position = x)
  if width <= 0 || position <= x
    0

  elsif position >= xn
    xn_index

  else
    x_range.index(position)

  end
end

#x_indicesArray (private)

Returns the same as #x_range, except as indices of an array.

Examples:

# width = 10
x_indices # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Returns:

  • (Array)


206
207
208
# File 'lib/vedeu/geometry/coordinate.rb', line 206

def x_indices
  (0...width).to_a
end

#x_position(index = 0) ⇒ Fixnum

Returns the x coordinate for a given index.

Examples:

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

Parameters:

  • index (Fixnum) (defaults to: 0)

Returns:



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/vedeu/geometry/coordinate.rb', line 145

def x_position(index = 0)
  if index <= 0
    x

  elsif index > xn_index
    xn

  else
    x_range[index]

  end
end

#x_rangeArray (private)

Returns an array with all coordinates from x to xn.

Examples:

# width = 10
# x = 4
# xn = 14
x_range # => [4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

Returns:

  • (Array)


219
220
221
# File 'lib/vedeu/geometry/coordinate.rb', line 219

def x_range
  (x...xn).to_a
end

#xnFixnum

Returns the maximum x coordinate for an area.

Examples:

# x = 5
# width = 20
xn # => 25

Returns:



50
51
52
53
54
55
56
57
58
# File 'lib/vedeu/geometry/coordinate.rb', line 50

def xn
  if width <= 0
    0

  else
    x + width

  end
end

#xn_indexFixnum (private)

Returns the maximum x index for an area.

Examples:

# width = 6
xn_index # => 5

Returns:



182
183
184
185
186
# File 'lib/vedeu/geometry/coordinate.rb', line 182

def xn_index
  return 0 if x_indices.empty?

  x_indices.last
end

#y_index(position = y) ⇒ Fixnum

Returns the index for a given y position.

Examples:

# y_range  = [7, 8, 9, 10]
# y = 8
y_index     # => 1 # because (y_range[1] = 8)
y_index(10) # => 3
y_index(5)  # => 0
y_index(15) # => 3

Parameters:

  • position (Fixnum) (defaults to: y)

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/vedeu/geometry/coordinate.rb', line 72

def y_index(position = y)
  if height <= 0 || position <= y
    0

  elsif position >= yn
    yn_index

  else
    y_range.index(position)

  end
end

#y_indicesArray (private)

Returns the same as #y_range, except as indices of an array.

Examples:

# height = 4
y_indices # => [0, 1, 2, 3]

Returns:

  • (Array)


195
196
197
# File 'lib/vedeu/geometry/coordinate.rb', line 195

def y_indices
  (0...height).to_a
end

#y_position(index = 0) ⇒ Fixnum

Returns the y coordinate for a given index.

Examples:

# y_range = [7, 8, 9, 10, 11]
y_position     # => 7
y_position(-2) # => 7
y_position(2)  # => 9
y_position(7)  # => 11

Parameters:

  • index (Fixnum) (defaults to: 0)

Returns:



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

def y_position(index = 0)
  if index <= 0
    y

  elsif index > yn_index
    yn

  else
    y_range[index]

  end
end

#y_rangeArray (private)

Returns an array with all coordinates from y to yn.

Examples:

# height = 4
# y  = 7
# yn  = 11
y_range # => [7, 8, 9, 10]

Returns:

  • (Array)


232
233
234
# File 'lib/vedeu/geometry/coordinate.rb', line 232

def y_range
  (y...yn).to_a
end

#ynFixnum

Returns the maximum y coordinate for an area.

Examples:

# y = 2
# height = 4
yn # => 6

Returns:



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

def yn
  if height <= 0
    0

  else
    y + height

  end
end

#yn_indexFixnum (private)

Returns the maximum y index for an area.

Examples:

# height = 3
yn_index # => 2

Returns:



169
170
171
172
173
# File 'lib/vedeu/geometry/coordinate.rb', line 169

def yn_index
  return 0 if y_indices.empty?

  y_indices.last
end