/* * Definition of type for 3D vectors: struct and some functions that * seem useful. * * (By putting this in a separate file we can potentially use it in many * programs.) */ #include #include typedef struct { double x; double y; double z; } three_d_vec; /* macro to square a number */ #define SQUARE(x) (x)*(x) three_d_vec vec_add(three_d_vec v1, three_d_vec v2) { three_d_vec result = { v1.x + v2.x, v1.y + v2.y, v1.z + v2.z }; return result; } three_d_vec vec_scalar_mult(double d, three_d_vec v1) { three_d_vec result = { d * v1.x, d * v1.y, d * v1.z }; return result; } void vec_print(three_d_vec v1, FILE * outfile) { fprintf(stdout, "(%f %f %f)", v1.x, v1.y, v1.z); } double vec_length(three_d_vec v1) { return sqrt(SQUARE(v1.x) + SQUARE(v1.y) + SQUARE(v1.z)); } /* * FIXME: * add dot and cross products? */ double vec_dot_product(three_d_vec v1, three_d_vec v2); three_d_vec vec_cross_product(three_d_vec v1, three_d_vec v2);