Re: Looking for 3-D vector routines ( dot & cross product, ..) for arrays of [x,y,z]'s [message #29598] |
Tue, 05 March 2002 11:29 |
James Kuyper
Messages: 425 Registered: March 2000
|
Senior Member |
|
|
William Gallery wrote:
> I need a set of routines that operate on ***arrays*** of 3-D vectors
> (x, y, z), including:
> length of a vector
> dot- and cross- products
> included angle between two vectors
>
>
> Before I write my own, are there any out there already?
There aren't any such routines to work on arrays of vectors, but the
equivalent code is fairly simple:
seed = 12345L
a = randomn(seed, 3, 5)
b = randomn(seed, 3, 5)
dot_product = total(a*b,1)
lengtha = total(a^2,1)^0.5
lengthb = total(b^2,1)^0.5
; Warning: not protected against division by 0:
angle = acos(dot_product/(lengtha*lengthb)
; If calculating only the angle, this is slightly faster
; since there's only one sqrt() per row
angle = acos(dot_product/(total(a^2,1)*total(b^2,1))^0.5)
cross_prod = dblarr(3,5)
cross_prod(0,*) = a(1,*)*b(2,*)-a(2,*)*b(1,*)
cross_prod(1,*) = -a(0,*)*b(2,*)+a(2,*)*b(0,*)
cross_prod(2,*) = a(0,*)*b(1,*)-a(1,*)*b(0,*)
|
|
|