class Graphdown::Edge

Constants

ARROW_SIDE_LENGTH
DIRECTION

Attributes

direction[R]
origin[RW]
target[RW]

Public Class Methods

new(origin_node, target_node, direction = :forward) click to toggle source
# 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

Public Instance Methods

arrow_d() click to toggle source
# 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?() click to toggle source
# File lib/graphdown/edge.rb, line 23
def backward?
  @direction == :backward || @direction == :two_way
end
child() click to toggle source
# 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?() click to toggle source
# File lib/graphdown/edge.rb, line 19
def forward?
  @direction == :forward || @direction == :two_way
end
line_d() click to toggle source
# File lib/graphdown/edge.rb, line 43
def line_d
  "M #{@origin.x} #{@origin.y} L #{@target.x} #{@target.y}"
end
parent() click to toggle source
# 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_d() click to toggle source
# 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

Private Instance Methods

length() click to toggle source
# File lib/graphdown/edge.rb, line 83
def length
  dx = @target.x - @origin.x
  dy = @target.y - @origin.y
  Math.sqrt(dx ** 2 + dy ** 2)
end