Class: DYI::Shape::Marker

Inherits:
Element
  • Object
show all
Defined in:
lib/dyi/shape/marker.rb

Overview

Marker object represents a symbol at One or more vertices of the lines.

Marker provides some pre-defined shapes and a custom marker defined freely.

Since:

  • 1.2.0

Constant Summary collapse

@@predefined_markers =

Since:

  • 1.2.0

{
          :circle => {
:view_box => "-1 -1 2 2",
:magnify => 1.0,
:creator => proc{|painting, direction|
  Shape::Circle.new([0, 0], 1, :painting => painting)
}},
          :triangle => {
:view_box => "-1 -1 2 2",
:magnify => 1.1954339629,
:creator => proc{|painting, direction|
  case direction
  when :to_end
    shape = Shape::Polygon.new([1, 0], :painting => painting)
    shape.line_to([-0.5, 0.8660254038], [-0.5, -0.8660254038])
  when :to_start
    shape = Shape::Polygon.new([-1, 0], :painting => painting)
    shape.line_to([0.5, -0.8660254038], [0.5, 0.8660254038])
  else
    shape = Shape::Polygon.new([0, -1], :painting => painting)
    shape.line_to([0.8660254038, 0.5], [-0.8660254038, 0.5])
  end
  shape
}},
          :square => {
:view_box => "-1 -1 2 2",
:magnify => 0.8862269255,
:creator => proc{|painting, direction|
  Shape::Rectangle.new([-1, -1], 2, 2, :painting => painting)
}},
          :rhombus => {
:view_box => "-1 -1 2 2",
:magnify => 1.2533141373,
:creator => proc{|painting, direction|
  shape = Shape::Polygon.new([1, 0], :painting => painting)
  shape.line_to([0, 1], [-1, 0], [0, -1])
  shape
}},
          :pentagon => {
:view_box => "-1 -1 2 2",
:magnify => 1.1494809262,
:creator => proc{|painting, direction|
  case direction
  when :to_end
    shape = Shape::Polygon.new([1, 0], :painting => painting)
    shape.line_to([0.3090169944, 0.9510565163],
                  [-0.8090169944, 0.5877852523],
                  [-0.8090169944, -0.5877852523],
                  [0.3090169944, -0.9510565163])
  when :to_start
    shape = Shape::Polygon.new([-1, 0], :painting => painting)
    shape.line_to([-0.3090169944, -0.9510565163],
                  [0.8090169944, -0.5877852523],
                  [0.8090169944, 0.5877852523],
                  [-0.3090169944, 0.9510565163])
  else
    shape = Shape::Polygon.new([0, -1], :painting => painting)
    shape.line_to([0.9510565163, -0.3090169944],
                  [0.5877852523, 0.8090169944],
                  [-0.5877852523, 0.8090169944],
                  [-0.9510565163, -0.3090169944])
  end
  shape
}},
          :hexagon => {
:view_box => "-1 -1 2 2",
:magnify => 1.0996361108,
:creator => proc{|painting, direction|
  case direction
  when :to_end, :to_start
    shape = Shape::Polygon.new([1, 0], :painting => painting)
    shape.line_to([0.5, 0.8660254038],
                  [-0.5, 0.8660254038],
                  [-1, 0],
                  [-0.5, -0.8660254038],
                  [0.5, -0.8660254038])
  else
    shape = Shape::Polygon.new([0, -1], :painting => painting)
    shape.line_to([0.8660254038, -0.5],
                  [0.8660254038, 0.5],
                  [0, 1],
                  [-0.8660254038, 0.5],
                  [-0.8660254038, -0.5])
  end
  shape
}}}

Constants inherited from Element

Element::ID_REGEXP

Instance Attribute Summary collapse

Attributes inherited from Element

#description, #title

Instance Method Summary collapse

Methods inherited from Element

#has_uri_reference?, #id, #id=, #include_external_file?, #inner_id

Constructor Details

#initialize(marker_type, options = {}) ⇒ Marker #initialize(shapes, options = {}) ⇒ Marker

Returns a new instance of Marker.

Overloads:

  • #initialize(marker_type, options = {}) ⇒ Marker

    Creates a new pre-defined marker.

    Parameters:

    • marker_type (Symbol)

      a type of the marker. Specifies the following: :circle, :triangle, :inverted_triangle, :square, :rhombus, :inverted_pentagon, :hexagon

    Options Hash (options):

    • :size (Number)

      size of the marker. Specifies the relative size to line width

    • :painting (Painting)

      painting of the marker

    • :orient (Number, "auto")

      how the marker is rotated. Specifies a rotated angle or "auto". "auto" means the marker rotate the orientation of the line

    • :direction (Symbol)

      a direction of the marker. This option is valid if option :orient value is "auto". Specifies the following: :to_start, :to_end

  • #initialize(shapes, options = {}) ⇒ Marker

    Creates a new custom marker.

    Parameters:

    Options Hash (options):

    • :units (String)

      a setting to define the coordinate system of the custom marker.

    • :view_box (String)
    • :ref_point (Coordinate)
    • :width (Length)
    • :height (Length)
    • :orient (Number, nil)

Raises:

  • (ArgumentError)

Since:

  • 1.2.0



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/dyi/shape/marker.rb', line 197

def initialize(shape, options={})
  case shape
  when Symbol
    inverted = !!(shape.to_s =~ /^inverted_/)
    marker_source = @@predefined_markers[inverted ? $'.to_sym : shape]
    raise ArgumentError, "`#{shape}' is unknown marker" unless marker_source
    @ref_point = Coordinate::ZERO
    if options[:orient] == 'auto'
      direction = (inverted ^ (options[:direction] == :to_start)) ? :to_start : :to_end
      @orient = 'auto'
    else
      direction = nil
      @orient = (options[:orient] || 0) + (inverted ? 180 : 0)
    end
    @shapes = [marker_source[:creator].call(options[:painting] || {}, direction)]
    @view_box = marker_source[:view_box]
    @marker_units = 'strokeWidth'
    @width = @height = Length.new(options[:size] || 3) * marker_source[:magnify]
  when Shape::Base, Array
    @ref_point = options[:ref_point] || Coordinate::ZERO
    @shapes = shape.is_a?(Shape::Base) ? [shape] : shape
    @view_box = options[:view_box] || "0 0 3 3"
    @marker_units = options[:units] || 'strokeWidth'
    @width = Length.new(options[:width] || 3)
    @height = Length.new(options[:height] || 3)
    @orient = options[:orient]
  else
    raise ArgumentError, "argument is a wrong class"
  end
end

Instance Attribute Details

#canvasObject

Since:

  • 1.2.0



31
32
33
# File 'lib/dyi/shape/marker.rb', line 31

def canvas
  @canvas
end

#heightObject

Since:

  • 1.2.0



31
32
33
# File 'lib/dyi/shape/marker.rb', line 31

def height
  @height
end

#marker_unitsObject

Since:

  • 1.2.0



31
32
33
# File 'lib/dyi/shape/marker.rb', line 31

def marker_units
  @marker_units
end

#orientObject

Since:

  • 1.2.0



31
32
33
# File 'lib/dyi/shape/marker.rb', line 31

def orient
  @orient
end

#ref_pointObject

Since:

  • 1.2.0



31
32
33
# File 'lib/dyi/shape/marker.rb', line 31

def ref_point
  @ref_point
end

#shapesObject

Since:

  • 1.2.0



31
32
33
# File 'lib/dyi/shape/marker.rb', line 31

def shapes
  @shapes
end

#view_boxObject

Since:

  • 1.2.0



31
32
33
# File 'lib/dyi/shape/marker.rb', line 31

def view_box
  @view_box
end

#widthObject

Since:

  • 1.2.0



31
32
33
# File 'lib/dyi/shape/marker.rb', line 31

def width
  @width
end

Instance Method Details

#child_elementsObject

Since:

  • 1.2.0



236
237
238
# File 'lib/dyi/shape/marker.rb', line 236

def child_elements
  @shapes
end

#set_canvas(canvas) ⇒ Object

Since:

  • 1.2.0



228
229
230
231
232
233
234
# File 'lib/dyi/shape/marker.rb', line 228

def set_canvas(canvas)
  if @canvas.nil?
    @canvas = canvas
  elsif @canvas != canvas
    raise Arguments, "the clipping is registered to another canvas"
  end
end

#write_as(formatter, io = $>) ⇒ Object

Since:

  • 1.2.0



240
241
242
# File 'lib/dyi/shape/marker.rb', line 240

def write_as(formatter, io=$>)
  formatter.write_marker(self, io)
end