Showing posts with label 3D vector transformation and rotation. Show all posts
Showing posts with label 3D vector transformation and rotation. Show all posts

Saturday, September 24, 2016

HP Prime: Mathematical Calculations with 3-Dimensional Vectors


HP Prime:  Mathematical Calculations with 3-Dimensional Vectors

Note: all examples are calculated in Degrees mode.

Rectangular to Spherical Coordinates

The program RECT2SPH converts the coordinates [x, y, z] to [r, θ, ϕ].

Syntax:  RECT2SPH([x, y, z])

HP Prime RECT2SPH:  Rectangular to Spherical Coordinates
EXPORT RECT2SPH(v)
BEGIN
// [x,y,z]→
LOCAL r,θ,φ,x,y,z;
x:=v(1); y:=v(2); z:=v(3);
r:=√(x^2+y^2+z^2);
θ:=ATAN(y/x);
φ:=ACOS(z/r);
RETURN [r,θ,φ];
END;

Example:  RECT2SPH([2, 3, 4]) return [5.38516480713, 56.309932474, 42.0311137741]

Spherical to Rectangular Coordinates

The program SPH2RECT converts the coordinates [r, θ,  φ] to [x, y, y].

Syntax:  SPH2RECT([r, θ,  φ])

HP Prime SPH2RECT:  Spherical Coordinates to Rectangular Coordinates
EXPORT SPH2RECT(v)
BEGIN
// [r,θ,φ]→
LOCAL r,θ,φ,x,y,z;
r:=v(1); θ:=v(2); φ:=v(3);
x:=r*COS(θ)*SIN(φ);
y:=r*SIN(θ)*SIN(φ);
z:=r*COS(φ);
RETURN [x,y,z];
END;

Example:  SPH2RECT([6, 30, 48]) returns [3.86149378532, 2.22943447643, 4.01478363815]

Linear Distance

The program LIN3DIST is the linear distance between two three-dimensional points.  The coordinates are Cartesian.  Enter each coordinate point separately. 

Syntax:  LIN3DIST(x1, x2, y1, y2, z1, z2)

HP Prime LIN3DIST:  Linear distance between coordinates
EXPORT LIN3DIST(x1,x2,y1,y2,z1,z2)
BEGIN
// linear distance
LOCAL d;
d:=√((x2-x1)^2+(y2-y1)^2
+(z2-z1)^2);
RETURN d;
END;

Example:  Find the linear distance between points (2,3,-7) and (-1,8,2).
Input:  LIN3DIST(2, -1, 3, 8, -7, 2) returns 10.7238052948.

Spherical Distance (Arc Length)

The program SPH3DIST is the spherical distance between two three-dimensional points that share the same radius.  This is similar to the great circle distance.

Syntax:  SPH3DIST(r, φ1, φ2 ,λ1 ,λ2)

HP Prime SPH3DIST:  Spherical distance between coordinates
EXPORT SPH3DIST(r,φ1,φ2,λ1,λ2)
BEGIN
// Spherical Distance
LOCAL d;
d:=ACOS(SIN(φ1)*SIN(φ2)+
COS(φ1)*COS(φ2)*COS(λ1-λ2));
d:=d*r;
RETURN d;
END;

Example:  Find the spherical distance between points φ1 = 40°, φ2 = 64°, λ1 = -18°, λ2 = 33°.  The radius is 14.

SPH3DIST(14, 40, 64, -18, 33) returns 519.226883434
   
Angle between Two Three-Dimensional Coordinates

The program VANGLE calculates the angle between two points.  Both points are entered in vector form.

Syntax:  VANGLE([x1,y1,z1], [x2,y2,z2])

HP Prime VANGLE:  Angle between two coordinates
EXPORT VANGLE(v1,v2)
BEGIN
// Angle between 2 vectors
LOCAL θ;
θ:=ACOS(DOT(v1,v2)/
(ABS(v1)*ABS(v2)));
RETURN θ;
END;

Example:  Find the angle between [5,4,5] and [2,0,-3]. 
VANGLE([5,4,5],[2,0,-3]) returns 99.8283573577°

Rotating a Cartesian Coordinate Vector

The program ROT3X, ROT3Y, and ROT3Z rotates the three-dimensional vector [x, y, z] with respect to the x-axis (ax),  respect to the y-axis (ay), and respect to the z-axis (az), respectively.

Syntax:  ROT3X(v, ax),  ROT3Y(v, ay),  ROT3Z(v, az)

Caution:  the result will be a matrix instead of a vector

HP Prime: ROT3X
EXPORT ROT3X(v,ax)
BEGIN
// [x,y,z],θx
v:=TRN(v);
v:=[[1,0,0],[0,COS(ax),−SIN(ax)],
[0,SIN(ax),COS(ax)]]*v;
RETURN TRN(v);
END;

HP Prime:  ROT3Y
EXPORT ROT3Y(v,ay)
BEGIN
// [x,y,z],θy
v:=TRN(v);
v:=[[COS(ay),0,SIN(ay)],
[0,1,0],[−SIN(ay),0,COS(ay)]]*v;
RETURN TRN(v);
END;

HP Prime:  ROT3Z
EXPORT ROT3Z(v,az)
BEGIN
// [x,y,z],θz
v:=TRN(v);
v:=[[COS(az),−SIN(az),0],
[SIN(az),COS(az),0],[0,0,1]]*v;
RETURN TRN(v);
END;

Example:  Rotate the vector [2, 3, 4] 30°, with respect to the x-axis, y-axis, and z-axis, separately and respectfully.

ROT3X([2, 3, 4], 30) returns [[ 2, 0.598076211352, 4.96410161514 ]]

ROT3Y([2, 3, 4], 30) returns [[ 3.73205080757, 3, 2.46410161514 ]]

ROT3Z([2, 3, 4], 30) returns [[ 0.232050807568, 3.59807621135, 4]]


This blog is property of Edward Shore, 2016




Thursday, April 23, 2015

HP Prime and TI-84+: Rotation and Translation of the 3-D Vector

HP Prime and TI-84+:  Rotation and Translation of the 3-D Vector

Greetings from Monterey, California!

Greetings from Cal State Monterey Bay 

Deep Sea Tank:  Monterey Bay Aquarium


Let P be a three dimensional column vector P = [[ x ],[ y ],[ z ]].

We can move P by a linear transformation by addition to get a new vector P’:

P’ = P + T where T = [[ t1 ],[ t2 ],[ t3 ]].

We can rotate point P by using one of three rotation matrices using left-multiplication:

P’ = RP  where R can take the form:

Rotation about the x-axis, 
Rx = [[ 1, 0, 0 ],[ 0, cos θ, -sin θ ],[ 0, sin θ, cos θ ]]

Rotation about the y-axis:
Ry = [[ cos θ, 0, -sin θ ],[ 0, 1, 0 ],[ sin θ, 0, cos θ ]]

Rotation about the z-axis:
Rz = [[ cos θ, -sin θ, 0 ],[ sin θ, cos θ, 0 ],[ 0, 0, 1 ]]

We can take all three rotation matrices into account to get:

P’ = Rx Ry Rz P

Adding a linear translation and we arrive at:

P’ = Rx Ry Rz P + T

HP Prime:  ROTTRAN3(m,a,b,c,t)

Input: 
m = a 3 x 1 column matrix which represents P
a = rotation angle for the x-axis
b = rotation angle for the y-axis
c = rotation angle for the z-axis
t = a 3 x 1 column matrix for linear transformation

Output:  3 x 1 column matrix which represents P’

Program:

EXPORT ROTTRAN3(m,a,b,c,t)
BEGIN
LOCAL x,y,z,n;

x:=[[1,0,0],[0,COS(a),−SIN(a)],
[0,SIN(a),COS(a)]];
y:=[[COS(b),0,−SIN(b)],[0,1,0],
[SIN(b),0,COS(b)]];
z:=[[COS(c),−SIN(c),0],
[SIN(c),COS(c),0],[0,0,1]];
n:=x*y*z*m+t;
RETURN n;

END;


TI-84 Plus:  ROTTRAN3

Original Point:  <X, Y, Z>
Angles for Rx (A), Ry (B), and Rz (C)
Translation Point:  <S, T, U>

Program:
: Disp “X,Y,Z:”
: Prompt X,Y,Z
: Disp “ANGELS OF X,Y,Z:”
: Prompt A,B,C
: Disp “LINEAR SHIFT:”
: Prompt S,T,U
: [[1,0,0][0,cos(A),-sin(A)
][0,sin(A),cos(A)]]*[[cos(
B),0,-sin(B)][0,1,0][sin(B
),0,cos(B)]]*[[cos(C),-sin
(C),0][sin(C),cos(C),0][0,
0,1]]*[[X][Y][Z]]->[J]
: [J]+[[S][T][U]]->[J]
: Disp [J]


Example:

P = [[1][2][3]]
Rotate angles (radians):  x: 0, y: 0.25, z: 0.15
T = [[0][0][1]]

P’ =
[[ -0.073764223786 ]
[ 2.12698028835 ]
[ 4.07741997334 ]]


Source:
Lengyel, Eric.  “Mathematics for 3D Game Programming & Computer Graphics” 2nd Edition.  Charles River Media, Inc.  Hingham, MA  2004


This program is property of Edward Shore.  2015.


Python – Earth’s Radius and Gravity in US Units

Python – Earth’s Radius and Gravity in US Units Introduction The following script, gravus2.py, estimates the Earth’s gravity i...