Class: Vector

Inherits:
Object
  • Object
show all
Defined in:
lib/euclidean/vector.rb

Overview

Monkeypatch Vector to overcome some deficiencies

Direct Known Subclasses

Euclidean::Point, Euclidean::Size

Constant Summary collapse

X =
Vector[1,0,0]
Y =
Vector[0,1,0]
Z =
Vector[0,0,1]

Unary operators collapse

Instance Method Summary collapse

Instance Method Details

#+@Object



10
11
12
# File 'lib/euclidean/vector.rb', line 10

def +@
  self
end

#-@Object



14
15
16
# File 'lib/euclidean/vector.rb', line 14

def -@
  Vector[*(@elements.map {|e| -e })]
end

#cross(other) ⇒ Vector Also known as: **

Cross-product of two Vectors

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/euclidean/vector.rb', line 21

def cross(other)
  Vector.Raise ErrDimensionMismatch unless @elements.size == other.size

  case @elements.size
  when 0 then raise ArgumentError, "Can't multply zero-length Vectors"
  when 1 then @elements.first * other.first
  when 2 then @elements.first * other[1] - @elements.last * other.first
  when 3 then Vector[ @elements[1]*other[2] - @elements[2]*other[1],
		@elements[2]*other[0] - @elements[0]*other[2],
		@elements[0]*other[1] - @elements[1]*other[0]]
  end
end