#include <iostream>
#include<math.h>
using namespace std;
class Vect3D
{
friend Vect3D& operator*(double, const Vect3D&) ;
friend ostream& operator<<(ostream&, const Vect3D&);
public:
Vect3D();
Vect3D(double, double, double);
const double& operator[](int i) const;
double& operator[](int i);
Vect3D& operator+(const Vect3D&) const;
Vect3D& operator+=(const Vect3D&);
Vect3D& operator*=(double);
Vect3D& operator*(double) const;
bool operator==(const Vect3D&) const;
static int solveQuadratic(Vect3D &, double&, double&);
private:
double v[3];
};
class Particle {
public:
Particle(double mass, Vect3D &location = Vect3D(0,0,0), Vect3D &velocity = Vect3D(0,0,0));
void applyForce(Vect3D&);
// inline void applyGravity(); w/o implementation would be OK too
inline void applyGravity() {
Vect3D force = Vect3D(0,0,0-g);
force *= mass;
applyForce(force);
};
void tick(double t);
Particle& operator+= (double);
Particle& operator-= (double);
Particle& operator+= (const Particle &);
friend ostream& operator<<(ostream&, Particle const&);
friend istream& operator>>(istream&, Particle&);
friend double collisionTime(Particle &, Particle&);
private:
static const double g=9.81;
static void intersectSolutions(double &t1, double &t2, double tt1, double tt2);
double mass;
Vect3D location;
Vect3D velocity;
Vect3D acceleration;
};
Particle& Particle::operator+= (double mass){
double prevMass = this->mass;
this->mass += mass;
this->acceleration *= (prevMass / this->mass);
return *this;
}
Particle& Particle::operator-= (double mass){
double prevMass = this->mass;
this->mass -= mass;
this->acceleration *= (prevMass / this->mass);
return *this;
}
Particle& Particle::operator+= (const Particle &other) {
if (!(location == other.location)) {
cerr << "Particles not in same location" << endl;
exit(1);
}
double oldMass = mass;
double newMass = mass + other.mass;
velocity = (1/newMass) * (mass * velocity + other.mass * other.velocity);
mass = newMass;
applyForce(oldMass * acceleration + other.mass * other.acceleration);
return *this;
}
ostream& operator<<(ostream &out, const Particle &p) {
out << "Mass : " << p.mass << endl;
out << "Location : " << p.location << endl;
out << "Velocity : " << p.velocity << endl;
out << "Acceleration : " << p.acceleration << endl;
return out;
}
istream& operator>>( istream &in, Particle& p ){
return in;
}
double collisionTime(Particle &p1, Particle&p2) {
Vect3D location = p1.location + -1 * p2.location;
Vect3D velocity = p1.velocity + -1 * p2.velocity;
Vect3D acceleration = p1.acceleration + -1 * p2.acceleration;
Vect3D quadratic[3];
for (int i=0;i<3;i++) {
quadratic[i][0] = acceleration[i]/2;
quadratic[i][1] = velocity[i];
quadratic[i][2] = location[i];
}
double t1, t2;
double tempT1, tempT2;
if (!Vect3D::solveQuadratic(quadratic[0], t1, t2)) return -1;
if (!Vect3D::solveQuadratic(quadratic[1], tempT1, tempT2)) return -1;
Particle::intersectSolutions(t1,t2,tempT1, tempT2);
if (!Vect3D::solveQuadratic(quadratic[2], tempT1, tempT2)) return -1;
Particle::intersectSolutions(t1,t2,tempT1, tempT2);
return t1 != -1 ? t1 : t2;
}
void Particle::intersectSolutions(double &t1, double &t2, double tt1, double tt2) {
if (tt1 == t1 && tt2 == t2) { // both solutions OK, do nothing preserve solution
} else if (tt1 == t1 || tt2 == t1) { // t1 is a good solution, preserve it, destroy t2
t2 = -1;
} else if (tt1 == t2 || tt2 == t2) { // t2 is a good solution, preserve it, destroy t1
t1 = -1;
} else {
t1 = t2 = -1; // No solution is OK
}
}
/*********************************************************************
* End of answer to Question 2
********************************************************************/
Particle::Particle(double mass, Vect3D &location, Vect3D &velocity) {
}
void Particle::applyForce(Vect3D&v) {
return;
}
void Particle::tick(double seconds) {
return;
}
Vect3D::Vect3D() {
v[1]=v[2]=v[3]=0;
}
Vect3D::Vect3D(double d1, double d2, double d3) {
v[1]=d1; v[1]=d2; v[1]=d3;
}
const double& Vect3D::operator[](int i) const {
return v[i];
}
double& Vect3D::operator[](int i) {
return v[i];
}
Vect3D& Vect3D::operator+(const Vect3D &other) const {
Vect3D* ret = new Vect3D(v[1]+other.v[1],v[2]+other.v[2],v[3]+other.v[3]);
return *ret;
}
Vect3D& Vect3D::operator+=(const Vect3D &other) {
for (int i=0; i<3; i++) {
v[i]+=other.v[i];
}
return *this;
}
Vect3D& Vect3D::operator*=(double d) {
for (int i=0; i<3; i++) {
v[i]*=d;
}
return *this;
}
Vect3D& Vect3D::operator*(double d) const {
Vect3D *ret = new Vect3D(v[1]*d,v[2]*d,v[3]*d);
return *ret;
}
bool Vect3D::operator==(const Vect3D &other) const {
for(int i=0; i<3; i++) {
if (other.v[i] != v[i]) return false;
}
return true;
}
/**************************************************
* static methods
* ***********************************************/
int Vect3D::solveQuadratic(Vect3D&v, double&s1, double&s2) {
double discriminant = v.v[1]*v.v[1] - 4 * v.v[0] * v.v[2];
if (discriminant < 0) return 0;
else if (discriminant == 0) {
s1 = s2 = v.v[1] / (-2 * v.v[0]);
return 1;
} else {
s1 = (-1 * v.v[1] + sqrt(discriminant)) / (2 * v.v[0]);
s2 = (-1 * v.v[1] - sqrt(discriminant)) / (2 * v.v[0]);
return 2;
}
}
/**************************************************
* friends
* ***********************************************/
Vect3D& operator*(double d, const Vect3D&v) {
return v*d;
}
ostream& operator<<(ostream &out, const Vect3D &v) {
out << '(' << v.v[1] << ',' << v.v[2] << ',' << v.v[3] << ')';
return out;
}
static double const PI = 3.14;
int main(int argc, char *argv[]) {
double m, R;
// Get input and initialize particle
cout << "Enter mass and then radius: " << endl;
cin >> m >> R;
double omega = PI/180;
Vect3D location = Vect3D(0,R,0);
Vect3D velocity = Vect3D(omega * R, 0, 0);
Particle p = Particle(m, location, velocity);
// The absolute value of the force remains the same, however the angle changes
// therefore we calculate it only once
double absoluteForce = m * omega * omega * R;
// At each second recalculate the force and apply the momentary force
for (int i=0; i < 360; i++) {
double angle = i * omega;
Vect3D force;
force[0]= absoluteForce * sin(angle);
force[1]= 0- absoluteForce * cos(angle);
p.applyForce(force);
p.tick(1);
cout << p;
}
system("pause");
return 0;
}
snvngrc1 0 Newbie Poster
snvngrc1 0 Newbie Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
snvngrc1 0 Newbie Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
snvngrc1 0 Newbie Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
mrnutty 761 Senior Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
snvngrc1 0 Newbie Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
snvngrc1 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.