Module: Vissen::Output::Context

Included in:
Cloud, Grid
Defined in:
lib/vissen/output/context.rb,
lib/vissen/output/context/grid.rb,
lib/vissen/output/context/cloud.rb,
lib/vissen/output/context/circle.rb

Overview

The output context gives the points that relate to it their position and color. Contexts can come in different forms and offer different functionality but they must be able to answer three questions:

  1. How many points are in the context,

  2. what is the absolute position of each point and

  3. what color palette corresponds to a palette index.

Defined Under Namespace

Classes: Circle, Cloud, Grid

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#heightFloat (readonly)

Returns the normalized width of the context.

Returns:

  • (Float)

    the normalized width of the context.



18
19
20
# File 'lib/vissen/output/context.rb', line 18

def height
  @height
end

#palettesArray<Palette> (readonly)

Returns the array of palettes used to render the context.

Returns:

  • (Array<Palette>)

    the array of palettes used to render the context.



22
23
24
# File 'lib/vissen/output/context.rb', line 22

def palettes
  @palettes
end

#widthFloat (readonly)

Returns the normalized width of the context.

Returns:

  • (Float)

    the normalized width of the context.



15
16
17
# File 'lib/vissen/output/context.rb', line 15

def width
  @width
end

Instance Method Details

#alloc_points(klass = nil, &block) ⇒ Array<klass>

Allocates, for each grid point, one object of the given class. Optionally takes a block that is expected to return each new object. The index of the element is passed to the given block.

Parameters:

  • klass (Class) (defaults to: nil)

    the class of the allocated objects.

  • block (Proc)

    the block to call for allocating each object.

Returns:

  • (Array<klass>)

    an array of new objects.

Raises:

  • (ArgumentError)

    if both a class and a block are given.



65
66
67
68
69
70
71
72
# File 'lib/vissen/output/context.rb', line 65

def alloc_points(klass = nil, &block)
  if klass
    raise ArgumentError if block_given?
    block = proc { klass.new }
  end

  Array.new(point_count, &block)
end

#centerArray<Float>

Uses the width and height och the context to calculate the x and y coordinates of the center point.

Returns:

  • (Array<Float>)

    the center coordinates.



78
79
80
# File 'lib/vissen/output/context.rb', line 78

def center
  [width / 2.0, height / 2.0]
end

#eachEnumerator, Integer

Iterates over the context points. The index of the the point is passed to the given block.

Returns:

  • (Enumerator, Integer)

    an ‘Enumerator` if no block is given, otherwise the number of points that was iterated over.



87
88
89
# File 'lib/vissen/output/context.rb', line 87

def each
  point_count.times
end

#each_distance_squared(x, y) ⇒ Enumerable Also known as: distance_squared

This utility method calculates the squared distance between each point and the given coordinate. The squared distance is then yielded to the given block.

Parameters:

  • x (Numeric)

    the x coordinate to calculate distances from.

  • y (Numeric)

    the y coordinate to calculate distances from.

Returns:

  • (Enumerable)

    if no block is given.



131
132
133
134
135
136
137
# File 'lib/vissen/output/context.rb', line 131

def each_distance_squared(x, y)
  return to_enum(__callee__, x, y) unless block_given?

  each_position do |_, x_i, y_i|
    yield (x_i - x)**2 + (y_i - y)**2
  end
end

#each_polar_offset(x, y) ⇒ Enumerable

This method calculates the distance and angle to each point and the given coordinate. The distance and angle are then yielded to the given block.

Parameters:

  • x (Numeric)

    the x coordinate to calculate angle and distance from.

  • y (Numeric)

    the y coordinate to calculate angle and distance from.

Returns:

  • (Enumerable)

    if no block is given.



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/vissen/output/context.rb', line 150

def each_polar_offset(x, y)
  return to_enum(__callee__, x, y) unless block_given?

  each_position do |_, x_i, y_i|
    dx = x_i - x
    dy = y_i - y

    distance = Math.sqrt(dx**2 + dy**2)
    angle    = Math.atan2 dy, dx

    yield distance, angle
  end
end

#each_positionEnumerator, Integer

Iterates over each point in the grid and yields the point index and x and y coordinates.

Returns:

  • (Enumerator, Integer)

    an ‘Enumerator` if no block is given, otherwise the number of points that was iterated over.



105
106
107
108
109
110
111
# File 'lib/vissen/output/context.rb', line 105

def each_position
  return to_enum(__callee__) unless block_given?

  point_count.times do |index|
    yield(index, *position(index))
  end
end

#index_from(index) ⇒ Integer

Context specific method to convert any domain specifc property (like row and column) to an index. The Cloud module calls this method when resolving the index given to its #[] method.

Parameters:

  • index (Object)

    the object or objects that are used to refer to points in this context.

Returns:

  • (Integer)

    the index of the point.



120
121
122
# File 'lib/vissen/output/context.rb', line 120

def index_from(index)
  index
end

#initialize(width, height, palettes: PALETTES) ⇒ Object

Output contexts give the two things to the vixels within them: a position and a color.

The width and height are always normalized to fit within a 1 x 1 square.

Parameters:

  • width (Numeric)

    the width of the context.

  • height (Numeric)

    the height of the context.

  • palettes (Array<Palette>) (defaults to: PALETTES)

    the color palettes to use when rendering the context.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vissen/output/context.rb', line 33

def initialize(width, height, palettes: PALETTES)
  if width.negative? || height.negative? || (width.zero? && height.zero?)
    raise RangeError, 'Contexts needs a size in at least one dimension'
  end

  # Normalize width and height
  normalizing_factor = 1.0 / [width, height].max

  @width    = width * normalizing_factor
  @height   = height * normalizing_factor
  @palettes = palettes
end

#one_dimensional?true, false

Returns true if the context has only one dimension.

Returns:

  • (true, false)

    true if the context has only one dimension.



52
53
54
# File 'lib/vissen/output/context.rb', line 52

def one_dimensional?
  @width == 0.0 || @height == 0.0
end

#point_countObject

This method must be implemented by any class that includes this module.

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/vissen/output/context.rb', line 47

def point_count
  raise NotImplementedError
end

#position(_index) ⇒ Array<Float>

This method must be implemented by a class that includes this module and should return the x and y coordinates of the point with the given index.

Parameters:

  • _index (Integer)

    the index of a point in the context.

Returns:

  • (Array<Float>)

    an array containing the x and y coordinates of the point associated with the given intex.

Raises:

  • (NotImplementedError)


97
98
99
# File 'lib/vissen/output/context.rb', line 97

def position(_index)
  raise NotImplementedError
end