Class: Vissen::Output::Context::Cloud

Inherits:
Object
  • Object
show all
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

Circle

Instance Attribute Summary collapse

Attributes included from Vissen::Output::Context

#height, #palettes, #width

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • points (Array<Point>, Array<Array<Integer>>)

    the position of the points in the context.

  • width (Float) (defaults to: 1.0)

    the width of the context.

  • height (Float) (defaults to: 1.0)

    the height of the context.

  • args (see Context)

    .



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

#pointsArray<Point> (readonly)

Returns the points in the context.

Returns:

  • (Array<Point>)

    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.

Parameters:

  • point_count (Integer)

    the number of points to scatter.

  • width (Numeric) (defaults to: 1.0)

    the width of the context.

  • height (Numeric) (defaults to: 1.0)

    the height of the context.

  • distance (Numeric) (defaults to: nil)

    the minimum distance between each point.

  • args (see Context)

    .

Raises:

  • (RangeError)

    if distance is too great and the allocation algorithm therefore is unlikely to converge.



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

#freezeself

Prevents any more points from being added.

Returns:

  • (self)


41
42
43
44
# File 'lib/vissen/output/context/cloud.rb', line 41

def freeze
  @points.freeze
  super
end

#point_countInteger

See ‘Context#point_count`.

Returns:

  • (Integer)

    the number of grid points.



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.

Returns:

  • (Array<Integer>)

    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