Hello everyone.
Hope you guys havnt given up on me yet. I think I have a solution now that might work, not sure if it's any better than anonytmouse suggestion but it's a bit different. Must admit I have not tested it very thoroughly yet but it seems to work on the 'few' test runs I've done.
My idea is this, using the time difference between receiver1 and receiver2 I can calculate an arc of difference possible points between the two points. These samples I then place inside an array. The I do the same thing between receiver1 and receiver3. Now I have two arc's that at one point meet, which is the coordinate of the sender.
I apologise for the lack of comments in the code.
Would appreciate any comments or advice.
Code:
//main.cpp
#include <iostream>
#include <cstdlib>
#include "ratio.h"
using namespace std;
int main(int argc, char *argv[])
{
double time1=0;
double time2=0;
double time3=0;
cout << endl << "Enter the time registered at receiver1 (0,0) : ";
cin >> time1;
while (!cin)
{
cout << endl << "Not a number! Enter time for receiver1 again : ";
cin.clear();
cin.ignore(0x7FFFL,'\n');
cin >> time1;
}
cout << endl << "Enter the time registered at receiver2 (0,10) : ";
cin >> time2;
while (!cin)
{
cout << endl << "Not a number! Enter time for receiver2 again : ";
cin.clear();
cin.ignore(0x7FFFL,'\n');
cin >> time2;
}
ratio receiver1(time2 - time1, 0);
receiver1.calculatePath();
cout << endl << "Enter the time registered at receiver3 (10,0) : ";
cin >> time3;
while (!cin)
{
cout << endl << "Not a number! Enter time for receiver3 again : ";
cin.clear();
cin.ignore(0x7FFFL,'\n');
cin >> time3;
}
ratio receiver2(time3 - time1, 1);
receiver2.calculatePath();
if (receiver1.interception(receiver2))
{
cout << endl << "intercpection" << '\t';
cout << "x = " << receiver1.getInterceptionX() << "\ty= " << receiver1.getInterceptionY() << endl << endl;;
}
else
{
cout << endl << "no interception...failed" << endl << endl;
}
system("PAUSE");
return 0;
}
Code:
class ratio
{
public:
ratio(double delay, int pos);
~ratio();
void calculatePath();
bool interception(ratio &obj);
inline double getInterceptionX() { return collisionX; }
inline double getInterceptionY() { return collisionY; }
inline double getPositionX(int counter) { return coordinates[X][counter];}
inline double getPositionY(int counter) { return coordinates[Y][counter]; }
private:
void showarray();
double getA();
double getB();
double getC();
double getPos();
double solve(double a, double b, double c);
bool isAnswerCorrect(double value);
double delay;
static const int X = 0;
static const int Y = 1;
static const int GRID_SIZE = 10;
static const double RESOLUTION = 0.05;
static const int POINTS = (int) (GRID_SIZE / RESOLUTION);
static const double SPEED = 1.0;
static const double UNCERTAINTY = 0.002;
double coordinates[2][POINTS+1];
double index;
double collisionX;
double collisionY;
int posistion;
};
Code:
#include "ratio.h"
#include <iostream>
#include <cmath>
using namespace std;
ratio::ratio(double d, int pos) : delay(d * SPEED), posistion(pos) {}
ratio::~ratio() {}
void ratio::calculatePath()
{
int counter=0;
for (index = 0; index <= (GRID_SIZE); index += RESOLUTION )
{
if (posistion == 0)
{
coordinates[X][counter] = getPos();
coordinates[Y][counter] = index;
}
else if (posistion == 1)
{
coordinates[X][counter] = index;
coordinates[Y][counter] = getPos();
}
counter++;
}
// showarray();
}
void ratio::showarray()
{
for (int counter=0; counter <= POINTS; counter++ )
cout << "X"<<counter<<" = " << coordinates[X][counter] << '\t' << "Y"<<counter<< " = " << coordinates[Y][counter] << endl;
}
double ratio::getPos()
{
double a = getA();
double b = getB();
double c = getC();
// cout << '\t' << a<<"x^2 " << b << "x " << c << endl;
return solve(a, b, c);
}
double ratio::getA()
{
return (pow((GRID_SIZE / delay), 2) - 1);
}
double ratio::getB()
{
return -2 * (( ( (GRID_SIZE*GRID_SIZE) - pow(delay,2)) / (2 * delay) ) * (GRID_SIZE / delay));
}
double ratio::getC()
{
double temp = ( (GRID_SIZE*GRID_SIZE) - pow(delay,2) ) / (2 * delay);
return pow (temp, 2) - pow(index,2);
}
double ratio::solve(double a, double b, double c)
{
// cout << a << '\t' << b << '\t' << c << endl;
double x1 = (-b - sqrt( pow(b,2) - (4*a*c) )) / (2*a);
double x2 = (-b + sqrt( pow(b,2) - (4*a*c) )) / (2*a);
// cout << "x1= " << x1 << "\tx2 = " << x2 << endl;
if ( isAnswerCorrect(x1) )
return x1;
else if (isAnswerCorrect(x2))
return x2;
else
return 0;
}
bool ratio::isAnswerCorrect(double answer)
{
double left = sqrt( pow((GRID_SIZE - answer),2) + pow(index,2) );
double right = delay + sqrt( pow(answer,2) + pow(index,2) );
// cout << endl << "left = " << left << "\tright = " << right << '\t';
if ( fabs(right - left) < UNCERTAINTY )
return true;
else
return false;
}
bool ratio::interception(ratio &obj)
{
for (int index=0; index < POINTS; index++)
{
for (int index2=0; index2 < POINTS; index2++)
{
if (fabs(getPositionX(index) - obj.getPositionX(index2)) < UNCERTAINTY)
{
if ( fabs(getPositionY(index) - obj.getPositionY(index2)) < UNCERTAINTY)
{
collisionX = getPositionX(index);
collisionY = getPositionY(index);
return true;
}
}
}
}
return false;
}