Skip to content

Instantly share code, notes, and snippets.

@nickstenning
Created January 31, 2009 18:51
Show Gist options
  • Save nickstenning/55625 to your computer and use it in GitHub Desktop.
Save nickstenning/55625 to your computer and use it in GitHub Desktop.
#import "planet.h"
#include <iostream>
using namespace std;
const double G = 1;
const double m_sun = 1;
const double m_earth = 1;
void leapfrog_step(std::vector<Particle>& particles, double dt) {
for (vector<Particle>::iterator p = particles.begin(); p != particles.end(); ++p) {
}
}
int main (int argc, char * const argv[]) {
Simulator sim(leapfrog_step);
}
#include "planet.h"
#include <cmath>
#include <iostream>
using namespace std;
xyvect::xyvect(double x0, double y0): x(x0), y(y0) {}
double xyvect::normsq() { return x*x + y*y; }
double xyvect::norm() { return sqrt(normsq()); }
double xyvect::ang() { return atan2(y,x); }
Particle::Particle(double mass, xyvect x0, xyvect v0): m(mass), x(x0), v(v0) {}
double Particle::mass() { return m; }
Simulator::Simulator(void(*stepFn)(std::vector<Particle>&, double)): step(stepFn) {}
void Simulator::add(Particle &p) {
particles.push_back(p);
}
int Simulator::run() {
for (vector<Particle>::iterator p = particles.begin(); p != particles.end(); ++p) {
cout << (*p).mass() << endl;
}
return 0;
}
// void leapfrog_step(std::vector<Particle>& particles, double dt) {
// for (vector<Particle>::iterator p = v.begin(); p != v.end(); ++p) {
// *p.x_inc(0.5*dt);
// *p.a_calc();
// *p.v_inc(dt);
// *p.x_inc(0.5*dt);
// *p.t_inc(dt);
// }
// }
//
// void euler_step(std::vector<Particle>& particles, double dt) {
// for (vector<Particle>::iterator p = v.begin(); p != v.end(); ++p) {
// *p.a_calc();
// *p.x_inc(dt);
// *p.v_inc(dt);
// *p.t_inc(dt);
// }
// }
//
#include <vector>
#ifndef PLANET_H
#define PLANET_H
class xyvect {
public:
xyvect(double xx, double yy);
double normsq();
double norm();
double ang();
private:
double x, y;
};
class Particle {
public:
Particle(double mass, xyvect x0, xyvect v0);
double mass();
private:
double m;
xyvect x;
xyvect v;
};
class Simulator {
public:
Simulator(void(*step)(std::vector<Particle>&, double));
void add(Particle &p);
int run();
private:
std::vector<Particle> particles;
void(*step)(std::vector<Particle>&, double);
};
void leapfrog_step(std::vector<Particle>& particles, double dt)
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment