AKFourtySeven
09.02.2009, 01:29
hier ne Vectorklasse
jetzt haben wir beide nen schnipsel
spieln wir jetzt "schnipseljagd" ;)
using namespace std;
class mVect
{
private:
// NO PRIVATE DATA
public:
double i, j, k;
mVect();
mVect(string); // NEEDS EXTRA LIBRARIES
mVect(double,double);
mVect(double,double,double);
mVect operator + (mVect);
mVect operator - (mVect);
mVect operator = (mVect);
mVect operator = (string); // NEEDS EXTRA LIBRARIES
mVect operator / (mVect);
double operator % (mVect);
double operator ^ (mVect);
mVect operator * (double);
mVect operator / (double);
mVect operator * (mVect);
double mod();
mVect unit();
void print(); // NEEDS EXTRA LIBRARIES
};
// INITIALIZE EMPTY VECTOR
mVect::mVect()
{
i=0;
j=0;
k=0;
}
// OUTPUT THE VECTOR IN THE FORM [i,j,k]
void mVect::print()
{
cout << "[" << i << "," << j << "," << k << "]";
}
// INITIALIZE VECTOR FROM A STRING IN THE FORM [i,j,k]
mVect::mVect(string v)
{
size_t p[2]={1,1}, found;
while (v[p[1]]!=',')
p[1]++;
stringstream(v.substr(p[0],p[1]-p[0])) >> i;
p[0]=++p[1];
while (v[p[1]]!=',' && v[p[1]]!=']')
p[1]++;
stringstream(v.substr(p[0],p[1]-p[0])) >> j;
if (v[p[1]]==']')
k=0;
else
{
p[0]=++p[1];
while (v[p[1]]!=']')
p[1]++;
stringstream(v.substr(p[0],p[1]-p[0])) >> k;
}
}
// INITIALIZE 2D VECTOR
mVect::mVect(double a, double b)
{
i=a;
j=b;
k=0;
}
// INITIALIZE 3D VECTOR
mVect::mVect(double a, double b, double c)
{
i=a;
j=b;
k=c;
}
// ADD TWO VECTORS TOGETHER
mVect mVect::operator + (mVect param)
{
mVect c;
c.i=i+param.i;
c.j=j+param.j;
c.k=k+param.k;
return (c);
}
// SUBTRACT ONE VECTOR FROM ANOTHER
mVect mVect::operator - (mVect param)
{
mVect c;
c.i=i-param.i;
c.j=j-param.j;
c.k=k-param.k;
return (c);
}
// SET A VECTOR TO ANOTHER VECTOR
mVect mVect::operator = (mVect param)
{
i=param.i;
j=param.j;
k=param.k;
return *this;
}
// SET A VECTOR TO A STRING IN THE FORM [i,j,k]
mVect mVect::operator = (string v)
{
mVect temp(v);
*this = temp;
return temp;
}
// CROSS PRODUCT
mVect mVect::operator / (mVect param)
{
mVect c;
c.i=j*param.k-k*param.j;
c.j=k*param.i-i*param.k;
c.k=i*param.j-j*param.i;
return (c);
}
// DOT PRODUCT
double mVect::operator % (mVect param)
{
return (i*param.i+j*param.j+k*param.k);
}
// ANGLE BETWEEN IN RADIANS
double mVect::operator ^ (mVect b)
{
mVect a=*this;
return (acos((a%b)/(a.mod()*b.mod())));
}
// MULTIPLY BY REAL
mVect mVect::operator * (double b)
{
mVect c;
c.i=i*b;
c.j=j*b;
c.k=k*b;
return (c);
}
// DIVIDE BY REAL
mVect mVect::operator / (double b)
{
mVect c;
c.i=i/b;
c.j=j/b;
c.k=k/b;
return (c);
}
// STRETCH VECTORS
mVect mVect::operator * (mVect b)
{
mVect c;
c.i=i*b.i;
c.j=j*b.j;
c.k=k*b.k;
return (c);
}
// MODULUS
double mVect::mod()
{
return sqrt(pow(i,2)+pow(j,2)+pow(k,2));
}
// UNIT VECTOR
mVect mVect::unit()
{
mVect c;
c.i=i/this->mod();
c.j=j/this->mod();
c.k=k/this->mod();
return c;
}
jetzt haben wir beide nen schnipsel
spieln wir jetzt "schnipseljagd" ;)
using namespace std;
class mVect
{
private:
// NO PRIVATE DATA
public:
double i, j, k;
mVect();
mVect(string); // NEEDS EXTRA LIBRARIES
mVect(double,double);
mVect(double,double,double);
mVect operator + (mVect);
mVect operator - (mVect);
mVect operator = (mVect);
mVect operator = (string); // NEEDS EXTRA LIBRARIES
mVect operator / (mVect);
double operator % (mVect);
double operator ^ (mVect);
mVect operator * (double);
mVect operator / (double);
mVect operator * (mVect);
double mod();
mVect unit();
void print(); // NEEDS EXTRA LIBRARIES
};
// INITIALIZE EMPTY VECTOR
mVect::mVect()
{
i=0;
j=0;
k=0;
}
// OUTPUT THE VECTOR IN THE FORM [i,j,k]
void mVect::print()
{
cout << "[" << i << "," << j << "," << k << "]";
}
// INITIALIZE VECTOR FROM A STRING IN THE FORM [i,j,k]
mVect::mVect(string v)
{
size_t p[2]={1,1}, found;
while (v[p[1]]!=',')
p[1]++;
stringstream(v.substr(p[0],p[1]-p[0])) >> i;
p[0]=++p[1];
while (v[p[1]]!=',' && v[p[1]]!=']')
p[1]++;
stringstream(v.substr(p[0],p[1]-p[0])) >> j;
if (v[p[1]]==']')
k=0;
else
{
p[0]=++p[1];
while (v[p[1]]!=']')
p[1]++;
stringstream(v.substr(p[0],p[1]-p[0])) >> k;
}
}
// INITIALIZE 2D VECTOR
mVect::mVect(double a, double b)
{
i=a;
j=b;
k=0;
}
// INITIALIZE 3D VECTOR
mVect::mVect(double a, double b, double c)
{
i=a;
j=b;
k=c;
}
// ADD TWO VECTORS TOGETHER
mVect mVect::operator + (mVect param)
{
mVect c;
c.i=i+param.i;
c.j=j+param.j;
c.k=k+param.k;
return (c);
}
// SUBTRACT ONE VECTOR FROM ANOTHER
mVect mVect::operator - (mVect param)
{
mVect c;
c.i=i-param.i;
c.j=j-param.j;
c.k=k-param.k;
return (c);
}
// SET A VECTOR TO ANOTHER VECTOR
mVect mVect::operator = (mVect param)
{
i=param.i;
j=param.j;
k=param.k;
return *this;
}
// SET A VECTOR TO A STRING IN THE FORM [i,j,k]
mVect mVect::operator = (string v)
{
mVect temp(v);
*this = temp;
return temp;
}
// CROSS PRODUCT
mVect mVect::operator / (mVect param)
{
mVect c;
c.i=j*param.k-k*param.j;
c.j=k*param.i-i*param.k;
c.k=i*param.j-j*param.i;
return (c);
}
// DOT PRODUCT
double mVect::operator % (mVect param)
{
return (i*param.i+j*param.j+k*param.k);
}
// ANGLE BETWEEN IN RADIANS
double mVect::operator ^ (mVect b)
{
mVect a=*this;
return (acos((a%b)/(a.mod()*b.mod())));
}
// MULTIPLY BY REAL
mVect mVect::operator * (double b)
{
mVect c;
c.i=i*b;
c.j=j*b;
c.k=k*b;
return (c);
}
// DIVIDE BY REAL
mVect mVect::operator / (double b)
{
mVect c;
c.i=i/b;
c.j=j/b;
c.k=k/b;
return (c);
}
// STRETCH VECTORS
mVect mVect::operator * (mVect b)
{
mVect c;
c.i=i*b.i;
c.j=j*b.j;
c.k=k*b.k;
return (c);
}
// MODULUS
double mVect::mod()
{
return sqrt(pow(i,2)+pow(j,2)+pow(k,2));
}
// UNIT VECTOR
mVect mVect::unit()
{
mVect c;
c.i=i/this->mod();
c.j=j/this->mod();
c.k=k/this->mod();
return c;
}