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)

Instance Method Summary (collapse)

Constructor Details

- (Circle) initialize(radius, **options)

Create a perfectly round circle.

Examples:

new(100)

Parameters:

  • radius (Pos)
  • options ({})

    +{}+



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

- (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.

Parameters:

  • x (Coord)

    +Num; a coordinate+

  • y (Coord)

    +Num; a coordinate+

Returns:

  • (Any)

- (Coords) at Originally defined in module Mixins::At

Get the x, y coordinates of the shape.

Returns:

  • (Coords)

    +[Num, Num] an x,y coordinate array+

- (Any) at=(at) Originally defined in module Mixins::At

Set the x, y coordinates of the shape directly.

Parameters:

  • at (Coords)

    +[Num, Num] an x,y coordinate array+

Returns:

  • (Any)

- (Any) move(x, y) Originally defined in module Mixins::At

Move the shape relatively.

Parameters:

  • x (Coord)

    +Num; a coordinate+

  • y (Coord)

    +Num; a coordinate+

Returns:

  • (Any)

- (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.

Parameters:

  • fn (Maybe[PolySides]) (defaults to: nil)

    The number of segments

Returns:



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(@options)
  svg << "/>"
end