Class: Vissen::Output::Context::Cloud
- Inherits:
-
Object
- Object
- Vissen::Output::Context::Cloud
- Includes:
- Vissen::Output::Context
- Defined in:
- lib/vissen/output/context/cloud.rb
Overview
The cloud context imposes no structure on the placement of its elements but instead accepts coordinates for each individual point.
Usage
The following example creates a context with three points, placed on a straight line.
placement = [[0.1, 0.1],
[0.5, 0.5],
[0.9, 0.9]]
context = Cloud.new placement
Direct Known Subclasses
Instance Attribute Summary collapse
-
#points ⇒ Array<Point>
readonly
The points in the context.
Attributes included from Vissen::Output::Context
Class Method Summary collapse
-
.scatter(point_count, width: 1.0, height: 1.0, distance: nil, **args) ⇒ Object
Randomly places points separated by a minimum distance.
Instance Method Summary collapse
-
#freeze ⇒ self
Prevents any more points from being added.
-
#initialize(points, width: 1.0, height: 1.0, **args) ⇒ Cloud
constructor
A new instance of Cloud.
-
#point_count ⇒ Integer
See ‘Context#point_count`.
-
#position(index) ⇒ Array<Integer>
The x and y coordinates of a point.
Methods included from Vissen::Output::Context
#alloc_points, #center, #each, #each_distance_squared, #each_polar_offset, #each_position, #index_from, #one_dimensional?
Constructor Details
#initialize(points, width: 1.0, height: 1.0, **args) ⇒ Cloud
Returns a new instance of Cloud.
29 30 31 32 33 34 35 36 |
# File 'lib/vissen/output/context/cloud.rb', line 29 def initialize(points, width: 1.0, height: 1.0, **args) super(width, height, **args) factor = @width / width @points = points.map { |point| Point.from point, scale: factor } freeze end |
Instance Attribute Details
#points ⇒ Array<Point> (readonly)
Returns the points in the context.
22 23 24 |
# File 'lib/vissen/output/context/cloud.rb', line 22 def points @points end |
Class Method Details
.scatter(point_count, width: 1.0, height: 1.0, distance: nil, **args) ⇒ Object
Randomly places points separated by a minimum distance. Note that the algorithm is nondeterministic in the time it takes to find the points.
Draws a random point from the space of valid coordinates and calculates the distance to all previously selected points. If the distances are all greater than the given value the point is accepted and the process repeats until all points have been found.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/vissen/output/context/cloud.rb', line 76 def scatter(point_count, width: 1.0, height: 1.0, distance: nil, **args) if distance d2 = (distance**2) raise RangeError if 2 * d2 * point_count > width * height else d2 = (width * height) / (2.0 * point_count) end points = place_points point_count, position_scrambler(width, height), distance_condition(d2) new(points, width: width, height: height, **args) end |
Instance Method Details
#freeze ⇒ self
Prevents any more points from being added.
41 42 43 44 |
# File 'lib/vissen/output/context/cloud.rb', line 41 def freeze @points.freeze super end |
#point_count ⇒ Integer
See ‘Context#point_count`.
49 50 51 |
# File 'lib/vissen/output/context/cloud.rb', line 49 def point_count @points.length end |
#position(index) ⇒ Array<Integer>
Returns the x and y coordinates of a point.
54 55 56 |
# File 'lib/vissen/output/context/cloud.rb', line 54 def position(index) @points[index].position end |