Class: Graphdown::Edge

Inherits:
Object
  • Object
show all
Defined in:
lib/graphdown/edge.rb

Defined Under Namespace

Classes: Vector

Constant Summary collapse

DIRECTION =
[:forward, :backward, :two_way].freeze
ARROW_SIDE_LENGTH =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin_node, target_node, direction = :forward) ⇒ Edge

Returns a new instance of Edge.



11
12
13
14
15
16
17
# File 'lib/graphdown/edge.rb', line 11

def initialize(origin_node, target_node, direction = :forward)
  @origin_node = origin_node
  @target_node = target_node
  @direction = DIRECTION.include?(direction) ? direction : :forward
  @origin = Point.new
  @target = Point.new
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



6
7
8
# File 'lib/graphdown/edge.rb', line 6

def direction
  @direction
end

#originObject

Returns the value of attribute origin.



5
6
7
# File 'lib/graphdown/edge.rb', line 5

def origin
  @origin
end

#targetObject

Returns the value of attribute target.



5
6
7
# File 'lib/graphdown/edge.rb', line 5

def target
  @target
end

Instance Method Details

#arrow_dObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/graphdown/edge.rb', line 47

def arrow_d
  ratio = ARROW_SIDE_LENGTH / length

  v1 = Graphdown::Edge::Vector.new(@target.x - @origin.x, @target.y - @origin.y)
  p1 = v1.scale(ratio).rotate(Math::PI / 6 * 5).point

  v2 = Graphdown::Edge::Vector.new(@target.x - @origin.x, @target.y - @origin.y)
  p2 = v2.scale(ratio).rotate(-Math::PI / 6 * 5).point

  p1.x += @target.x
  p1.y += @target.y
  p2.x += @target.x
  p2.y += @target.y

  "M #{@target.x} #{@target.y} L #{p1.x} #{p1.y} L #{p2.x} #{p2.y} Z"
end

#backward?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/graphdown/edge.rb', line 23

def backward?
  @direction == :backward || @direction == :two_way
end

#childObject



35
36
37
38
39
40
41
# File 'lib/graphdown/edge.rb', line 35

def child
  case @direction
  when :forward  then @target_node
  when :backward then @origin_node
  when :two_way  then @target_node
  end
end

#forward?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/graphdown/edge.rb', line 19

def forward?
  @direction == :forward || @direction == :two_way
end

#line_dObject



43
44
45
# File 'lib/graphdown/edge.rb', line 43

def line_d
  "M #{@origin.x} #{@origin.y} L #{@target.x} #{@target.y}"
end

#parentObject



27
28
29
30
31
32
33
# File 'lib/graphdown/edge.rb', line 27

def parent
  case @direction
  when :forward  then @origin_node
  when :backward then @target_node
  when :two_way  then @origin_node
  end
end

#reverse_arrow_dObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/graphdown/edge.rb', line 64

def reverse_arrow_d
  ratio = ARROW_SIDE_LENGTH / length

  v1 = Graphdown::Edge::Vector.new(@origin.x - @target.x, @origin.y - @target.y)
  p1 = v1.scale(ratio).rotate(Math::PI / 6 * 5).point

  v2 = Graphdown::Edge::Vector.new(@origin.x - @target.x, @origin.y - @target.y)
  p2 = v2.scale(ratio).rotate(-Math::PI / 6 * 5).point

  p1.x += @origin.x
  p1.y += @origin.y
  p2.x += @origin.x
  p2.y += @origin.y

  "M #{@origin.x} #{@origin.y} L #{p1.x} #{p1.y} L #{p2.x} #{p2.y} Z"
end