Write a program that reads in a pair of 2D vectors and outputs the angle between
ID: 3655765 • Letter: W
Question
Write a program that reads in a pair of 2D vectors and outputs the angle between the vectors in degrees.
Don't modify main. Add 5 functions normalize() (doesn't return a value), dot_product(), radians2degrees(), compute_vector_angle(), and output_angle()(does not return a value) so that the program produces the desired results.
about the functions:
normalize() which normalizes vector(x, y), is supposed to be passed by reference and has 2 parameters (x and y) and is DOUBLE
dot_product() which returns the dot product of two vectors. Input to the function are the coordinate (x1, y1) and (x2, y2) of two vectors, output is the dot product. The function has four parameters:
a number x1 of type double representing the first x coordinate;
a number y1 of type double representing the first y coordinate;
a number x2 of type double representing the second x coordinate;
a number y2 of type double representing the second y coordinate;
compute_vector_angle() which returns the angle in Radians between vectors (x1; y1) and (x2; y2). Input to the function are the vector coordinates (x1; y1) and (x2; y2). Output is the angle in radians. The function has four parameters (x1, x2, y1, y2)
--The algorithm for computing the angle is:
(a) Normalize (x1; y1) and (x2; y2);
(b) product (x1; y1) (x2; y2);
((x1; y1) (x2; y2) is the dot product of vectors (x1; y1) and (x2; y2)).
(c) angle cos^(-1)(product)
(d) Return angle
Use normalize() to normalize (x1; y1) and (x2; y2).
Use your C++ function dot_product() to compute the dot product of (x1; y1) and
(x2; y2).
acos to compute cos^(-1)
radians2degrees() converts radians to degrees. Input to the function is an angle in radians. Output is the angle in degrees. The function has one parameter, a number radians of type double, representing the angle in radians. The function returns a number of type double representing the angle converted to degrees
------Note that the main program calls the function radians2degrees() to convert the angle in radians to the angle in degrees.
.
output_angle() outputs "Angle between vectors (x1; y1) and (x2; y2) = angle degrees" where x1; y1; x2; and y2 are the vector coordinates and angle is the angle between the vectors in degrees. The function has five parameters (x1,x2,y1,y2,angle in degrees). does not return a value, but passed by a value
Here is main:
int main()
{
double u1, v1; // coordinates of vector 1
double u2, v2; // coordinates of vector 2
double radians; // angle in radians
double degrees; // angle in degrees
// Read points
cout << "Enter first vector (2 floats): ";
cin >> u1 >> v1;
cout << "Enter second vector (2 floats): ";
cin >> u2 >> v2;
// compute angle in radians between (u1, v1) and (u2, v2)
radians = compute_vector_angle(u1, v1, u2, v2);
// convert radians to degrees
degrees = radians2degrees(radians);
// output angle
output_angle(u1, v1, u2, v2, degrees);
return(0);
}
Explanation / Answer
#define _USE_MATH_DEFINES #include #include #include using namespace std; void normalize(double&,double&) ; double dot_product(double,double,double,double); double radians2degrees(double); double compute_vector_angle(double,double,double,double); void output_angle(double,double,double,double,double); int main() { double u1, v1; // coordinates of vector 1 double u2, v2; // coordinates of vector 2 double radians; // angle in radians double degrees; // angle in degrees // Read points cout > u1 >> v1; cout > u2 >> v2; // compute angle in radians between (u1, v1) and (u2, v2) radians = compute_vector_angle(u1, v1, u2, v2); // convert radians to degrees degrees = radians2degrees(radians); // output angle output_angle(u1, v1, u2, v2, degrees); return(0); } //normalize() which normalizes vector(x, y), is supposed to be passed by reference and has 2 parameters (x and y) and is DOUBLE void normalize(double &x,double &y){ double length=sqrt(pow(x,2)+pow(y,2)); x=x/length; y=y/length; return; } /*dot_product() which returns the dot product of two vectors. Input to the function are the coordinate (x1, y1) and (x2, y2) of two vectors, output is the dot product. The function has four parameters: a number x1 of type double representing the first x coordinate; a number y1 of type double representing the first y coordinate; a number x2 of type double representing the second x coordinate; a number y2 of type double representing the second y coordinate;*/ double dot_product(double x1,double y1,double x2,double y2){ return (x1*x2)+(y1*y2); } //Input to the function is an angle in radians. //Output is the angle in degrees. //The function has one parameter, a number radians of type double, representing the angle in radians. //The function returns a number of type double representing the angle converted to degrees double radians2degrees(double radians){ return radians*180/M_PI; } /* compute_vector_angle() which returns the angle in Radians between vectors (x1; y1) and (x2; y2). Input to the function are the vector coordinates (x1; y1) and (x2; y2). Output is the angle in radians. The function has four parameters (x1, x2, y1, y2) */ double compute_vector_angle(double x1,double x2,double y1,double y2){ //(a) Normalize (x1; y1) and (x2; y2); normalize(x1,y1); normalize(x2,y2); //(b) product (x1; y1) (x2; y2); //((x1; y1) (x2; y2) is the dot product of vectors (x1; y1) and (x2; y2)). double product=dot_product(x1,y1,x2,y2); //(c) angle cos^(-1)(product) double angle=acos(product); //(d) Return angle return angle; } /*output_angle() outputs "Angle between vectors (x1; y1) and (x2; y2) = angle degrees" where x1; y1; x2; and y2 are the vector coordinates and angle is the angle between the vectors in degrees. The function has five parameters (x1,x2,y1,y2,angle in degrees) . does not return a value, but passed by a value */ void output_angle(double x1,double x2,double y1,double y2,double angle_degrees){ coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.