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
|
#include <cmath>
#include <iostream>
#ifndef PI
#define PI 3.14159265
#endif
using std::cout;
using std::endl;
struct Vector{float i,j,k;}; //vector struct
struct Point{float x,y,z;}; //cord struct
//functions
float GetAngle(Vector v1, Vector v2, float *a, float *b);
float Magnitude(Vector v1);
Vector GetVec(Point a, Point b);
int main()
{
//create two vectors to compare
Vector myVec1 = {0};
Vector myVec2 = {0};
//create two sets of points (you would read them in)
Point a1 = {1,5,-7};
Point a2 = {-2,12,19};
Point b1 = {12,-9,1};
Point b2 = {0,-7,1};
//find out the vectors between the points
myVec1 = GetVec(a1,a2);
myVec2 = GetVec(b1,b2);
//create floats to hold the magnitudes of the two vectors
float magnitude1 = 0.f;
float magnitude2 = 0.f;
//create variable to store the angle
float angle = 0.f;
//work out the angle
angle = GetAngle(myVec1, myVec2, &magnitude1, &magnitude2);
//output results (you would write this to your file)
cout << angle << endl;
cout << magnitude1 << endl;
cout << magnitude2 << endl;
return 0;
}
float GetAngle(Vector v1, Vector v2, float *a, float *b)
{
float tempAngle;
float DotProduct;
//store the magnitude values via our pointers
*a = Magnitude(v1);
*b = Magnitude(v2);
//work out the dot product of the two vectors
DotProduct = ((v1.i * v2.i)+(v1.j*v2.j)+(v1.k * v2.k));
//angle = inv cosine of dot product divided the the product of the magnitude of vector a and vector b
tempAngle = acos(DotProduct/((*a) * (*b)));
//return the angle in degrees
return tempAngle*180/PI;
}
float Magnitude(Vector v1)
{
return sqrt(pow(v1.i, 2) + pow(v1.j, 2) + pow(v1.k, 2)); //return the magnitude (pythagoras)
}
Vector GetVec(Point a, Point b)
{
Vector temp = {0};
temp.i = b.x - a.x;
temp.j = b.y - a.y;
temp.k = b.z - a.z;
return temp; //return vector between two points
}
|