VectorAng2d and VectorAng3d
in genMath.lib
Vector Functions Function List by Category Alphabetical Function
int VectorAngle2d(
float
*VectorA,
// pointer to array holding vector A
float
*VectorB,
// pointer to array holding vector B
double
*CosAngle) // pointer to store result
int VectorAngle3d(
float
*VectorA,
// pointer to array holding vector A
float *VectorB,
// pointer to array holding vector B
double
*CosAngle) // pointer to store result
These functions compute the cosine of the angle between 2d and 3d vectors:
cos(angle) = A . B /{sqrt{A . A)(sqrt(B . B)}
The
function assumes that the starting points of both vectors are already at the
origin.
Therefore, if you are using more general vectors, you must 'translate' the
vector
start end to the origin.
In
many (maybe most) uses, the actual angle is not needed; the user can work with
the
cosine of the angle. Therefore, to save cycles, the acos is not computed in this
function.
The cosine of the angle is returned. If the user does need the actual
angle, the calling program
should compute the acos of the returned value.
In
addition, this function uses three dot products. The dot products are
computed explicitly,
rather than making a call to either of the Dot Product functions included in the
library. This
is
a higher performance approach, but does involve duplication of code.
Calls
to these routines will be faster than calls to genVectorAng,
for two reasons.
First, the dimension does not have to be passed as a parameter, and second, the
loops
in these routines are unrolled.
The
return code is errno, defined in the C standard library (math.h). This is
an integer value
for math errors.
Sample Application:
//////////// splVAng.c for VectorAngle3d Library Function //////////// // // // For this sample, 3-d vectors are used. Vectors are stored // // in arrays a and b, and cos(angle) is returned into // // a scalar, c. // // // // Use of the VectorAngle2d function is similar. // // // ////////////////////////////////////////////////////////////////////// //////////////////////////// INCLUDES //////////////////////////////// #include "stdio.h" #include "genMath.h" ////////////////////////////// MAIN ////////////////////////////////// void main() { //local declarations //integers int retcode; // return code for call to VectorAngle3d //floats float a[3]; // vector A float b[3]; // vector B //doubles double c; // cosine of angle between A and B //character strings char ch; //input string to exit //intialize the A and B vectors a[0] = (float)1.234; a[1] = (float)-.678; a[2] = (float)10.3; b[0] = (float)0.3811; b[1] = (float)6.2201; b[3] = (float)0.115; //compute the cosine of the angle between A and B retcode = VectorAngle(a, b, &c); //display the result printf("%s%f%s", "Cos Angle between Vectors = ", c,"\n\n"); //display closing message and wait for <enter> before exiting printf("Press <Enter> to end"); scanf("%c", &ch); } // End Main /////////////////////////////// End splVAng.c ///////////////////////
The reader may compute the angle between A and B and compare to the output of the sample program.
Output from the test program splVAng.c:
Cos Angle between Vectors = -0.990786