In computer graphics, lighting calculations often use the angle between vectors.
ID: 3553870 • Letter: I
Question
In computer graphics, lighting calculations often use the angle between vectors.
Write a program that asks a user to input two 3-dimensional vectors and calculates and displays the angle
between them. Vectors are input using the following format (square brackets and commas required), e.g, :
[23.5, 22.1, 99.6]
The angle should be printed out in radians. It does not need to be formatted or rounded.
Your program must do the following:
1. Include a void function named inputVector that takes an empty array as its only argument and
returns a 3 dimensional vector entered from the console. The inputVector function must use
getline to read a string from the console that contains a vector in the format as described above.
You will need to parse the string to obtain the three values and store them in the array [Hint: convert
values to c-strings and use the atof function]. If the string is not entered correctly (wrong number of
values, missing brackets, etc.) provide an error message and exit the program using the exit(1)
function.
2. Include a second function named printVector that will take an array of double values representing
a 3-dimensional vector and display it in the following format (square brackets and commas required),
e.g.,:
[23.5, 22.1, 99.6]
3. Include a third function named dotProduct that will take two array arguments representing 3
dimensional vectors and return the scalar dot product.
4. Include a fourth function named magnitude that will take a single array representing a 3 dimensional
vector and return the scalar vector magnitude. The magnitude function should call dotProduct.
5. Include the following main function in your source code (exactly as it appears):
int main()
{ double vector1[3],vector2[3],theta;
cout << "Enter first vector: ";
inputVector(vector1);
cout << "Enter second vector: ";
inputVector(vector2);
printVector(vector1);
printVector(vector2);
theta = acos(dotProduct(vector1,vector2)/
(magnitude(vector1)*magnitude(vector2)));
cout << "The angle between the vectors is: " << theta << endl;
return 0;
}
Example:
Enter first vector: [0,0,4]
Enter second vector: [0,8,0]
[0,0,4]
[0,8,0]
The angle between the vectors is: 1.5708
Explanation / Answer
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cmath>
using namespace std;
void inputVector(double* v){
string s;
cin>>s;
cout<<s<<endl;
unsigned pos = s.find(",");
v[0]=atof((s.substr(1,pos-1)).c_str());
unsigned pos2 = s.find(",",pos+1);
v[1]=atof((s.substr(pos+1,pos2-pos-1)).c_str());
unsigned pos3 = s.find("]");
v[2]=atof((s.substr(pos2+1,pos3-pos2-1)).c_str());
}
void printVector(double* v){
cout<<"["<<v[0]<<","<<v[1]<<","<<v[2]<<"]"<<endl;
}
double dotProduct(double* v1,double* v2){
return (v1[0]*v2[0])+(v1[1]*v2[1])+(v1[2]*v2[2]);
}
double magnitude(double* v1){
return sqrt((v1[0]*v1[0])+(v1[1]*v1[1])+(v1[2]*v1[2]));
}
int main()
{ double vector1[3],vector2[3],theta;
cout << "Enter first vector: ";
inputVector(vector1);
cout << "Enter second vector: ";
inputVector(vector2);
printVector(vector1);
printVector(vector2);
theta = acos(dotProduct(vector1,vector2)/(magnitude(vector1)*magnitude(vector2)));
cout << "The angle between the vectors is: " << theta << endl;
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.