|
#include "StdAfx.h"
#include "StarVector.h"
const double StarVector::PI_2 = 6.283185307179586476925286766559;
StarVector::StarVector(int iX, int iY)
: X(iX), Y(iY)
{
}
StarVector::StarVector(double dblAngle, double dblDistance)
{
setVector(dblAngle, dblDistance);
}
StarVector::StarVector(const StarVector& refVector)
{
X = refVector.X;
Y = refVector.Y;
}
StarVector::~StarVector(void)
{
}
StarVector& StarVector::operator=(const StarVector& refVector)
{
X = refVector.X;
Y = refVector.Y;
return *this;
}
StarVector& StarVector::operator+=(const StarVector& refVector)
{
X += refVector.X;
Y -= refVector.Y;
return *this;
}
void StarVector::setVector(double dblAngle, double dblDistance)
{
// reset angle [1,9[ -> [0,8[ -> [0,2PI[
//
dblAngle -= 1.0;
dblAngle *= PI_2;
dblAngle /= 8.0;
double dblX = dblDistance * cos(dblAngle);
double dblY = dblDistance * sin(dblAngle);
X = static_cast<int>(dblX);
Y = static_cast<int>(dblY);
}
StarVector StarVector::operator-(const StarVector& refVector)
{
int iX = X - refVector.X;
int iY = Y - refVector.Y;
return StarVector(iX, iY);
//return StarVector(abs(iX), abs(iY));
}
void StarVector::getVector(double& dblAngle, double& dblDistance)
{
dblDistance = sqrt(static_cast<double>(X*X + Y*Y));
if(X == 0)
{
dblAngle = (Y < 0) ? 3.0 : 7.0;
return;
}
else
{
if(Y == 0)
{
dblAngle = (X > 0) ? 1.0 : 5.0;
return;
}
else
{
double dblFoo = atan2(static_cast<double>(abs(Y)), static_cast<double>(abs(X)));
if(X > 0)
{
if(Y > 0)
{
dblAngle = 9.0 - (8.0 * dblFoo) / static_cast<double>(PI_2);
}
else
{
dblAngle = 1.0 + (8.0 * dblFoo) / static_cast<double>(PI_2);
}
}
else
{
if(Y > 0)
{
dblAngle = 5.0 + (8.0 * dblFoo) / static_cast<double>(PI_2);
}
else
{
dblAngle = 5.0 - (8.0 * dblFoo) / static_cast<double>(PI_2);
}
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I'm from Belgium, happily (should my wife ever read this) married and have two boys.
After a first attempt on the Atari during college in
an old foreign dialect of basic, via Pascal I am now
developing mostly in VC++ / VB / C#.