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

Returns a new instance of Vedeu::Coordinate.

Parameters:

  • height (Fixnum)
  • width (Fixnum)
  • x (Fixnum)
  • y (Fixnum)


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

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

Instance Attribute Details

#heightFixnum (readonly)

Returns:

  • (Fixnum)


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

def height
  @height
end

#nameString (readonly, protected)

Returns:

  • (String)


124
125
126
# File 'lib/vedeu/geometry/coordinate.rb', line 124

def name
  @name
end

#widthFixnum (readonly)

Returns:

  • (Fixnum)


12
13
14
# File 'lib/vedeu/geometry/coordinate.rb', line 12

def width
  @width
end

#xFixnum (readonly)

Returns:

  • (Fixnum)


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

def x
  @x
end

#yFixnum (readonly)

Returns:

  • (Fixnum)


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

def y
  @y
end

Instance Method Details

#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)


172
173
174
# File 'lib/vedeu/geometry/coordinate.rb', line 172

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:

  • (Fixnum)


107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/vedeu/geometry/coordinate.rb', line 107

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)


185
186
187
# File 'lib/vedeu/geometry/coordinate.rb', line 185

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:

  • (Fixnum)


62
63
64
65
66
67
68
69
70
# File 'lib/vedeu/geometry/coordinate.rb', line 62

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:

  • (Fixnum)


148
149
150
151
152
# File 'lib/vedeu/geometry/coordinate.rb', line 148

def xn_index
  return 0 if x_indices.empty?

  x_indices.last
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)


161
162
163
# File 'lib/vedeu/geometry/coordinate.rb', line 161

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:

  • (Fixnum)


83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vedeu/geometry/coordinate.rb', line 83

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)


198
199
200
# File 'lib/vedeu/geometry/coordinate.rb', line 198

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:

  • (Fixnum)


44
45
46
47
48
49
50
51
52
# File 'lib/vedeu/geometry/coordinate.rb', line 44

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:

  • (Fixnum)


135
136
137
138
139
# File 'lib/vedeu/geometry/coordinate.rb', line 135

def yn_index
  return 0 if y_indices.empty?

  y_indices.last
end