Class: Vissen::Output::Context::Circle
- 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
Attributes included from Vissen::Output::Context
Class Method Summary collapse
-
.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.
-
.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).
Instance Method Summary collapse
-
#initialize(point_count, offset: 0, width: 1.0, height: 1.0, radius: [width, height].min / 2.0, **args) ⇒ Circle
constructor
A new instance of Circle.
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.
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.
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).
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 |