Class: Vissen::Output::Context::Circle

Inherits:
Cloud
  • Object
show all
Defined in:
lib/vissen/output/context/circle.rb

Overview

Output context with the points placed counter clockwise on a circle. By specifying an offset it is possible to adjust the position of the end points of the element array.

Instance Attribute Summary

Attributes inherited from Cloud

#points

Attributes included from Vissen::Output::Context

#height, #palettes, #width

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Cloud

#freeze, #point_count, #position, scatter

Methods included from Vissen::Output::Context

#alloc_points, #center, #each, #each_distance_squared, #each_polar_offset, #each_position, #index_from, #one_dimensional?, #point_count, #position

Constructor Details

#initialize(point_count, offset: 0, width: 1.0, height: 1.0, radius: [width, height].min / 2.0, **args) ⇒ Circle

Returns a new instance of Circle.

Parameters:

  • point_count (Integer)

    the number of points.

  • offset (Numeric) (defaults to: 0)

    the angle offset, in radians, of the first point.

  • width (Numeric) (defaults to: 1.0)

    (see Context)

  • height (Numeric) (defaults to: 1.0)

    (see Context)

  • radius (Numeric) (defaults to: [width, height].min / 2.0)

    the radius of the context.

  • args (see CloudContext)

    .



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/vissen/output/context/circle.rb', line 17

def initialize(point_count,
               offset: 0,
               width: 1.0,
               height: 1.0,
               radius: [width, height].min / 2.0,
               **args)

  circle = self.class.position_generator(point_count, radius, offset)
  center = [width.to_f / 2, height.to_f / 2]
  points = self.class.place_points circle, center

  super(points, width: width, height: height, **args)
end

Class Method Details

.place_points(generator, center) ⇒ Array<Point>

Uses a position generator to allocate an array of ‘Point` objects placed arount a circle centered around the given coordinates.

Parameters:

  • generator (Enumerator)

    the position generator (see .position_generator).

  • center (Array<Numeric>)

    the x and y coordinates of the circle center.

Returns:

  • (Array<Point>)

    an array of ‘Point` objects placed around a circle.



62
63
64
65
# File 'lib/vissen/output/context/circle.rb', line 62

def place_points(generator, center)
  x0, y0 = center
  generator.map { |x, y| Point.new x0 + x, y0 + y }
end

.position_generator(point_count, radius, offset = 0) ⇒ Enumerator

Creates a generator (‘Enumerator`) for x and y coordinates equidistantly placed along a circle centered around (0, 0).

Parameters:

  • point_count (Integer)

    the number of points along the circle.

  • radius (Numeric)

    the radius of the circle.

  • offset (Numeric) (defaults to: 0)

    the angular offset of the first point. An offset of pi/2 would place the first point at the twelve o’clock position.

Returns:

  • (Enumerator)

    an enumerator that yields ‘point_count` x and y coordinates along a circle.



42
43
44
45
46
47
48
49
50
51
# File 'lib/vissen/output/context/circle.rb', line 42

def position_generator(point_count, radius, offset = 0)
  angle_factor = 2.0 * Math::PI / point_count

  Enumerator.new(point_count) do |y|
    point_count.times do |index|
      angle = index * angle_factor + offset
      y << [radius * Math.cos(angle), radius * Math.sin(angle)]
    end
  end
end