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
|
#include "vector.h"
#include "math.h"
Vector::Vector(){
x = y = z = 0.0f;
}
Vector::Vector(float X,float Y, float Z){
x = X; y = Y; z = Z;
}
Vector::~Vector()
{}
float Vector::operator [ ](int i) const{
switch(i){
case 0: return x;
case 1:return y;
case 2: return z;
default :
std::cout << "ERROR ....";
return x;
}
//return something
return x;
}
float Vector::length() const{
return sqrt(x*x + y*y + z*z);
}
float Vector::dot(const Vector &v) const {
return (x*v.x + y*v.y + z*v.z);
}
const Vector Vector::norm() const{
return Vector( 0.0,0.0,0.0 );
}
const Vector Vector::cross(const Vector &v) const {
return Vector( (y*v.z) - (z*v.y) ,((z*v.x)-(x*v.z)),((x*v.y)-(y*v.x)) );
}
// C++ print routine
std::ostream & operator<< (std::ostream & ostr, Vector v){
ostr << "(" << v.x << ", " << v.y << ", " << v.z << ")";
return ostr;
}
const Vector operator -(const Vector &v){
return Vector( -v.x,-v.y,-v.z );
}
//Addition
const Point operator+ (const Point &p, const Vector &v){
return Point((p.x + v.x),(p.y + v.y),(p.z + v.z));
}
const Vector operator+ (const Vector &v, const Vector &w){
return Vector( v.x+w.y,v.y+w.y,v.z+w.z );
}
// Subtraction
const Point operator- (Point p, Vector v){
return Point( p.x-v.x,p.y-v.y,p.z-v.z );
}
const Vector operator- (Vector v, Vector w){
return Vector( v.x-w.x,v.y-w.y,w.z-w.z );
}
const Vector operator- (const Point &tail, const Point &head){
return Vector( tail.x -head.x ,tail.y - head.y,tail.z -head.z );
}
// Scalar Multiplication
const Vector operator* (float s, const Vector &v){
return Vector( s * v.x,s * v.y,s * v.z );
}
const Vector operator* (const Vector &v, float s){
return Vector( v.x*s,v.y*s,v.z*s );
}
|