genVectorAng
in genMath.lib

Vector Functions            Function List by Category            Alphabetical Function

Sample Code

int genVectorAngle(
                                  float *VectorA,        // pointer to array holding vector A
                                  float *VectorB,        // pointer to array holding vector B
                                  int dimen,                 // dimension of vectors A and B
                                  double *CosAngle) // pointer to store result

This function computes the cosine of the angle between two vectors of dimension n:

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 calling the genVectorDotProd function.  This is a higher performance approach, 
but does involve duplication of code.

The explicit routines VectorAngle2d and VectorAngle3d are faster due to code
optimizations in explicit algorithms.

The return code is errno, defined in the C standard library (math.h).  This is an integer value
for math errors.

 
Sample Application:
/////////// splgnAng.c for genVectorAngle 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.						    //
//								    //
//////////////////////////////////////////////////////////////////////

//////////////////////////// INCLUDES ////////////////////////////////

#include "stdio.h"
#include "genMath.h"

////////////////////////////// MAIN //////////////////////////////////
void main()
{

	//local declarations
	//integers
	int retcode;			// return code for call to genVectorAngle

	//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 = genVectorAngle(a, b, 3, &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 splgnAng.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 splgnAng.c: 
Cos Angle between Vectors = -0.990786