Module: Sevgi::Geometry::Operation::Align

Extended by:
Align
Included in:
Align
Defined in:
lib/sevgi/geometry/operation/align.rb

Overview

Alignment operation implementation.

Instance Method Summary collapse

Instance Method Details

#align(element, other, alignment = :center) ⇒ Sevgi::Geometry::Element

Returns an element translated to align with another element.

Parameters:

Returns:

Raises:



17
18
19
20
21
# File 'lib/sevgi/geometry/operation/align.rb', line 17

def align(element, other, alignment = :center)
  offset = alignment(element, other, alignment)

  element.translate(offset.x, offset.y)
end

#alignment(element, other, alignment = :center) ⇒ Sevgi::Geometry::Point

Returns the offset needed to align one element with another.

Parameters:

Returns:

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sevgi/geometry/operation/align.rb', line 30

def alignment(element, other, alignment = :center)
  OperationInapplicableError.("Not a Geometric Element: #{other}") unless other.is_a?(Element)

  this, that = element.box, other.box

  case alignment
  when :center
    Point[
      that.position.x + ((that.width - this.width) / 2.0) - this.position.x,
      that.position.y + ((that.height - this.height) / 2.0) - this.position.y
    ]
  when :left
    Point[that.position.x - this.position.x, 0]
  when :right
    Point[(that.position.x + that.width) - (this.position.x + this.width), 0]
  when :top
    Point[0, that.position.y - this.position.y]
  when :bottom
    Point[0, (that.position.y + that.height) - (this.position.y + this.height)]
  else
    ArgumentError.("No such type of alignment: #{alignment}")
  end
end

#applicable?(element) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reports whether the alignment handler can operate on an element.

Parameters:

  • element (Object)

    candidate element

Returns:

  • (Boolean)


58
# File 'lib/sevgi/geometry/operation/align.rb', line 58

def applicable?(element) = element.respond_to?(:translate)