Class: VectorSalad::StandardShapes::Oval

Inherits:
BasicShape
  • Object
show all
Includes:
Mixins::At
Defined in:
lib/vector_salad/standard_shapes/oval.rb,
lib/vector_salad/exporters/svg_exporter.rb

Overview

Oval 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, #to_simple_path

Constructor Details

#initialize(width, height, **options) ⇒ Oval

Returns a new instance of Oval.



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

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

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



10
11
12
# File 'lib/vector_salad/standard_shapes/oval.rb', line 10

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



10
11
12
# File 'lib/vector_salad/standard_shapes/oval.rb', line 10

def width
  @width
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
50
# File 'lib/vector_salad/standard_shapes/oval.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
  dw = c * @width
  dh = c * @height
  Path.new(
    N.n(@x + @width, @y),
    N.c(@x + @width, @y + dh),
    N.c(@x + dw, @y + @height),
    N.n(@x, @y + @height),
    N.c(@x - dw, @y + @height),
    N.c(@x - @width, @y + dh),
    N.n(@x - @width, @y),
    N.c(@x - @width, @y - dh),
    N.c(@x - dw, @y - @height),
    N.n(@x, @y - @height),
    N.c(@x + dw, @y - @height),
    N.c(@x + @width, @y - dh),
    N.n(@x + @width, @y),
    @options
  )
end

#to_svgObject

Export the shape to an svg string



112
113
114
115
116
117
# File 'lib/vector_salad/exporters/svg_exporter.rb', line 112

def to_svg
  svg = "<ellipse cx=\"#{at[0]}\" cy=\"#{at[1]}\""
  svg << " rx=\"#{width}\" ry=\"#{height}\""
  svg << VectorSalad::Exporters::SvgExporter.options(@options)
  svg << "/>"
end