#include #include #include // define vector typedef struct vec { double x; double y; double z; } vector; // function prototype double cvt_feet_to_meters(double feet); double cvt_meters_to_feet(double meters); void print_vector(char *str , vector *x); void print_dot(char *str,vector *a,vector *b); double magnitude(vector *x); void print_vector_magnitude(char *str,vector *x); double direction_2d(vector *x); void print_vector_direction_2d(char *str,vector *x); void prob_05(); void prob_02(); void prob_03(); double dot(vector *x, vector *y); vector *cross(vector *x, vector *y); void print_cross_and_free(char *str , vector *a,vector *b); int main() { printf("c output chapter 2\n"); prob_05(); } void prob_05() { vector a; vector b; vector c; // a + b vector d; // b - a vector e; // a - b vector f; vector g; vector h; a.x = 4.0; a.y = -3.0; a.z = 0.0; b.x = 6.0; b.y = 8.0; b.z = 0.0; printf("Problem 5\n"); c.x = a.x + b.x; c.y = a.y + b.y; c.z = a.z + b.z; d.x = b.x - a.x; d.y = b.y - a.y; d.z = b.z - a.z; e.x = a.x - b.x; e.y = a.y - b.y; e.z = a.z - b.z; f.x = 2.0; f.y = -3.0; f.z = 7.0; g.x = 12.0; g.y = 45.0; g.z = -8.0; h.x = -5.0; h.y = -3.0; h.z = 42.0; print_vector("a",&a); print_vector("b",&b); print_vector("c",&c); print_vector("d",&d); print_vector("e",&e); print_dot("a.a",&a,&a); print_dot("a.b",&a,&b); print_dot("b.a",&b,&a); print_dot("a.c",&a,&c); print_dot("d.a",&d,&a); print_dot("a.e",&a,&e); print_dot("e.a",&e,&a); print_cross_and_free("axa",&a,&a); print_cross_and_free("axb",&a,&b); print_cross_and_free("bxa",&b,&a); print_cross_and_free("axc",&a,&c); print_cross_and_free("dxa",&d,&a); print_cross_and_free("axe",&a,&e); print_cross_and_free("exa",&e,&a); print_cross_and_free("fxg",&f,&g); print_cross_and_free("fxh",&f,&h); print_cross_and_free("gxh",&g,&h); print_cross_and_free("gxf",&g,&f); print_cross_and_free("hxf",&h,&f); print_cross_and_free("hxg",&h,&g); } double dot(vector *a, vector *b) { double ret; ret = a->x * b->x + a->y * b->y + a->z * b->z; return ret; } void print_dot(char* str,vector *a, vector *b) { printf("dot %s = %8.4f\n",str,dot(a,b)); } vector *cross(vector *a, vector *b) { vector *ret; ret = (vector *) malloc(sizeof(vector)); ret->x = a->y * b->z - b->y * a->z; ret->y = -( a->x * b->z - b->x * a->z); ret->z = a->x * b->y - b->x * a->y; return ret; } void print_cross_and_free(char *str , vector *a,vector *b) { vector *it; it = cross(a,b); printf("cross %s = %8.4f i + %8.4f j + %8.4f k\n",str,it->x,it->y,it->z); free(it); } void print_vector(char *str , vector *x) { printf("vector %s = %8.4f i + %8.4f j + %8.4f k\n",str,x->x,x->y,x->z); print_vector_magnitude(str,x); print_vector_direction_2d(str,x); } void print_vector_magnitude(char *str,vector *x) { printf("vector %s's magnitude is %8.4f\n",str,magnitude(x)); } void print_vector_direction_2d(char *str,vector *x) { printf("vector %s's direction (radians) is %8.4f\n",str,direction_2d(x)); printf("vector %s's direction (degrees) is %8.4f\n",str,direction_2d(x) * 45.0 / atan(1.0)); } double magnitude(vector *x) { return sqrt(x->x * x->x + x->y * x->y + x->z * x->z); } double direction_2d(vector *x) { return atan2(x->y, x->x); }