Class: VectorSalad::StandardShapes::Circle
- Inherits:
-
BasicShape
- Object
- BasicShape
- VectorSalad::StandardShapes::Circle
- Includes:
- Mixins::At
- Defined in:
- lib/vector_salad/standard_shapes/circle.rb,
lib/vector_salad/exporters/svg_exporter.rb
Overview
Perfect circle shape.
Instance Attribute Summary (collapse)
-
- (Object) options
inherited
from BasicShape
Returns the value of attribute options.
-
- (Object) radius
readonly
Returns the value of attribute radius.
Instance Method Summary (collapse)
-
- (Any) [](x, y)
included
from Mixins::At
Set the x, y coordinates of the shape.
-
- (Coords) at
included
from Mixins::At
Get the x, y coordinates of the shape.
-
- (Any) at=(at)
included
from Mixins::At
Set the x, y coordinates of the shape directly.
-
- (Circle) initialize(radius, **options)
constructor
Create a perfectly round circle.
-
- (Any) move(x, y)
included
from Mixins::At
Move the shape relatively.
-
- (Object) to_path
Convert the shape to a path.
-
- (Path) to_simple_path(fn = nil)
Flatten the circle into many small straight line segments.
-
- (Object) to_svg
Export the shape to an svg string.
Constructor Details
- (Circle) initialize(radius, **options)
Create a perfectly round circle.
17 18 19 20 21 22 |
# File 'lib/vector_salad/standard_shapes/circle.rb', line 17 def initialize(radius, **) @options = @radius = radius @x, @y = 0, 0 self end |
Instance Attribute Details
- (Object) options Originally defined in class BasicShape
Returns the value of attribute options
- (Object) radius (readonly)
Returns the value of attribute radius
10 11 12 |
# File 'lib/vector_salad/standard_shapes/circle.rb', line 10 def radius @radius end |
Instance Method Details
- (Any) [](x, y) Originally defined in module Mixins::At
Set the x, y coordinates of the shape.
- (Coords) at Originally defined in module Mixins::At
Get the x, y coordinates of the shape.
- (Any) at=(at) Originally defined in module Mixins::At
Set the x, y coordinates of the shape directly.
- (Any) move(x, y) Originally defined in module Mixins::At
Move the shape relatively.
- (Object) to_path
Convert the shape to a path
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/vector_salad/standard_shapes/circle.rb', line 25 def to_path # Standard: # http://stackoverflow.com/a/13338311 # c = 4 * (Math.sqrt(2) - 1) / 3 # c = 0.5522847498307936 # # Better: # http://spencermortensen.com/articles/bezier-circle/ c = 0.551915024494 d = c * @radius Path.new( N.n(@x + @radius, @y), N.c(@x + @radius, @y + d), N.c(@x + d, @y + @radius), N.n(@x, @y + @radius), N.c(@x - d, @y + @radius), N.c(@x - @radius, @y + d), N.n(@x - @radius, @y), N.c(@x - @radius, @y - d), N.c(@x - d, @y - @radius), N.n(@x, @y - @radius), N.c(@x + d, @y - @radius), N.c(@x + @radius, @y - d), N.n(@x + @radius, @y), **@options ) end |
- (Path) to_simple_path(fn = nil)
Flatten the circle into many small straight line segments.
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/vector_salad/standard_shapes/circle.rb', line 57 def to_simple_path(fn = nil) fn ||= (@radius * 4).ceil nodes = [] arc = (2.0 * Math::PI) / fn fn.times do |t| a = arc * t x = @radius * Math.cos(a) + @x y = @radius * Math.sin(a) + @y nodes << N.n(x, y) end Path.new(*nodes, **@options) end |
- (Object) to_svg
Export the shape to an svg string
102 103 104 105 106 |
# File 'lib/vector_salad/exporters/svg_exporter.rb', line 102 def to_svg svg = "<circle cx=\"#{at[0]}\" cy=\"#{at[1]}\" r=\"#{radius}\"" svg << Exporters::SvgExporter.(@options) svg << "/>" end |