Vectors

 

Vector:

-magnitude, (distance)

-bearing – degrees from north 0 ->360

-width

-height

//See pg 429, overloading restrictions

Class Vector

{

Private:

double m_dMag;

double m_dBear;

double m_dWidth;

dou\uble m_dHeight;

___________________________________________

Public:

Vector(); //initialize to 0’s

//Vector(double dMag, double dBear);

//Vector(double dWidth, double dHeight);

//Because Vector constructors have the same name, but different arguements you must distinguish between the three.....some ways

//1) Add Dummy Argument

2) Change one of the constructors to take floats or

ints.

  1. Combine constructors and add a boolean flag.

//using 2)

explicit Vector(double dMag, double dBear);

explicit Vector (float dWidth, float dHeight);

//using 3), preferred

Vector (double d1 double d2 bool bPolar = true);

___________________________________________

public:

Vector ();

Private:

double CalcWidth();

double CalcHeight();

double CalcMag();

double CalcBear();

Vector (double d1, double d2, bool bPolar = true);

Vector operator + (const vector&) const;

Vector oeprator – (const Vector&) const;

Vector operator –()const;

};

Vector operator * (double d1, const Vector&v);

}

int main()

{

Vector v1; //values ???

Vector v2 (100.5, 90.0);

// Vector v2(100.5F, 90.oF); // if use 2) in class

//Vector v3 (100.4, 100.4);

Vector v3(100.4, 100.4,false); //using 3) in class

int x=5;

int y=7;

int z = x+y;

Vector V1 (100, 61);

Vector V2 (300, -100, false);

Vector V3 = V2 +V1;

Vector V4 =V3 *7.0;

Vector V5 = V4 – V3;

Vector V6 = -V5;

Vector 7 = 14.62 *V2;

}

Vector :: Vector();

m_dMag(0.0),

m_dBear(0.0),

m_dWidth(0.0),

m_dHeight(0.0)

{}

Vector::Vector()

{

m_dMag = 0.0;

m_dBear = 0.0;

m_dWidth = 0.0;

m_dHeight = 0.0;

}

Vector::Vector(double d1, double d2, bool bPolar))

{

if(bPolar == true)

{

m_dMag = d1;

m_dBear = d2;

m_dHeight = calcHeight();

m_dWidth = CalcWidth();

}

else

{

m_dWidth = d1;

m-dHeight = d2;

m_dMag = CalcMag();

m_dBear = CalcBear();

}

}

double Vector::CalcMag()

{

//precondition to this function: width and height are valid values.

return sqrt ((m_dWidth * m_dWidth) + (m_dHeight * m_dHeight));

}

double Vector::CalcBear()

{

//use of atan2 – y/x , must handle 0,0 vector.

if ((n_dWidth == 0.00) && (m_dHeight == 0.0))

//problem with the equality function ‘==’ and a double ‘n_dWidth

//AreEqual, Ross’ function

if (AreEqual(m_dWidth, 0.00)&& AreEqual(m_dHeight, 0.00))

{

return 0.00;

}

double dRad = atan2(m_dWidth, m_dHeight);

return dRad * 57.295779;

//above line, to convert radians (the return of atan2) to degrees

double Vector :: CalWidth()

{

return = m_dMag * sin(m_dBear / 57.295779);

}

double Vector::CalcHeight()

{

return = m_dMag * cos(m-dBear / 57.295779);

}

//overloaded operator

//when passing an object as a paramteter, pass it by reference.

Vector Vector ::Operator + (const Vector& v) const

{

Vector ret(m_dWidth + v. m_dWidth, m_dHeight + v. m_dHeight, false);

return ret;

}

Vector Vector::operator * (double d1) const

{

Vector ret (m_dWidth* d1, m_dHeight * d1, false);

return ret;

}

Vector Vector :: Operator – (const Vector& v)const

{

vector ret(m_dWidth – v. M_dWidth, M-dHeight – v. M-dHeight, false);

return ret;

}

Vector Vector::Operator – () const

{

vector ret(-m_dwidth, - M_dHeight, false);

return ret;

}

//Vector operator* (double d1, const Vector& v)

{

return r*d1;

Vector re(v.n_dWidth*d1, v. n_dHeight * d1, false);

returnret;

}

//to use with class vector:

//Vector operator *(double d1, const Vector& v);

 

 

 

Back | HOME | Next




Canpol Solutions Przeszukaj Katalog -- przeszukaj katalog / zamowienia, komentarze --  Poczta Polonijnej Witryny

Internet ExplorerUzywaj zawsze najnowszej przegladarki
M.I.E. lub, ewentualnie Netscape ktorej, niestety na razie nie polecamy..
Netscape


Canpol Solutions © Polonijna Witryna