Class: VectorSalad::StandardShapes::Circle

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

Attributes inherited from BasicShape

#options

Instance Method Summary collapse

Methods included from Mixins::At

#[], #at, #at=, #move

Methods inherited from BasicShape

#flip, #flip_x, #flip_y, #jitter, #move, #rotate, #scale, #to_a, #to_bezier_path, #to_cubic_path, #to_multi_path

Constructor Details

#initialize(radius, **options) ⇒ Circle

Returns a new instance of Circle.



17
18
19
20
21
22
# File 'lib/vector_salad/standard_shapes/circle.rb', line 17

def initialize(radius, **options)
  @options = options
  @radius = radius
  @x, @y = 0, 0
  self
end

Instance Attribute Details

#radiusObject (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

#to_pathObject

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
# File 'lib/vector_salad/standard_shapes/circle.rb', line 25

def to_path
  # http://stackoverflow.com/a/13338311
  # c = 4 * (Math.sqrt(2) - 1) / 3
  # c = 0.5522847498307936
  #
  # 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

#to_simple_path(fn = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vector_salad/standard_shapes/circle.rb', line 55

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

#to_svgObject

Export the shape to an svg string



103
104
105
106
107
# File 'lib/vector_salad/exporters/svg_exporter.rb', line 103

def to_svg
  svg = "<circle cx=\"#{at[0]}\" cy=\"#{at[1]}\" r=\"#{radius}\""
  svg << VectorSalad::Exporters::SvgExporter.options(@options)
  svg << "/>"
end