Class: DYI::Shape::Rectangle

Inherits:
Base show all
Defined in:
lib/dyi/shape/base.rb

Overview

The rectangle in the vector image

Since:

  • 0.0.0

Direct Known Subclasses

Image

Constant Summary

Constants inherited from GraphicalElement

GraphicalElement::CLASS_REGEXP

Constants inherited from Element

Element::ID_REGEXP

Instance Attribute Summary

Attributes inherited from Base

#anchor_href, #anchor_target, #attributes, #clipping, #parent

Attributes inherited from GraphicalElement

#css_class

Attributes inherited from Element

#description, #title

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#add_animation, #add_painting_animation, #add_transform_animation, #animate?, #animations, #clear_clipping, #draw_on, #has_marker?, #has_uri_reference?, #root_element?, #root_node?, #rotate, #scale, #set_clipping, #set_clipping_shapes, #set_event, #skew_x, #skew_y, #transform, #translate

Methods inherited from GraphicalElement

#add_css_class, #add_event_listener, #css_classes, #event_listeners, #event_target?, #remove_css_class, #remove_event_listener, #set_event, #to_reused_source

Methods inherited from Element

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

Constructor Details

#initialize(left_top, width, height, options = {}) ⇒ Rectangle

Returns a new instance of Rectangle.

Parameters:

  • left_top (Coordinate)

    a left-top coordinate of the rectangle

  • width (Length)

    width of the rectangle

  • heigth (Length)

    heigth of the rectangle

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :painting (Painting)

    painting status of this shape

  • :rx (Length)

    the x-axis radius of the ellipse for rounded the rectangle

  • :ry (Length)

    the y-axis radius of the ellipse for rounded the rectangle

  • :description (String)

    the description of this shape

  • :title (String)

    the title of this shape

Since:

  • 0.0.0



419
420
421
422
423
424
425
426
427
428
# File 'lib/dyi/shape/base.rb', line 419

def initialize(left_top, width, height, options={})
  width = Length.new(width)
  height = Length.new(height)
  @lt_pt = Coordinate.new(left_top)
  @lt_pt += Coordinate.new(width, 0) if width < Length::ZERO
  @lt_pt += Coordinate.new(0, height) if height < Length::ZERO
  @width = width.abs
  @height = height.abs
  @attributes = init_attributes(options)
end

Class Method Details

.create_on_corner(top, right, bottom, left, options = {}) ⇒ Rectangle

Create a new instance of Rectangle.

Parameters:

  • top (Length)

    a y-axis coordinate of the top

  • right (Length)

    a x-axis coordinate of the right side

  • bottom (Length)

    a y-axis coordinate of the bottom

  • left (Length)

    a x-axis coordinate of the left side

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :painting (Painting)

    painting status of the rectangle

  • :rx (Length)

    the x-axis radius of the ellipse for rounded the rectangle

  • :ry (Length)

    the y-axis radius of the ellipse for rounded the rectangle

Returns:

  • (Rectangle)

    a new instance of Rectangle

Since:

  • 0.0.0



496
497
498
499
500
501
# File 'lib/dyi/shape/base.rb', line 496

def create_on_corner(top, right, bottom, left, options={})
  left_top = Coordinate.new([left, right].min, [top, bottom].min)
  width = (Length.new(right) - Length.new(left)).abs
  height = (Length.new(bottom) - Length.new(top)).abs
  new(left_top, width, height, options)
end

.create_on_width_height(left_top, width, height, options = {}) ⇒ Rectangle

Create a new instance of Rectangle.

Parameters:

  • left_top (Coordinate)

    a coordinate of a corner of the rectangle

  • width (Length)

    width of the rectangle

  • heigth (Length)

    heigth of the rectangle

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :painting (Painting)

    painting status of the shape

  • :rx (Length)

    the x-axis radius of the ellipse for rounded the rectangle

  • :ry (Length)

    the y-axis radius of the ellipse for rounded the rectangle

Returns:

  • (Rectangle)

    a new instance of Rectangle

Since:

  • 0.0.0



481
482
483
# File 'lib/dyi/shape/base.rb', line 481

def create_on_width_height(left_top, width, height, options={})
  new(left_top, width, height, options)
end

Instance Method Details

#bottomLength

Returns a y-axis coordinate of the bottom of the rectangle.

Returns:

  • (Length)

    a y-axis coordinate of the bottom

Since:

  • 0.0.0



450
451
452
# File 'lib/dyi/shape/base.rb', line 450

def bottom
  @lt_pt.y + height
end

#centerCoordinate

Returns a coordinate of the center of the rectangle.

Returns:

Since:

  • 0.0.0



456
457
458
# File 'lib/dyi/shape/base.rb', line 456

def center
  @lt_pt + Coordinate.new(width.quo(2), height.quo(2))
end

#leftLength

Returns a x-axis coordinate of the left side of the rectangle.

Returns:

  • (Length)

    the x-axis coordinate of the left side

Since:

  • 0.0.0



432
433
434
# File 'lib/dyi/shape/base.rb', line 432

def left
  @lt_pt.x
end

#rightLength

Returns a x-axis coordinate of the right side of the rectangle.

Returns:

  • (Length)

    the x-axis coordinate of the right side

Since:

  • 0.0.0



438
439
440
# File 'lib/dyi/shape/base.rb', line 438

def right
  @lt_pt.x + width
end

#topLength

Returns a y-axis coordinate of the top of the rectangle.

Returns:

  • (Length)

    a y-axis coordinate of the top

Since:

  • 0.0.0



444
445
446
# File 'lib/dyi/shape/base.rb', line 444

def top
  @lt_pt.y
end

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

Writes the shape on io object.

Parameters:

  • formatter (Formatter::Base)

    an object that defines the image format

  • io (IO) (defaults to: $>)

    an io to be written

Since:

  • 0.0.0



463
464
465
# File 'lib/dyi/shape/base.rb', line 463

def write_as(formatter, io=$>)
  formatter.write_rectangle(self, io, &(block_given? ? Proc.new : nil))
end