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...
 
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
234 {
235  test();
236 
237  return 0;
238 }
Here is the call graph for this function:

◆ test()

static void test ( void  )
static

Testing function.

Returns
void
201 {
202  vec_3d a = {1., 2., 3.};
203  vec_3d b = {1., 1., 1.};
204  float d;
205 
206  // printf("%s", print_vector(&a, "a"));
207  // printf("%s", print_vector(&b, "b"));
208 
209  d = vector_norm(&a);
210  // printf("|a| = %.4g\n", d);
211  assert(fabsf(d - 3.742f) < 0.01);
212  d = vector_norm(&b);
213  // printf("|b| = %.4g\n", d);
214  assert(fabsf(d - 1.732f) < 0.01);
215 
216  d = dot_prod(&a, &b);
217  // printf("Dot product: %f\n", d);
218  assert(fabsf(d - 6.f) < 0.01);
219 
220  vec_3d c = vector_prod(&a, &b);
221  // printf("Vector product ");
222  // printf("%s", print_vector(&c, "c"));
223  assert(fabsf(c.x - (-1.f)) < 0.01);
224  assert(fabsf(c.y - (2.f)) < 0.01);
225  assert(fabsf(c.z - (-1.f)) < 0.01);
226 }
Here is the call graph for this function:
vector_norm
float vector_norm(const vec_3d *a)
Compute the norm a vector.
Definition: vectors_3d.c:138
vec_3d_
3D vector type
Definition: geometry_datatypes.h:22
test
static void test()
Testing function.
Definition: vectors_3d.c:200
vector_prod
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
dot_prod
float dot_prod(const vec_3d *a, const vec_3d *b)
Obtain the dot product of two 3D vectors.
Definition: vectors_3d.c:76