In: Computer Science
C++ please test 1 > run Enter 1st vector (2 floats): 0 0 Coordinates cannot both be zero. Enter 1st vector (2 floats): 0 0 Coordinates cannot both be zero. Enter 1st vector (2 floats): 1 0 Enter 2nd vector (2 floats): 0 0 Coordinates cannot both be zero. Enter 2nd vector (2 floats): 0 0 Coordinates cannot both be zero. Enter 2nd vector (2 floats): 1 0 First vector: (1, 0) has length 1 Second vector: (1, 0) has length 1 Angle between vectors (1, 0) and (1, 0) = 0 degrees. The vectors are NOT ORTHOGONAL. Test 2 > run Enter 1st vector (2 floats): 1 0 Enter 2nd vector (2 floats): 0 0 Coordinates cannot both be zero. Enter 2nd vector (2 floats): 1 1 First vector: (1, 0) has length 1 Second vector: (1, 1) has length 1.41421 Angle between vectors (1, 0) and (1, 1) = 45 degrees. The vectors are NOT ORTHOGONAL. Test 3 > run Enter 1st vector (2 floats): 0 1 Enter 2nd vector (2 floats): 1 0 First vector: (0, 1) has length 1 Second vector: (1, 0) has length 1 Angle between vectors (0, 1) and (1, 0) = 90 degrees. The vectors are ORTHONORMAL. Test 4 > run Enter 1st vector (2 floats): 2 2 Enter 2nd vector (2 floats): -1 0 First vector: (2, 2) has length 2.82843 Second vector: (-1, 0) has length 1 Angle between vectors (2, 2) and (-1, 0) = 135 degrees. The vectors are NOT ORTHOGONAL. Test 5 > run Enter 1st vector (2 floats): 1.25 -0.25 Enter 2nd vector (2 floats): 0.35 3.14 First vector: (1.25, -0.25) has length 1.27475 Second vector: (0.35, 3.14) has length 3.15945 Angle between vectors (1.25, -0.25) and (0.35, 3.14) = 94.9497 degrees. The vectors are NOT ORTHOGONAL. Test 6 > run Enter 1st vector (2 floats): 0.35 3.14 Enter 2nd vector (2 floats): 1.25 -0.25 First vector: (0.35, 3.14) has length 3.15945 Second vector: (1.25, -0.25) has length 1.27475 Angle between vectors (0.35, 3.14) and (1.25, -0.25) = 94.9497 degrees. The vectors are NOT ORTHOGONAL.78 Test 7 > run Enter 1st vector (2 floats): 0.70710678118 0.70710678118 Enter 2nd vector (2 floats): -0.70710678118 0.70710678118 First vector: (0.707107, 0.707107) has length 1 Second vector: (-0.707107, 0.707107) has length 1 Angle between vectors (0.707107, 0.707107) and (-0.707107, 0.707107) = 90 degrees. The vectors are ORTHONORMAL. Test 8 > run Enter 1st vector (2 floats): -0.70710678118 0.70710678118 Enter 2nd vector (2 floats): -0.70710678118 -0.70710678118 First vector: (-0.707107, 0.707107) has length 1 Second vector: (-0.707107, -0.707107) has length 1 Angle between vectors (-0.707107, 0.707107) and (-0.707107, -0.707107) = 90 degrees. The vectors are ORTHONORMAL. Test 9 > run Enter 1st vector (2 floats): 0 -5 Enter 2nd vector (2 floats): -1 0 First vector: (0, -5) has length 5 Second vector: (-1, 0) has length 1 Angle between vectors (0, -5) and (-1, 0) = 90 degrees. The vectors are ORTHOGONAL, but NOT ORTHONORMAL. A 2D vector consists of an (x, y) pair in the cartesian coordinate system. Write a program that reads in a pair of 2D vectors and outputs the angle between the vectors in degrees and whether they are orthogonal, orthonormal, or orthogonal but not orthonormal. Please read the description of the math required in this assignment at this link https://www.mathsisfun.com/algebra/vectors-dot-product.html. Your task is to add eight functions so that the program produces the desired results.
Use the global constant called EPSILON whenever you are comparing two decimal numbers (type double). See the lecture notes "13_Numerical" on Numerical Error.
For each function below, first write the function prototype before the main function and then write the function definition after the main function. The comments in the program template indicate where to place these.
TASK 3: Write a function called read_vector which inputs from the user a 2D vector by reading its x- and y- coordinates. If the user enters zero for BOTH coordinates then repeatedly display the message "Coordinates cannot both be zero." and re-prompt the user. Use the global constant EPSILON to compare these values to zero. Define the function so that it does not return a value and has three input parameters:
TASK 4: Write a function called compute_vector_angle which computes the angle between two vectors (see the math in the link provided above) in RADIANS given two vectors. This function will call two functions (see TASK 4a and TASK 4c), called normalize and dot_product. Define the function compute_vector_angle so that it returns the angle in RADIANS (type double) and has four input parameters (all pass-by-value):
As described in https://www.mathsisfun.com/algebra/vectors-dot-product.html, the angle between two vectors can be computed using the dot product. If you want to compute the angle between the vectors a and b, then use a · b = |a| * |b| * cos(θ) and solve for θ, where |a| and |b| are the lengths of the vectors. To simplify this calculation, we can use the normalized versions of vectors a and b. A normalized vector has the same direction but instead has unit length. If we normalize the vectors a and b into two new vectors, say a' and b', then a' · b' = cos(θ), which has the same angle θ in the equation for a · b. If you solve for θ then you have computed the angle between the vectors a and b. I.e., θ = arc cos(a' · b').
The algorithm for computing the angle is:
TASK 4a: Write a function called normalize which normalizes a vector (x, y) by dividing by its length. This function is called twice by the compute_vector_angle function. Call the function length_vec in TASK 4b to compute the length of a vector. Define the function normalize so that it does not return a value and has two parameters (both pass-by-reference):
Given vector (x, y) and its length l, the normalized vector is (x', y') where x' = x/l and y' = y/l. Though, if length l is zero then x' = 0 and 'y' = 0. Use the global constant EPSILON to compare the length value to zero.
TASK 4b: Write a function called length_vec which computes the length of vector (x, y) as √ (x2 + y2), i.e. the square root of (x2 + y2). Define the function length_vec so that it returns the length (type double) and has two input parameters (both const and pass-by-value):
TASK 4c: Write a function called dot_product which computes the dot product of two vectors, say (x1, y1) and (x2, y2), as x1 * x2 + y1 * y2 (see https://www.mathsisfun.com/algebra/vectors-dot-product.html). Define the function dot_product so that it returns the dot product (type double) and has four input parameters (all const and pass-by-value):
TASK 5: Write a function called radians2degrees which converts radians to degrees. See the lecture notes on how to compute this. Define the function radians2degrees so that it returns a value in degrees (type double) and has one input parameter (const and pass-by-value):
TASK 6: Write a function called output_angle which displays both input vectors, the angle between the two vectors, and whether the vectors are either "NOT ORTHOGONAL", "ORTHONORMAL" ,or "ORTHOGONAL, but NOT ORTHONORMAL". See the test runs above. This function will call the function print_vec (see TASK 6a) twice. Define the function so that it does not return a value and has five input parameters (all const and pass-by-value):
Two vectors are ORTHOGONAL if the angle between them is 90 degrees. Two vectors are ORTHONORMAL if they are orthogonal and both have unit length (the vector length is one). Two vectors are ORTHOGONAL, BUT NOT ORTHONORMAL when they are orthogonal but at least one vector does not have unit length. To check for orthgonality, use the global constant EPSILON to compare the angle with 90 degrees. To check that a vector has unit length, use the global constant EPSILON to compare its length with one. Compute the vector's length by calling the function length_vec (see TASK 4b).
TASK 6a: Write a function called print_vec which displays a message, the coordinates of a vector, and its length. Define the function so that it does not return a value and has four input parameters:
Program
#include<iostream>
#include<cmath>
using namespace std;
const double EPSILON = 1e-11;
// function prototypes
void read_vector(string, double&, double&);
double length_vec(double, double);
double dot_product(double,double,double,double);
double compute_vector_angle(double,double,double,double);
double radians2degrees(double);
void normalize(double &u, double &v);
void print_vec(string, double, double);
void output_angle(double,double,double,double,double);
// *** DO NOT CHANGE ANY CODE IN THE MAIN FUNCTION.
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_vector("Enter 1st vector (2 floats): ", u1, v1);
read_vector("Enter 2nd vector (2 floats): ", 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);
cout << endl;
return(0);
}
// DEFINE FUNCTION read_vector
void read_vector(string msg, double &u, double &v)
{
while(1)
{
cout<<msg;
cin>>u>>v;
if(u!=0 || v!=0)
return;
cout<<"Coordinates cannot both be zero."<<endl;
}
}
// DEFINE FUNCTION length_vec
double length_vec(double u, double v)
{
return sqrt(u*u+v*v);
}
// DEFINE FUNCTION dot_product
double dot_product(double u1, double v1, double u2, double
v2)
{
return u1*u2 + v1*v2;
}
// DEFINE FUNCTION compute_vector_angle HERE.
double compute_vector_angle(double u1, double v1, double u2, double
v2)
{
double len1 = length_vec(u1, v1);
double len2 = length_vec(u2, v2);
normalize(u1, v1);
normalize(u2, v2);
double prod = dot_product(u1, v1, u2, v2);
double angle = acos(prod);
return angle;
}
//DEFINE FUNCTION radians2degrees
double radians2degrees(double rad)
{
double pi = 4*atan(1);
return 180*rad/pi;
}
// DEFINE FUNCTION normalize
void normalize(double &u, double &v)
{
double len = length_vec(u, v);
u = u/len;
v = v/len;
}
// DEFINE FUNCTION print_vec
void print_vec(string msg, double u, double v)
{
cout<<msg<<"("<<u<<","<<v<<")
has length "<<length_vec(u, v)<<endl;
}
// DEFINE FUNCTION output_angle
void output_angle(double u1, double v1, double u2, double v2,
double degrees)
{
print_vec("First vector:", u1, v1);
print_vec("Second vector:", u2, v2);
cout<<"Angle between vectors
"<<"("<<u1<<","<<v1<<") and
("<<u2<<","<<v2<<") = "<<degrees
<<" degrees."<<endl;
if(fabs(degrees-90)<=EPSILON && fabs(length_vec(u1,
v1)-1)<=EPSILON && fabs(length_vec(u2,
v2)-1)<=EPSILON)
cout<<"The vectors are ORTHONORMAL."<<endl;
else if(fabs(degrees-90)<=EPSILON &&
(fabs(length_vec(u1, v1)-1)>EPSILON|| fabs(length_vec(u2,
v2)-1)>EPSILON))
cout<<"The vectors are ORTHOGONAL, but NOT
ORTHONORMAL."<<endl;
else if(fabs(degrees-90)<=EPSILON)
cout<<"The vectors are ORTHOGONAL."<<endl;
else
cout<<"The vectors are NOT ORTHOGONAL."<<endl;
}
Output:
Solving your question and
helping you to well understand it is my focus. So if you face any
difficulties regarding this please let me know through the
comments. I will try my best to assist you. However if you are
satisfied with the answer please don't forget to give your
feedback. Your feedback is very precious to us, so don't give
negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid
plagiarism.
Thank you.