I am trying to get this to print the angle between a set of coordinates in degre
ID: 3655704 • Letter: I
Question
I am trying to get this to print the angle between a set of coordinates in degrees. Any idea how i can get this to work correctly? I put the error at the end. I don't really understand other syntax (like "stdio.h" and all that), but I am trying to get this program to work with the syntax that is already in use. Any help would be appreciated
--------------------------------------- This program reads in the coordinates of two 2D vectors
and outputs the angle between the vectors in degrees.-------------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
--------------------------------------------------------------------function prototypes---------------------------------------------------------------------
// ENTER FUNCTION PROTOTYPE FOR normalize() HERE.
double normalize(double &x, double &y);
// ENTER FUNCTION PROTOTYPE FOR dot_product() HERE.
double dot_product(double x1, double x2, double y1, double y2);
// ENTER FUNCTION PROTOTYPE FOR compute_vector_angle() HERE.
double compute_vector_angle(double x1, double x2, double y1, double y2);
// ENTER FUNCTION PROTOTYPE FOR radians2degrees() HERE.
double radians2degrees(double numRad);
// ENTER FUNCTION PROTOTYPE FOR output_angle() HERE.
void output_angle(double x1, double x2, double y1, double y2, double numDeg);
--------------------------------------------------------------------------//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);
}
------------------------------------------------------------FUNCTION DEFINITIONS----------------------------------------------------------------------
// DEFINE FUNCTION normalize() HERE.
double normalize(double &x, double &y)
{
if (sqrt(pow(x,2)+pow(x,2))==0)
{
x=0;
y=0;
}
else
{
x=(x/sqrt(pow(x,2)+pow(x,2)));
y=(y/sqrt(pow(x,2)+pow(x,2)));
}
}
// DEFINE FUNCTION dot_product() HERE.
double dot_product(double x1, double x2, double y1, double y2)
{
double u1, v1;
u1=x1;
v1=y1;
double u2, v2;
u2=x2;
v2=y2;
double dot_pcalc;
dot_pcalc=x1*x2+y1*y2;
return(dot_pcalc);
}
// DEFINE FUNCTION compute_vector_angle() HERE.
double compute_vector_angle(double x1, double x2, double y1, double y2)
{
double x;
double y;
x=normalize(x1, y1);
y=normalize(x2, y2);
double product;
product=dot_product(x1, x2, y1, y2);
double angle;
angle=acos(product);
return(angle);
}
// DEFINE FUNCTION radians2degrees() HERE.
double radians2degrees(double numRad)
{
compute_vector_angle(numRad);
double numDeg;
numDeg=180*(numRad/M_PI);
return(numDeg);
}
// DEFINE FUNCTION output_angle() HERE.
void output_angle(double x1, double x2, double y1, double y2, double numDeg)
{
radians2degrees(numDeg);
cout<<"Angle between vectors x1,y1 and x2,y2 is "<<numDeg<<" degrees "<<endl;
}
---------------------------------------------------------ERRORS--------------------------------------------------------------------------
vector2D_angle.cpp: In function
Explanation / Answer
There are multiple errors in program you wrote. In Normalize function actually return type is Double but you are not returning anything inside function and i think the logic what you wrote for normalize function is totally wrong. In some functions you are calling functions but you are not assigning returned values to some variables.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.