1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
/*
File: vector_angle.cpp
Created by: Me
Creation Date: 03/17/2013
Last Modification Date: 03/17/2013
THIS PROGRAM IS TAKEN FROM CSE202 SPRING 09 BY PROF WENGER
Synopsis:
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.
void normalize(double & x, double & y);
// ENTER FUNCTION PROTOTYPE FOR dot_product() HERE.
int dot_product(double x1, double y1, double x2, double y2);
// ENTER FUNCTION PROTOTYPE FOR compute_vector_angle() HERE.
int compute_vector_angle(double x1, double y1, double x2, double y2);
// ENTER FUNCTION PROTOTYPE FOR radians2degrees() HERE.
int radians2degrees(double radians);
// ENTER FUNCTION PROTOTYPE FOR output_angle() HERE.
void output_angle(double x1, double y1, double x2, double y2, double angle);
// *** DO NOT CHANGE ANY CODE IN THE MAIN FUNCTION.
// *** DO NOT CHANGE ANY CODE IN THE MAIN FUNCTION.
// *** 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 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);
}
// DEFINE FUNCTION normalize() HERE.
void normalize(double & x, double & y)
{
if(((x*x)+(y*y)) != 0)
{
x = x/pow(((x*x)+(y*y)),(1/2));
y = y/pow(((x*x)+(y*y)),(1/2));
}
else if(((x*x)+(y*y)) == 0)
{
x = 0;
y = 0;
}
}
// DEFINE FUNCTION dot_product() HERE.
int dot_product(double x1, double y1, double x2, double y2)
{
double dot(0); // variable for output of dot product
dot = (x1*x2)+(y1*y2);
return(dot);
}
// DEFINE FUNCTION compute_vector_angle() HERE.
int compute_vector_angle(double x1, double y1, double x2, double y2)
{
double radians(0); // variable for output of angle in radians
double product(0); // variable for output of dot_product
normalize(x1,y1);
normalize(x2,y2);
product = dot_product(x1,y1,x2,y2);
radians = acos(product);
return(radians);
}
// DEFINE FUNCTION radians2degrees() HERE.
int radians2degrees(double radians)
{
double degrees(0); // variable for output angle in degrees
degrees = 180*(radians/M_PI);
return(degrees);
}
// DEFINE FUNCTION output_angle() HERE.
void output_angle(double x1, double y1, double x2, double y2, double degrees)
{
cout << "Angle between vectors (" << x1 << "," << y1
<< ") and (" << x2 << "," << y2 << ") = " << degrees
<< " degrees.";
}
|