module Vissen::Output::Context

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.

Attributes

height[R]

@return [Float] the normalized width of the context.

palettes[R]

@return [Array<Palette>] the array of palettes used to render the

context.
width[R]

@return [Float] the normalized width of the context.

Public Class Methods

new(width, height, palettes: PALETTES) click to toggle source

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.

@param width [Numeric] the width of the context. @param height [Numeric] the height of the context. @param palettes [Array<Palette>] the color palettes to use when

rendering the context.
# 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

Public Instance Methods

alloc_points(klass = nil, &block) click to toggle source

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.

@raise [ArgumentError] if both a class and a block are given.

@param klass [Class] the class of the allocated objects. @param block [Proc] the block to call for allocating each object. @return [Array<klass>] an array of new objects.

# 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
center() click to toggle source

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

@return [Array<Float>] the center coordinates.

# File lib/vissen/output/context.rb, line 78
def center
  [width / 2.0, height / 2.0]
end
distance_squared(x, y)
each() click to toggle source

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

@return [Enumerator, Integer] an `Enumerator` if no block is given,

otherwise the number of points that was iterated over.
# File lib/vissen/output/context.rb, line 87
def each
  point_count.times
end
each_distance_squared(x, y) { |x_i| ... } click to toggle source

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

@param x [Numeric] the x coordinate to calculate distances from. @param y [Numeric] the y coordinate to calculate distances from. @return [Enumerable] if no block is given.

# 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
Also aliased as: distance_squared
each_polar_offset(x, y) { |distance, angle| ... } click to toggle source

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.

@param x [Numeric] the x coordinate to calculate angle and distance

from.

@param y [Numeric] the y coordinate to calculate angle and distance

from.

@return [Enumerable] if no block is given.

# 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_position() { |index, *position(index)| ... } click to toggle source

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

@return (see each)

# 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) click to toggle source

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.

@param index [Object] the object or objects that are used to refer to

points in this context.

@return [Integer] the index of the point.

# File lib/vissen/output/context.rb, line 120
def index_from(index)
  index
end
one_dimensional?() click to toggle source

@return [true, false] true if the context has only one dimension.

# File lib/vissen/output/context.rb, line 52
def one_dimensional?
  @width == 0.0 || @height == 0.0
end
point_count() click to toggle source

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

# File lib/vissen/output/context.rb, line 47
def point_count
  raise NotImplementedError
end
position(_index) click to toggle source

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.

@param _index [Integer] the index of a point in the context. @return [Array<Float>] an array containing the x and y coordinates of

the point associated with the given intex.
# File lib/vissen/output/context.rb, line 97
def position(_index)
  raise NotImplementedError
end