home
history
links from
printable
login


3D Vectors Script Webb  all rights reserved  ©2004

Author  Mike Williams
Email: mrw@netcomuk.co.uk
Webpage:
Summary: Basic 3D vector functionality.
Instructions: This script supports 2 vectors, A and B. A vector is defined using the top 3 stack values in reverse order (z is top of the stack, y is next, then x)  use >A to define A and >B to define B. A> and B> put the current vector values on the stack. A<>B swaps the values of A and B.
All vector operators leave their results on the stack to be then stored or cleared.
Binary operators that are are A+B (addition), AB (subtraction), A.B (dot product) and AxB (cross product).
Unary operators are A (length), a^ (unit vector, or direction cosines), and s.A (scaling).
Clear removes 3 values from the stack as a quick way of removing a vector's values.
Code:
RPN.2.f+3
\V1.0  nuff said
\V1.1  test for 0 length in unit vec
\V1.2  fix cross product
\Get & put A & B vectors
[a]xaxbxc;
[b]xdxexf;
[x]XcXbXa;
[y]XfXeXd;
\Duplicate a vector
[d]g3g3g3;
\Vector functions
[l]r32Pr32Pr32P++s; \length
[s]g1r5*r2g1r5*r2r4*; \scale
[t]r6r4+r5r4+r4r4+; \sum
[u]r6r4r5r4r4r4; \difference
[v]r6r4*r5r4*r4r4*++; \dot product
"3D Vector"
"A+B: vector sum" CaCbCt;
"AB: vector difference" CaCbCu;
"A.B: vector dot product" CaCbCv;
"AxB: vector product" xbxf*xcxe*xcxd*xaxf*xaxe*xbxd*;
~
"A: vector length" CaCl;
"a^: unit vector" CaClg10=0(d1D'Zero length vector!OK'd1:Car4tCs);
"s.A: scale vector" ?1Car4Cs;
"A<>B: swap vectors" CaCbCxCy;
~
">A: get A from stack" ?3Cx;
">B: get B from stack" ?3Cy;
"A>: put A on stack" Ca;
"B>: put B on stack" Cb;
~
"Clear: pop vector values" ?3d1d1d1;

