Class: VectorSalad::StandardShapes::Rect

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

Overview

and Square subclass

Direct Known Subclasses

Square

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

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

Create a rectangle with specified width and height.

Examples:

new(100, 200)

Parameters:

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

    +{}+



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

def initialize(width, height, **options)
  @width, @height = width, height
  @options = options
  @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/rect.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/rect.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
# File 'lib/vector_salad/standard_shapes/rect.rb', line 26

def to_path
  Path.new(
    N.n(@x, @y),
    N.n(@x, @y + @height),
    N.n(@x + @width, @y + @height),
    N.n(@x + @width, @y),
    **@options
  )
end

- (Object) to_svg

Export the shape to an svg string



121
122
123
124
125
126
# File 'lib/vector_salad/exporters/svg_exporter.rb', line 121

def to_svg
  svg = "<rect x=\"#{at[0]}\" y=\"#{at[1]}\""
  svg << " width=\"#{width}\" height=\"#{height}\""
  svg << Exporters::SvgExporter.options(@options)
  svg << "/>"
end