Class: Archimate::Svg::Path

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

Constant Summary collapse

LINE_CURVE_RADIUS =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Path

Returns a new instance of Path.



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

def initialize(connection)
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



10
11
12
# File 'lib/archimate/svg/path.rb', line 10

def connection
  @connection
end

Instance Method Details

#curve_segment(a, b, c) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/archimate/svg/path.rb', line 59

def curve_segment(a, b, c)
  pt1 = Segment.new(a, b).from_end(LINE_CURVE_RADIUS)
  pt2 = Segment.new(b, c).from_start(LINE_CURVE_RADIUS)
  [
    line_to(pt1),
    q_curve(b, pt2)
  ]
end

#dObject

New implementation of SVG d method for a set of points making smooth curves at each bendpoint

Given three points: a, b, c The result should be: (a is already part of the path -> first point is a move_to command) line_to(segment(a-b) - curve_radius (from end)) q_curve(b, segment(b-c) - curve_radius (from start))

For cases with more bendpoints (with values d, e, …) repeat the above section with c as the new a value (so then [c, d, e], [d, e, f], etc.)



48
49
50
51
52
53
54
55
56
57
# File 'lib/archimate/svg/path.rb', line 48

def d
  [move_to(points.first)]
    .concat(
      points
        .each_cons(3)
        .flat_map { |a, b, c| curve_segment(a, b, c) }
    )
    .concat([line_to(points.last)])
    .join(" ")
end

#lengthObject

Returns Float length of this path.

Returns:

  • Float length of this path



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

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

#midpointObject

Returns Point mid-point on Path.

Returns:

  • Point mid-point on Path



22
23
24
# File 'lib/archimate/svg/path.rb', line 22

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



28
29
30
31
32
33
34
35
# File 'lib/archimate/svg/path.rb', line 28

def point(fraction)
  length_from_start = length * fraction
  segments.each do |segment|
    return segment.from_start(length_from_start) if segment.length >= length_from_start
    length_from_start -= segment.length
  end
  Point.new(0.0, 0.0)
end

#pointsObject



68
69
70
# File 'lib/archimate/svg/path.rb', line 68

def points
  @points ||= calc_points
end

#segment_lengthsObject

Returns the lengths of each segment of the line



77
78
79
# File 'lib/archimate/svg/path.rb', line 77

def segment_lengths
  segments.map(&:length)
end

#segmentsObject



72
73
74
# File 'lib/archimate/svg/path.rb', line 72

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