#include <iostream>#include <iomanip>#include <cmath>#include <ctime>using namespace std;class Vector { double x, y, z;public: Vector(); Vector(double x, double y, double z); friend ostream &operator<<(ostream &stream, const Vector &v); friend Vector operator+(const Vector &v1, const Vector &v2); friend Vector operator-(const Vector &v1, const Vector &v2); friend Vector operator*(const double &m, const Vector &v); friend Vector operator*(const Vector &v, const double &m); friend double operator%(const Vector &v1, const Vector &v2);};Vector::Vector(){}Vector::Vector(double x, double y, double z): x(x), y(y), z(z){}ostream &operator<<(ostream &stream, const Vector &v){ stream << '(' << v.x << ',' << v.y << ',' << v.z << ')'; return stream;}Vector operator+(const Vector &v1, const Vector &v2){ return Vector(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);}Vector operator-(const Vector &v1, const Vector &v2){ return Vector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);}Vector operator*(const double &m, const Vector &v){ return Vector(m * v.x, m * v.y, m * v.z);}Vector operator*(const Vector &v, const double &m){ return Vector(m * v.x, m * v.y, m * v.z);}double operator%(const Vector &v1, const Vector &v2){ return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);}Vector intercept(const Vector &p_shooter, double bullet_speed, const Vector &p_target, const Vector &v_target){ Vector BA = p_target - p_shooter; double vxv = v_target % v_target; double b = BA % v_target; double a = bullet_speed * bullet_speed - vxv; return p_target + (b + sqrt(b*b + a * (BA%BA))) / a * v_target;}Vector shoot_at(const Vector &p_shooter, const Vector &p_interception, double bullet_speed){ Vector v = p_interception-p_shooter; return bullet_speed/sqrt(v%v) * v;}int main(){ unsigned int c, n; unsigned int EVENT_COUNT; bool q; // ask the user cout << "Enter number of shots to calculate: " << endl; cin >> EVENT_COUNT; // allocate the memory cout << "allocating memory" << endl; Vector *p_shooter = new(nothrow) Vector[EVENT_COUNT]; Vector *p_target = new(nothrow) Vector[EVENT_COUNT]; double *bullet_speed = new(nothrow) double[EVENT_COUNT]; Vector *v_target = new(nothrow) Vector[EVENT_COUNT]; Vector *p_hit = new(nothrow) Vector[EVENT_COUNT]; Vector *v_shoot = new(nothrow) Vector[EVENT_COUNT]; if(!p_shooter || !p_target || !bullet_speed || !v_target || !p_hit || !v_shoot) { cout << "failed!" << endl; return 1; } else cout << "completed" << endl; // randomize timer cout << "randomize_timer: 1 second" << endl; c = clock() + 1000; while(clock() < c) rand(); cout << "randomize_timer completed\n" << endl; // simulate EVENT_COUNT shots cout << "calculating " << EVENT_COUNT << " events" << endl; c = clock(); for(n = 0; n < EVENT_COUNT; n++) { *p_shooter = Vector((rand() % 101) - 50, (rand() % 101) - 50, (rand() % 101) - 50); *p_target = Vector((rand() % 101) - 50, (rand() % 101) - 50, (rand() % 101) - 50); *bullet_speed = (rand() % 5) + 10; *v_target = Vector((rand() % 7) - 3, (rand() % 7) - 3, (rand() % 7) - 3); *p_hit = intercept(*p_shooter, *bullet_speed, *p_target, *v_target); *v_shoot = shoot_at(*p_shooter, *p_hit, *bullet_speed); p_shooter++; p_target++; bullet_speed++; v_target++; p_hit++; v_shoot++; } cout << "calculation finished in " << clock() - c << " milliseconds" << endl; // ask the user cout << "do u want to see the results? (1/0)" << endl; cin >> q; if(!q) return 0; // display 4xEVENT_COUNT lines of bulk data cout << "displaying calculations:" << endl; p_shooter -= EVENT_COUNT; p_target -= EVENT_COUNT; bullet_speed -= EVENT_COUNT; v_target -= EVENT_COUNT; p_hit -= EVENT_COUNT; v_shoot -= EVENT_COUNT; for(n = 0; n < EVENT_COUNT; n++) { cout << clock(); cout << "shooter loc: " << *p_shooter << ", target loc: " << *p_target << '\n'; cout << "bullet's speed: " << *bullet_speed << ", target dir " << *v_target << '\n'; cout << "shoot dir: " << *v_shoot << ", intercept loc: " << *p_hit << '\n' << endl; p_shooter++; p_target++; bullet_speed++; v_target++; p_hit++; v_shoot++; } return 0;}
The Vector and the Point is now combined, also operator% is used as the dot product. I've made optimizations where I could. The results are quite satisfactory: on my PC one million of these calculations take only one second. Thanks alvaro, your help is greatly appreciated!
There's something I concluded from this. Geometry sucks! :)