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)

Instance Method Summary (collapse)

Constructor Details

- (Oval) initialize(width, height, **options)

Create an oval.

Examples:

new(100, 200)

Parameters:

  • width (Pos)
  • height (Pos)
  • options ({})

    +{}+



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

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

Instance Attribute Details

- (Object) height (readonly)

Returns the value of attribute height



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

def height
  @height
end

- (Object) options Originally defined in class BasicShape

Returns the value of attribute options

- (Object) width (readonly)

Returns the value of attribute width



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

def width
  @width
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



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/oval.rb', line 26

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

- (Object) to_svg

Export the shape to an svg string



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

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