Sha256: c419aeed4294762751351ab49a22a8892419a585aa8424cff0a9d958d03517a1

Contents?: true

Size: 849 Bytes

Versions: 13

Compression:

Stored size: 849 Bytes

Contents

require 'matrix'

# Monkeypatch Vector to overcome some deficiencies
class Vector
    X = Vector[1,0,0]
    Y = Vector[0,1,0]
    Z = Vector[0,0,1]

# @group Unary operators
    def +@
	self
    end

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

    # Cross-product of two {Vector}s
    # @return [Vector]
    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
    alias ** cross
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
geometry-6.6 lib/geometry/vector.rb
geometry-6.5 lib/geometry/vector.rb
geometry-6.4 lib/geometry/vector.rb
geometry-6.3 lib/geometry/vector.rb
geometry-6.2 lib/geometry/vector.rb
geometry-in-ruby-0.0.2 lib/geometry/vector.rb
geometry-in-ruby-0.0.1 lib/geometry/vector.rb
aurora-geometry-0.0.5 lib/geometry/vector.rb
aurora-geometry-0.0.4 lib/aurora-geometry/vector.rb
aurora-geometry-0.0.3 lib/aurora-geometry/vector.rb
aurora-geometry-0.0.2 lib/geometry/vector.rb
geometry-6.1 lib/geometry/vector.rb
geometry-6 lib/geometry/vector.rb