VectorLength2d and VectorLength3d
in genMath.lib
Vector Functions Function List by Category Alphabetical Function
int VectorLength2d(
float *Vector, // pointer to array holding
vector
float *Len) // pointer to
store result
int VectorLength3d(
float *Vector, // pointer to array holding
vector
float
*Len) // pointer to
store result
These
functions compute the length of a 2d and 3d vectors. The functions
'assume'
that one end of the vector is at the origin. If you need to compute the
length of a
more general vector, pass the coordinate differences to the function (such as x1
- x2,
y1-y2):
2d
Length
= sqrt{ a[0]^2 + a[1]^2 }
3d Length = sqrt{ a[0]^2 + a[1]^2 + a[2]^2 }
Calls
to these routines will be faster than calls to genVectorLength,
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:
/////////// splVLen3.c for VectorLength3d Library Function /////////// // // // // For this sample, a 3-d vector is used. The vector is // // stored in array a, and the length is returned into // // c. // // // // Use of VectorLength2d is similar. // // // ////////////////////////////////////////////////////////////////////// //////////////////////////// INCLUDES //////////////////////////////// #include "stdio.h" #include "genMath.h" ////////////////////////////// MAIN ////////////////////////////////// void main() { //local declarations //integers int retcode; // return code for call to VectorLength3d //floats float a[3]; // vector float c; // Length of vector //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; //compute the length retcode = VectorLength3d(a,&c); //display the result printf("%s%f%s", "Vector Length = ", c,"\n\n"); //display closing message and wait for <enter> before exiting printf("Press <Enter> to end"); scanf("%c", &ch); } // End Main /////////////////////////////// End splVLen3.c ///////////////////////
The reader may compute the length of A and compare to the output of the sample program.
Output from the test program splVLen3.c:
Vector Length = 10.395790