module Vector

Public Instance Methods

/(scalar) click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 36
def /(scalar)
  map { |value| value / scalar }
end
angle() click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 52
def angle
  Math::atan2 at(1), at(0)
end
cross(other) click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 72
def cross(other)
  perp.dot other
end
dot(other) click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 28
def dot(other)
  [self, other].transpose.map { |values| values.inject(:*) }.inject(:+)
end
minus(other) click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 24
def minus(other)
  [self, other].transpose.map { |values| values.inject(:-) }
end
negate() click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 40
def negate
  map { |value| -value }
end
norm() click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 56
def norm
  Math::sqrt(dot self)
end
normalised() click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 60
def normalised
  self / norm
end
perp() click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 68
def perp
  [-self[1], self[0]]
end
plus(other) click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 20
def plus(other)
  [self, other].transpose.map { |values| values.inject(:+) }
end
proj(other) click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 64
def proj(other)
  dot(other) / other.norm
end
rotate_by(angle) click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 2
def rotate_by(angle)
  cos = Math::cos(angle)
  sin = Math::sin(angle)
  [self[0] * cos - self[1] * sin, self[0] * sin + self[1] * cos]
end
rotate_by!(angle) click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 8
def rotate_by!(angle)
  replace rotate_by(angle)
end
rotate_by_degrees(angle) click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 12
def rotate_by_degrees(angle)
  rotate_by(angle * Math::PI / 180.0)
end
rotate_by_degrees!(angle) click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 16
def rotate_by_degrees!(angle)
  replace rotate_by_degrees(angle)
end
times(scalar) click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 32
def times(scalar)
  map { |value| value * scalar }
end
to_d() click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 44
def to_d
  map(&:to_d)
end
to_f() click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 48
def to_f
  map(&:to_f)
end
within?(polygon) click to toggle source
# File lib/nswtopo/geometry/vector.rb, line 76
def within?(polygon)
  polygon.map do |point|
    point.minus self
  end.ring.inject(0) do |winding, (p0, p1)|
    case
    when p1[1] > 0 && p0[1] <= 0 && p0.minus(p1).cross(p0) >= 0 then winding + 1
    when p0[1] > 0 && p1[1] <= 0 && p1.minus(p0).cross(p0) >= 0 then winding - 1
    when p0[1] == 0 && p1[1] == 0 && p0[0] >= 0 && p1[0] < 0 then winding + 1
    when p0[1] == 0 && p1[1] == 0 && p1[0] >= 0 && p0[0] < 0 then winding - 1
    else winding
    end
  end != 0
end