Module: Vissen::Output::Context
- 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:
-
How many points are in the context,
-
what is the absolute position of each point and
-
what color palette corresponds to a palette index.
Defined Under Namespace
Instance Attribute Summary collapse
-
#height ⇒ Float
readonly
The normalized width of the context.
-
#palettes ⇒ Array<Palette>
readonly
The array of palettes used to render the context.
-
#width ⇒ Float
readonly
The normalized width of the context.
Instance Method Summary collapse
-
#alloc_points(klass = nil, &block) ⇒ Array<klass>
Allocates, for each grid point, one object of the given class.
-
#center ⇒ Array<Float>
Uses the width and height och the context to calculate the x and y coordinates of the center point.
-
#each ⇒ Enumerator, Integer
Iterates over the context points.
-
#each_distance_squared(x, y) ⇒ Enumerable
(also: #distance_squared)
This utility method calculates the squared distance between each point and the given coordinate.
-
#each_polar_offset(x, y) ⇒ Enumerable
This method calculates the distance and angle to each point and the given coordinate.
-
#each_position ⇒ Enumerator, Integer
Iterates over each point in the grid and yields the point index and x and y coordinates.
-
#index_from(index) ⇒ Integer
Context specific method to convert any domain specifc property (like row and column) to an index.
-
#initialize(width, height, palettes: PALETTES) ⇒ Object
Output contexts give the two things to the vixels within them: a position and a color.
-
#one_dimensional? ⇒ true, false
True if the context has only one dimension.
-
#point_count ⇒ Object
This method must be implemented by any class that includes this module.
-
#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.
Instance Attribute Details
#height ⇒ Float (readonly)
Returns the normalized width of the context.
18 19 20 |
# File 'lib/vissen/output/context.rb', line 18 def height @height end |
#palettes ⇒ Array<Palette> (readonly)
Returns the array of palettes used to render the context.
22 23 24 |
# File 'lib/vissen/output/context.rb', line 22 def palettes @palettes end |
#width ⇒ Float (readonly)
Returns 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.
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 |
#center ⇒ Array<Float>
Uses the width and height och the context to calculate the x and y coordinates of the center point.
78 79 80 |
# File 'lib/vissen/output/context.rb', line 78 def center [width / 2.0, height / 2.0] end |
#each ⇒ Enumerator, Integer
Iterates over the context points. The index of the the point is passed to the given block.
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.
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.
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_position ⇒ Enumerator, Integer
Iterates over each point in the grid and yields the point index and x and y coordinates.
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.
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.
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.
52 53 54 |
# File 'lib/vissen/output/context.rb', line 52 def one_dimensional? @width == 0.0 || @height == 0.0 end |
#point_count ⇒ Object
This method must be implemented by any class that includes this module.
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.
97 98 99 |
# File 'lib/vissen/output/context.rb', line 97 def position(_index) raise NotImplementedError end |