Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
vectors_3d.c File Reference

Functions related to 3D vector operations. More...

#include <stdio.h>
#include <math.h>
#include <assert.h>
#include "geometry_datatypes.h"
Include dependency graph for vectors_3d.c:

Functions

vec_3d vector_sub (const vec_3d *a, const vec_3d *b)
 Subtract one vector from another. More...
 
vec_3d vector_add (const vec_3d *a, const vec_3d *b)
 Add one vector to another. More...
 
float dot_prod (const vec_3d *a, const vec_3d *b)
 Obtain the dot product of two 3D vectors. More...
 
vec_3d vector_prod (const vec_3d *a, const vec_3d *b)
 Compute the vector product of two 3d vectors. More...
 
const char * print_vector (const vec_3d *a, const char *name)
 Print formatted vector on stdout. More...
 
float vector_norm (const vec_3d *a)
 Compute the norm a vector. More...
 
vec_3d unit_vec (const vec_3d *a)
 Obtain unit vector in the same direction as given vector. More...
 
mat_3x3 get_cross_matrix (const vec_3d *a)
 The cross product of vectors can be represented as a matrix multiplication operation. More...
 
double get_angle (const vec_3d *a, const vec_3d *b)
 Obtain the angle between two given vectors. More...
 
static void test ()
 Testing function. More...
 
int main (void)
 Main function. More...
 

Detailed Description

Functions related to 3D vector operations.

Author
Krishna Vedala

Function Documentation

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
261{
262 test();
263
264 return 0;
265}
static void test()
Testing function.
Definition: vectors_3d.c:223
Here is the call graph for this function:

◆ test()

static void test ( void  )
static

Testing function.

Returns
void
224{
225 vec_3d a = {1., 2., 3.};
226 vec_3d b = {1., 1., 1.};
227 float d;
228
229 // printf("%s", print_vector(&a, "a"));
230 // printf("%s", print_vector(&b, "b"));
231
232 d = vector_norm(&a);
233 // printf("|a| = %.4g\n", d);
234 assert(fabsf(d - 3.742f) < 0.01);
235 d = vector_norm(&b);
236 // printf("|b| = %.4g\n", d);
237 assert(fabsf(d - 1.732f) < 0.01);
238
239 d = dot_prod(&a, &b);
240 // printf("Dot product: %f\n", d);
241 assert(fabsf(d - 6.f) < 0.01);
242
243 vec_3d c = vector_prod(&a, &b);
244 // printf("Vector product ");
245 // printf("%s", print_vector(&c, "c"));
246 assert(fabsf(c.x - (-1.f)) < 0.01);
247 assert(fabsf(c.y - (2.f)) < 0.01);
248 assert(fabsf(c.z - (-1.f)) < 0.01);
249
250 double alpha = get_angle(&a, &b);
251 // printf("The angle is %f\n", alpha);
252 assert(fabsf(alpha - 0.387597) < 0.01);
253}
float dot_prod(const vec_3d *a, const vec_3d *b)
Obtain the dot product of two 3D vectors.
Definition: vectors_3d.c:76
double get_angle(const vec_3d *a, const vec_3d *b)
Obtain the angle between two given vectors.
Definition: vectors_3d.c:202
float vector_norm(const vec_3d *a)
Compute the norm a vector.
Definition: vectors_3d.c:138
vec_3d vector_prod(const vec_3d *a, const vec_3d *b)
Compute the vector product of two 3d vectors.
Definition: vectors_3d.c:105
3D vector type
Definition: geometry_datatypes.h:22
Here is the call graph for this function: