Class: Archimate::Svg::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/archimate/svg/path.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Path

Returns a new instance of Path.



8
9
10
# File 'lib/archimate/svg/path.rb', line 8

def initialize(connection)
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/archimate/svg/path.rb', line 6

def connection
  @connection
end

Instance Method Details

#dObject

builds the line coordinates for the path rough drawing is the point at center of first element, point of each bendpoint, and center of end element First naive implementation if left/right range of both overlap, use centerpoint of overlap range as x val if top/bottom range of both overlap, use centerpoint of overlap range as y val



46
47
48
# File 'lib/archimate/svg/path.rb', line 46

def d
  [move_to(points.first)].concat(points[1..-1].map { |pt| line_to(pt) }).join(" ")
end

#lengthObject

Returns Float length of this path.

Returns:

  • Float length of this path



13
14
15
# File 'lib/archimate/svg/path.rb', line 13

def length
  segment_lengths.reduce(0) { |total, length| total + length }
end

#midpointObject

Returns Point mid-point on Path.

Returns:

  • Point mid-point on Path



18
19
20
# File 'lib/archimate/svg/path.rb', line 18

def midpoint
  point(0.5)
end

#point(fraction) ⇒ Object

Returns Point at the given percent along line between start and end.

Parameters:

  • fraction

    Float 0.0-1.0

Returns:

  • Point at the given percent along line between start and end



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/archimate/svg/path.rb', line 24

def point(fraction)
  length_from_start = length * fraction
  segments.each do |a, b|
    seg_length = b - a
    if seg_length >= length_from_start
      pct = length_from_start / seg_length
      return Point.new(
        a.x + ((b.x - a.x) * pct),
        a.y + ((b.y - a.y) * pct)
      )
    else
      length_from_start -= seg_length
    end
  end
  Point.new(0.0, 0.0)
end

#pointsObject



50
51
52
# File 'lib/archimate/svg/path.rb', line 50

def points
  @points ||= calc_points
end

#segment_lengthsObject

Returns the lengths of each segment of the line



59
60
61
# File 'lib/archimate/svg/path.rb', line 59

def segment_lengths
  segments.map { |a, b| b - a }
end

#segmentsObject



54
55
56
# File 'lib/archimate/svg/path.rb', line 54

def segments
  (0..points.length - 2).map { |i| [points[i], points[i + 1]] }
end