Class: Graphdown::Edge::Vector

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

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Vector

Returns a new instance of Vector.



90
91
92
93
# File 'lib/graphdown/edge.rb', line 90

def initialize(x, y)
  @v = Matrix[[x, y]]
  @matrices = []
end

Instance Method Details

#pointObject



95
96
97
98
99
# File 'lib/graphdown/edge.rb', line 95

def point
  @matrices.each { |matrix| @v *= matrix }
  x, y = @v.row(0).to_a
  Point.new(x, y)
end

#rotate(radian) ⇒ Object



110
111
112
113
114
115
# File 'lib/graphdown/edge.rb', line 110

def rotate(radian)
  rotation_matrix = Matrix[[Math.cos(radian), Math.sin(radian)],
                           [-Math.sin(radian), Math.cos(radian)]]
  @matrices << rotation_matrix
  self
end

#scale(ratio) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/graphdown/edge.rb', line 101

def scale(ratio)
  unit_vector = @v.row(0).normalize
  nx, ny = unit_vector.map(&:to_f).to_a
  scaling_matrix = Matrix[[1 + (ratio - 1) * nx ** 2, (ratio - 1) * nx * ny],
                          [(ratio - 1) * nx * ny, 1 + (ratio - 1) * ny ** 2]]
  @matrices << scaling_matrix
  self
end