This is the first version of something I decided to make... it's kinda like a gravity simulator or something. You'll see what I mean (maybe) if you run it.
Currently it's early days because the way the points are moved is by how many other points are above/below and left/right to them. This means that systems usually end up in infinite orbits currently.
Tomorrow I'll change it so the attraction is a function of their mass. You can already see I'm setting up for it.
As usual, if there's an error somehow you should tell me.
By the way, if anyone can score a 20 or higher I'll give them something. Because that's pretty hard.
C++ code:
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <string>
#include <sstream>
#include <Windows.h>
#include <vector>
#include <time.h>
using namespace std;
string converttostr(unsigned int a)
{
stringstream ss;
if(ss << a)
{
return ss.str();
}else
{
return "Fail converting integer type to string type";
}
}
string converttostr(int a)
{
stringstream ss;
if(ss << a)
{
return ss.str();
}else
{
return "Fail converting integer type to string type";
}
}
int converttoint(string a)
{
stringstream ss;
ss << a;
int b;
if(ss >> b)
{
return b;
}else
{
return -1;
}
}
int isum(vector<int> a)
{
int x = 0;
for(int i = 0; i < a.size(); i++)
{
x += a.at(i);
}
return x;
}
const double G = (6.67e-11);
class obj
{
unsigned int mass;
int Vx, Vy;
int x, y;
public:
vector<int> rpath;
vector<int> dpath;
void accelerate(double r, unsigned int M)
{
M = M*pow(10.0,12.0);
if(mass >= 1)
{
cout << "a = " << ((G*M*1000)/(pow(r,2.0))) + 0.5 << endl;
Vx += ((G*M*1000)/pow(r,2.0)) + 0.5;
cout << Vx << endl;
}else
{
Vx = 0;
}
}
void setmass(unsigned int a)
{
mass += a;
}
string getmass()
{
return ((mass >= 1) ? converttostr(mass) : " ");
}
int* getcoords()
{
int temp[2] = {x, y};
return (temp);
}
void clearpath()
{
rpath.clear();
dpath.clear();
}
void clearall()
{
mass = 0;
rpath.clear();
dpath.clear();
Vx = 0;
Vy = 0;
}
void operator= (const obj& other)
{
mass = other.mass;
rpath = other.rpath;
dpath = other.dpath;
Vx = other.Vx;
Vy = other.Vy;
}
obj(unsigned int xco = 0, unsigned int yco = 0, unsigned int m = 0, unsigned int sx = 0, unsigned sy = 0)
{
x = xco;
y = yco;
mass = m;
Vx = sx;
Vy = sy;
}
};
void drawfirstgrid(vector<vector<obj>>& ogrid);
void tick(vector<vector<obj>>& ogrid)
{
//cout << "tick" << endl;
vector<obj> list;
for(int i = 0; i < ogrid.size(); i++)
{
for(int j = 0; j < ogrid[i].size(); j++)
{
if((converttoint(ogrid[i][j].getmass())) >= 1)
{
list.push_back(ogrid[i][j]);
ogrid[i][j].clearall();
}
}
}
for(int i = 0; i < list.size(); i++)
{
int x = (list[i].getcoords()[0]);
x += isum(list[i].rpath);
if(x >= ogrid[0].size())
{
x = ogrid[0].size()-1;
}
//cout << "this x: " << x << endl;
int y = (list[i].getcoords()[1]);
y += isum(list[i].dpath);
if(y >= ogrid.size())
{
y = ogrid.size()-1;
}
//cout << "this y: " << y << endl;
if(converttoint(ogrid[y][x].getmass()) == 0)
{
ogrid[y][x] = list[i];
}else
{
ogrid[y][x].setmass(converttoint(list[i].getmass()));
}
ogrid[y][x].clearpath();
}
drawfirstgrid(ogrid);
}
void pathfind(int Ax, int Ay, int Bx, int By, obj& curobj)
{
/*cout << "pathfind" << endl;
cout << "Obj(" << Ax << "," << Ay << ")" << "has found Obj(" << Bx << "," << By << ")" << endl;*/
int rightvector = -(Ax - Bx);
int downvector = -(Ay - By);
if(rightvector != 0)
{
curobj.rpath.push_back(rightvector/abs(rightvector));
}else
{
curobj.rpath.push_back(0);
}
if(downvector != 0)
{
curobj.dpath.push_back(downvector/abs(downvector));
}else
{
curobj.dpath.push_back(0);
}
}
void interact(obj& curobj, vector<vector<obj>>& ogrid)
{
//cout << "interact" << endl;
for(int i = 0; i < ogrid.size(); i++)
{
for(int j = 0; j < ogrid[i].size(); j++)
{
if((converttoint(ogrid[i][j].getmass())) >= 1)
{
int Ax = curobj.getcoords()[0];
int Ay = curobj.getcoords()[1];
int Bx = ogrid[i][j].getcoords()[0];
int By = ogrid[i][j].getcoords()[1];
if( (Ax - Bx != 0) || (Ay - By != 0) )
{
pathfind(Ax, Ay, Bx, By, curobj);
}
}
}
}
}
void scangrid(vector<vector<obj>>& ogrid)
{
//cout << "scangrid" << endl;
for(int i = 0; i < ogrid.size(); i++)
{
for(int j = 0; j < ogrid[i].size(); j++)
{
if(converttoint(ogrid[i][j].getmass()) >= 1)
{
interact(ogrid[i][j], ogrid);
}
}
}
bool flag = false;
for(int i = 0; i < ogrid.size(); i++)
{
for(int j = 0; j < ogrid[i].size(); j++)
{
if(converttoint(ogrid[i][j].getmass()) >= 1)
{
/*cout << "rs for (" << ogrid[i][j].getcoords()[0] << ", " << ogrid[i][j].getcoords()[1] << ") :";
cout << isum(ogrid[i][j].rpath) << endl;
cout << "ds for (" << ogrid[i][j].getcoords()[0] << ", " << ogrid[i][j].getcoords()[1] << ") :";
cout << isum(ogrid[i][j].dpath) << endl;*/
//flag = true;
}
if(flag){break;}
}
if(flag){break;}
}
//system("pause");
tick(ogrid);
}
void drawfirstgrid(vector<vector<obj>>& ogrid)
{
//cout << "drawfgrid" << endl;
for(int i = 0; i < ogrid.size(); i++)
{
for(int j = 0; j < ogrid[i].size(); j++)
{
cout << ogrid[i][j].getmass() << " ";
}
cout << endl;
}
cout << endl;
string a;
getline(cin, a);
scangrid(ogrid);
}
void setgrid(unsigned int grids[2])
{
//cout << "setgrid" << endl;
srand(time(NULL));
vector<vector<obj>> ogrid;
ogrid.resize(grids[0], vector<obj> (grids[1]));
for(int i = 0; i < grids[0]; i++)
{
for(int j = 0; j < grids[1]; j++)
{
unsigned int rnd = rand() % 100;
if(rnd % 19 == 0)
{
ogrid[i][j].obj::obj(j, i, 1);
}else
{
ogrid[i][j].obj::obj(j, i , 0);
}
}
}
drawfirstgrid(ogrid);
}
int main()
{
cout << "mass is measured in kg*10^12, time in 1000s. Distance is in meters, G is defined as 6.67e-11" << endl;
while(true)
{
unsigned int gridsize[] = {0,0};
string getsize;
try
{
cout << "Enter grid dimensions, x (space) y : ";
if(getline(cin, getsize))
{
for(int i = 0; i <= 1; i++)
{
string temp;
temp = (i == 0) ? getsize.substr(0, getsize.find(' ')) : getsize;
(converttoint(temp) != -1) ? gridsize[i] = converttoint(temp) : throw 0;
getsize.erase(0, getsize.find(' '));
}
cout << "calling setgrid with " << gridsize[0] << "," << gridsize[1] << endl;
setgrid(gridsize);
cout << endl << "--End--" << endl;
//break;
}
throw 0;
}
catch(int a)
{
switch(a)
{
case 0:
cout << "input must be a number!" << endl;
cin.clear();
cin.sync();
break;
case 1:
cout << "grid sizes greater than 20 not allowed" << endl;
break;
}
}
}
system("pause");
return 0;
}
pastebin
Hopefully I included everything.
Oh by the way I'm fairly sure the try/throw/catch methods in main are messed up right now, those will also be fixed.
Last edited by Fear; Apr 3, 2014 at 02:32 AM.