# include # include int main ( ) { float pi = 3.14159265; float u[2]; float u_cross_v; float u_dot_v; float u_norm; float uv_angle; float uv_cosine; float v[2]; float v_norm; float x; float y; printf ( "Enter x, y for vector 1: " ); scanf ( "%f%f", &x, &y ); u[0] = x; u[1] = y; printf ( "Enter x, y for vector 2: " ); scanf ( "%f%f", &x, &y ); v[0] = x; v[1] = y; // // Compute the norms of the two vectors. // u_norm = sqrt ( u[0] * u[0] + u[1] * u[1] ); printf ( " ||u|| = %g\n", u_norm ); v_norm = sqrt ( pow ( v[0], 2 ) + pow ( v[1], 2 ) ); printf ( " ||v|| = %g\n", v_norm ); // // The dot product of two vectors is 0 if they are perpendicular to each other. // u_dot_v = u[0] * v[0] + u[1] * v[1]; printf ( " u.v = %g\n", u_dot_v ); u_cross_v = u[0] * v[1] - u[1] * v[0]; printf ( " uxv = %g\n", u_cross_v ); // // The cosine of the angle between two vectors is the dot product divided by their norms. // uv_cosine = u_dot_v / u_norm / v_norm; printf ( " cosine(u,v) = %g\n", uv_cosine ); // // The ANGLE between two vectors is the inverse cosine or "arc-cosine" of the cosine. // This will come out in RADIANS unless we divide the result by PI and multiply by 180. // printf ( "\n" ); uv_angle = acos ( uv_cosine ); printf ( " angle(u,v) = %g radians\n", uv_angle ); uv_angle = uv_angle / pi * 180; printf ( " angle(u,v) = %g degrees\n", uv_angle ); return 0; }