The arithmetics of the program are incorrect
mvector.h
Code:
#include <cmath>
#ifndef _MVECTOR_H
#define _MVECTOR_H
namespace math
{
class Vector
{
public:
float x;
float y;
float z;
Vector(float a=0,float b=0,float c=0)
{
x=a;
y=b;
z=c;
}
Vector operator+(Vector &v1)
{
Vector tmp;
tmp.x=v1.x+x;
tmp.y=v1.y+y;
tmp.z=v1.z+z;
return tmp;
};
Vector operator-(Vector v1)
{
Vector tmp;
tmp.x=x-v1.x;
tmp.y=y-v1.y;
tmp.y=z-v1.z;
return tmp;
};
float VecLength(Vector v)
{
return sqrt(pow(v.x,2)+pow(v.y,2)+pow(v.z,2));
};
float DotProduct(Vector v1,Vector v2)
{
return (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
}
Vector CrossProduct(Vector v1, Vector v2)
{
Vector temp;
temp.x=v1.y*v2.z - v1.z*v2.y;
temp.y=v1.z*v2.x - v1.x*v2.z;
temp.z=v1.x*v2.y - v1.y*v2.x;
return temp;
};
};
}
#endif
main.cpp
Code:
#include <iostream>
#include <cmath>
#include "Mvector.h"
using namespace math;
using namespace std;
Vector a= Vector(1,-8,-3);
Vector b=Vector(2,2,3);
Vector c= a-b;
int main()
{
std::cout <<c.x<< std::endl;
std::cout <<c.y<< std::endl;
std::cout <<c.z<< std::endl;
return 0;
}
for some reason it outputs:
-1
-6
0
whats wrong?