This commit is contained in:
Joshua Coles 2023-02-09 12:41:41 +00:00
parent f79763eee4
commit 496c1022db
7 changed files with 428 additions and 404 deletions

View File

@ -6,11 +6,11 @@
// colors // colors
namespace colours { namespace colours {
GLfloat blue[] = { 0.1, 0.3, 0.9, 1.0 }; // blue GLfloat blue[] = {0.1, 0.3, 0.9, 1.0}; // blue
GLfloat red[] = { 1.0, 0.2, 0.1, 0.2 }; // red GLfloat red[] = {1.0, 0.2, 0.1, 0.2}; // red
GLfloat green[] = { 0.3, 0.6, 0.3, 1.0 }; // green GLfloat green[] = {0.3, 0.6, 0.3, 1.0}; // green
GLfloat paleGrey[] = { 0.7, 0.7, 0.7, 1.0 }; // green GLfloat paleGrey[] = {0.7, 0.7, 0.7, 1.0}; // green
GLfloat darkGrey[] = { 0.2, 0.2, 0.2, 1.0 }; // green GLfloat darkGrey[] = {0.2, 0.2, 0.2, 1.0}; // green
} }
@ -18,53 +18,53 @@ namespace colours {
// if there is an active particle then it gets moved, // if there is an active particle then it gets moved,
// if not then add a particle // if not then add a particle
void DLASystem::Update() { void DLASystem::Update() {
if (lastParticleIsActive == 1) if (lastParticleIsActive == 1)
moveLastParticle(); moveLastParticle();
else if (numParticles < endNum) { else if (numParticles < endNum) {
addParticleOnAddCircle(); addParticleOnAddCircle();
setParticleActive(); setParticleActive();
} }
if (lastParticleIsActive == 0 || slowNotFast == 1) if (lastParticleIsActive == 0 || slowNotFast == 1)
glutPostRedisplay(); //Tell GLUT that the display has changed glutPostRedisplay(); //Tell GLUT that the display has changed
} }
void DLASystem::clearParticles() { void DLASystem::clearParticles() {
// delete particles and the particle list // delete particles and the particle list
for (int i = 0; i < numParticles; i++) { for (int i = 0; i < numParticles; i++) {
delete particleList[i]; delete particleList[i];
} }
particleList.clear(); particleList.clear();
numParticles = 0; numParticles = 0;
} }
// remove any existing particles and setup initial condition // remove any existing particles and setup initial condition
void DLASystem::Reset() { void DLASystem::Reset() {
// stop running // stop running
running = 0; running = 0;
clearParticles(); clearParticles();
lastParticleIsActive = 0; lastParticleIsActive = 0;
// set the grid to zero // set the grid to zero
for (int i = 0; i < gridSize; i++) { for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) { for (int j = 0; j < gridSize; j++) {
grid[i][j] = 0; grid[i][j] = 0;
} }
} }
// setup initial condition and parameters // setup initial condition and parameters
addCircle = 10; addCircle = 10;
killCircle = 2.0*addCircle; killCircle = 2.0 * addCircle;
clusterRadius = 0.0; clusterRadius = 0.0;
// add a single particle at the origin // add a single particle at the origin
double pos[] = { 0.0, 0.0 }; double pos[] = {0.0, 0.0};
addParticle(pos); addParticle(pos);
// set the view // set the view
int InitialViewSize = 40; int InitialViewSize = 40;
setViewSize(InitialViewSize); setViewSize(InitialViewSize);
} }
@ -72,248 +72,245 @@ void DLASystem::Reset() {
// note the position has the initial particle at (0,0) // note the position has the initial particle at (0,0)
// but this corresponds to the middle of the grid array ie grid[ halfGrid ][ halfGrid ] // but this corresponds to the middle of the grid array ie grid[ halfGrid ][ halfGrid ]
void DLASystem::setGrid(double pos[], int val) { void DLASystem::setGrid(double pos[], int val) {
int halfGrid = gridSize / 2; int halfGrid = gridSize / 2;
grid[(int)(pos[0] + halfGrid)][(int)(pos[1] + halfGrid)] = val; grid[(int) (pos[0] + halfGrid)][(int) (pos[1] + halfGrid)] = val;
} }
// read the grid cell for a given position // read the grid cell for a given position
int DLASystem::readGrid(double pos[]) { int DLASystem::readGrid(double pos[]) {
int halfGrid = gridSize / 2; int halfGrid = gridSize / 2;
return grid[(int)(pos[0] + halfGrid)][(int)(pos[1] + halfGrid)]; return grid[(int) (pos[0] + halfGrid)][(int) (pos[1] + halfGrid)];
} }
// check if the cluster is big enough and we should stop: // check if the cluster is big enough and we should stop:
// to be safe, we need the killCircle to be at least 2 less than the edge of the grid // to be safe, we need the killCircle to be at least 2 less than the edge of the grid
int DLASystem::checkStop() { int DLASystem::checkStop() {
if (killCircle + 2 >= gridSize / 2) { if (killCircle + 2 >= gridSize / 2) {
pauseRunning(); pauseRunning();
cout << "STOP" << endl; cout << "STOP" << endl;
glutPostRedisplay(); // update display glutPostRedisplay(); // update display
return 1; return 1;
} } else return 0;
else return 0;
} }
// add a particle to the system at a specific position // add a particle to the system at a specific position
void DLASystem::addParticle(double pos[]) { void DLASystem::addParticle(double pos[]) {
// create a new particle // create a new particle
Particle * p = new Particle(pos); Particle *p = new Particle(pos);
// push_back means "add this to the end of the list" // push_back means "add this to the end of the list"
particleList.push_back(p); particleList.push_back(p);
numParticles++; numParticles++;
// pos coordinates should be -gridSize/2 < x < gridSize/2 // pos coordinates should be -gridSize/2 < x < gridSize/2
setGrid(pos, 1); setGrid(pos, 1);
} }
// add a particle to the system at a random position on the addCircle // add a particle to the system at a random position on the addCircle
// if we hit an occupied site then we do nothing except print a message // if we hit an occupied site then we do nothing except print a message
// (this should never happen) // (this should never happen)
void DLASystem::addParticleOnAddCircle() { void DLASystem::addParticleOnAddCircle() {
double pos[2]; double pos[2];
double theta = rgen.random01() * 2 * M_PI; double theta = rgen.random01() * 2 * M_PI;
pos[0] = ceil(addCircle * cos(theta)); pos[0] = ceil(addCircle * cos(theta));
pos[1] = ceil(addCircle * sin(theta)); pos[1] = ceil(addCircle * sin(theta));
if (readGrid(pos) == 0) if (readGrid(pos) == 0)
addParticle(pos); addParticle(pos);
else else
cout << "FAIL " << pos[0] << " " << pos[1] << endl; cout << "FAIL " << pos[0] << " " << pos[1] << endl;
} }
// send back the position of a neighbour of a given grid cell // send back the position of a neighbour of a given grid cell
// NOTE: there is no check that the neighbour is inside the grid, // NOTE: there is no check that the neighbour is inside the grid,
// this has to be done separately... // this has to be done separately...
void DLASystem::setPosNeighbour(double setpos[], double pos[], int val) { void DLASystem::setPosNeighbour(double setpos[], double pos[], int val) {
switch (val) { switch (val) {
case 0: case 0:
setpos[0] = pos[0] + 1.0; setpos[0] = pos[0] + 1.0;
setpos[1] = pos[1]; setpos[1] = pos[1];
break; break;
case 1: case 1:
setpos[0] = pos[0] - 1.0; setpos[0] = pos[0] - 1.0;
setpos[1] = pos[1]; setpos[1] = pos[1];
break; break;
case 2: case 2:
setpos[0] = pos[0]; setpos[0] = pos[0];
setpos[1] = pos[1] + 1.0; setpos[1] = pos[1] + 1.0;
break; break;
case 3: case 3:
setpos[0] = pos[0]; setpos[0] = pos[0];
setpos[1] = pos[1] - 1.0; setpos[1] = pos[1] - 1.0;
break; break;
} }
} }
// if the view is smaller than the kill circle then increase the view area (zoom out) // if the view is smaller than the kill circle then increase the view area (zoom out)
void DLASystem::updateViewSize() { void DLASystem::updateViewSize() {
double mult = 1.2; double mult = 1.2;
if (viewSize < 2.0*killCircle) { if (viewSize < 2.0 * killCircle) {
setViewSize(viewSize * mult); setViewSize(viewSize * mult);
} }
} }
// set the view to be the size of the add circle (ie zoom in on the cluster) // set the view to be the size of the add circle (ie zoom in on the cluster)
void DLASystem::viewAddCircle() { void DLASystem::viewAddCircle() {
setViewSize(2.0*addCircle); // factor of 2 is to go from radius to diameter setViewSize(2.0 * addCircle); // factor of 2 is to go from radius to diameter
} }
// when we add a particle to the cluster, we should update the cluster radius // when we add a particle to the cluster, we should update the cluster radius
// and the sizes of the addCircle and the killCircle // and the sizes of the addCircle and the killCircle
void DLASystem::updateClusterRadius(double pos[]) { void DLASystem::updateClusterRadius(double pos[]) {
double rr = distanceFromOrigin(pos); double rr = distanceFromOrigin(pos);
if (rr > clusterRadius) { if (rr > clusterRadius) {
clusterRadius = rr; clusterRadius = rr;
// this is how big addCircle is supposed to be: // this is how big addCircle is supposed to be:
// either 20% more than cluster radius, or at least 5 bigger. // either 20% more than cluster radius, or at least 5 bigger.
double check = clusterRadius * addRatio; double check = clusterRadius * addRatio;
if (check < clusterRadius + 5) if (check < clusterRadius + 5)
check = clusterRadius + 5; check = clusterRadius + 5;
// if it is smaller then update everything... // if it is smaller then update everything...
if (addCircle < check) { if (addCircle < check) {
addCircle = check; addCircle = check;
killCircle = killRatio * addCircle; killCircle = killRatio * addCircle;
updateViewSize(); updateViewSize();
} }
checkStop(); checkStop();
} }
} }
// make a random move of the last particle in the particleList // make a random move of the last particle in the particleList
void DLASystem::moveLastParticle() { void DLASystem::moveLastParticle() {
int rr = rgen.randomInt(4); // pick a random number in the range 0-3, which direction do we hop? int rr = rgen.randomInt(4); // pick a random number in the range 0-3, which direction do we hop?
double newpos[2]; double newpos[2];
Particle *lastP = particleList[numParticles - 1]; Particle *lastP = particleList[numParticles - 1];
setPosNeighbour(newpos, lastP->pos, rr); setPosNeighbour(newpos, lastP->pos, rr);
if (distanceFromOrigin(newpos) > killCircle) { if (distanceFromOrigin(newpos) > killCircle) {
//cout << "#deleting particle" << endl; //cout << "#deleting particle" << endl;
setGrid(lastP->pos, 0); setGrid(lastP->pos, 0);
particleList.pop_back(); // remove particle from particleList particleList.pop_back(); // remove particle from particleList
numParticles--; numParticles--;
setParticleInactive(); setParticleInactive();
} }
// check if destination is empty // check if destination is empty
else if (readGrid(newpos) == 0) { else if (readGrid(newpos) == 0) {
setGrid(lastP->pos, 0); // set the old grid site to empty setGrid(lastP->pos, 0); // set the old grid site to empty
// update the position // update the position
particleList[numParticles - 1]->pos[0] = newpos[0]; particleList[numParticles - 1]->pos[0] = newpos[0];
particleList[numParticles - 1]->pos[1] = newpos[1]; particleList[numParticles - 1]->pos[1] = newpos[1];
setGrid(lastP->pos, 1); // set the new grid site to be occupied setGrid(lastP->pos, 1); // set the new grid site to be occupied
// check if we stick // check if we stick
if (checkStick()) { if (checkStick()) {
//cout << "stick" << endl; //cout << "stick" << endl;
setParticleInactive(); // make the particle inactive (stuck) setParticleInactive(); // make the particle inactive (stuck)
updateClusterRadius(lastP->pos); // update the cluster radius, addCircle, etc. updateClusterRadius(lastP->pos); // update the cluster radius, addCircle, etc.
if (numParticles % 100 == 0 && logfile.is_open()) { if (numParticles % 100 == 0 && logfile.is_open()) {
logfile << numParticles << " " << clusterRadius << endl; logfile << numParticles << " " << clusterRadius << endl;
} }
} }
} } else {
else { // if we get to here then we are trying to move to an occupied site
// if we get to here then we are trying to move to an occupied site // (this should never happen as long as the sticking probability is 1.0)
// (this should never happen as long as the sticking probability is 1.0) cout << "reject " << rr << endl;
cout << "reject " << rr << endl; cout << lastP->pos[0] << " " << lastP->pos[1] << endl;
cout << lastP->pos[0] << " " << lastP->pos[1] << endl; //cout << newpos[0] << " " << newpos[1] << " " << (int)newpos[0] << endl;
//cout << newpos[0] << " " << newpos[1] << " " << (int)newpos[0] << endl; //printOccupied();
//printOccupied(); }
}
} }
// check if the last particle should stick (to a neighbour) // check if the last particle should stick (to a neighbour)
int DLASystem::checkStick() { int DLASystem::checkStick() {
Particle *lastP = particleList[numParticles - 1]; Particle *lastP = particleList[numParticles - 1];
int result = 0; int result = 0;
// loop over neighbours // loop over neighbours
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
double checkpos[2]; double checkpos[2];
setPosNeighbour(checkpos, lastP->pos, i); setPosNeighbour(checkpos, lastP->pos, i);
// if the neighbour is occupied... // if the neighbour is occupied...
if (readGrid(checkpos) == 1) if (readGrid(checkpos) == 1)
result = 1; result = 1;
} }
return result; return result;
} }
// constructor // constructor
DLASystem::DLASystem(Window *set_win) { DLASystem::DLASystem(Window *set_win) {
cout << "creating system, gridSize " << gridSize << endl; cout << "creating system, gridSize " << gridSize << endl;
win = set_win; win = set_win;
numParticles = 0; numParticles = 0;
endNum = 1000; endNum = 1000;
// allocate memory for the grid, remember to free the memory in destructor // allocate memory for the grid, remember to free the memory in destructor
grid = new int*[gridSize]; grid = new int *[gridSize];
for (int i = 0; i < gridSize; i++) { for (int i = 0; i < gridSize; i++) {
grid[i] = new int[gridSize]; grid[i] = new int[gridSize];
} }
slowNotFast = 1; slowNotFast = 1;
// reset initial parameters // reset initial parameters
Reset(); Reset();
addRatio = 1.2; // how much bigger the addCircle should be, compared to cluster radius addRatio = 1.2; // how much bigger the addCircle should be, compared to cluster radius
killRatio = 1.7; // how much bigger is the killCircle, compared to the addCircle killRatio = 1.7; // how much bigger is the killCircle, compared to the addCircle
// this opens a logfile, if we want to... // this opens a logfile, if we want to...
//logfile.open("opfile.txt"); //logfile.open("opfile.txt");
} }
// destructor // destructor
DLASystem::~DLASystem() { DLASystem::~DLASystem() {
// strictly we should not print inside the destructor but never mind... // strictly we should not print inside the destructor but never mind...
cout << "deleting system" << endl; cout << "deleting system" << endl;
// delete the particles // delete the particles
clearParticles(); clearParticles();
// delete the grid // delete the grid
for (int i = 0; i < gridSize; i++) for (int i = 0; i < gridSize; i++)
delete[] grid[i]; delete[] grid[i];
delete[] grid; delete[] grid;
if (logfile.is_open()) if (logfile.is_open())
logfile.close(); logfile.close();
} }
// this draws the system // this draws the system
void DLASystem::DrawSquares() { void DLASystem::DrawSquares() {
// draw the particles // draw the particles
double halfSize = 0.5; double halfSize = 0.5;
for (int p = 0; p < numParticles; p++) { for (int p = 0; p < numParticles; p++) {
double *vec = particleList[p]->pos; double *vec = particleList[p]->pos;
glPushMatrix(); glPushMatrix();
if (p == numParticles - 1 && lastParticleIsActive == 1) if (p == numParticles - 1 && lastParticleIsActive == 1)
glColor4fv(colours::red); glColor4fv(colours::red);
else if (p == 0) else if (p == 0)
glColor4fv(colours::green); glColor4fv(colours::green);
else else
glColor4fv(colours::blue); glColor4fv(colours::blue);
glRectd(drawScale*(vec[0] - halfSize), glRectd(drawScale * (vec[0] - halfSize),
drawScale*(vec[1] - halfSize), drawScale * (vec[1] - halfSize),
drawScale*(vec[0] + halfSize), drawScale * (vec[0] + halfSize),
drawScale*(vec[1] + halfSize)); drawScale * (vec[1] + halfSize));
glPopMatrix(); glPopMatrix();
} }
// print some information (at top left) // print some information (at top left)
// this ostringstream is a way to create a string with numbers and words (similar to cout << ... ) // this ostringstream is a way to create a string with numbers and words (similar to cout << ... )
ostringstream str; ostringstream str;
str << "num " << numParticles << " size " << clusterRadius; str << "num " << numParticles << " size " << clusterRadius;
// print the string // print the string
win->displayString(str, -0.9, 0.9, colours::red); win->displayString(str, -0.9, 0.9, colours::red);
// if we are paused then print this (at bottom left) // if we are paused then print this (at bottom left)
if (running == 0) { if (running == 0) {
ostringstream pauseStr; ostringstream pauseStr;
pauseStr << "paused"; pauseStr << "paused";
win->displayString(pauseStr, -0.9, -0.9, colours::red); win->displayString(pauseStr, -0.9, -0.9, colours::red);
} }
} }

View File

@ -5,7 +5,9 @@
#include <fstream> #include <fstream>
#include <stdio.h> #include <stdio.h>
#include <vector> #include <vector>
#define _USE_MATH_DEFINES #define _USE_MATH_DEFINES
#include <math.h> #include <math.h>
#include <random> #include <random>
#include <string> #include <string>
@ -19,13 +21,13 @@ using namespace std;
class DLASystem { class DLASystem {
private: private:
// these are private variables and functions that the user will not see // these are private variables and functions that the user will not see
Window *win; // window in which the system is running Window *win; // window in which the system is running
// list of particles // list of particles
vector<Particle*> particleList; vector<Particle *> particleList;
int numParticles; int numParticles;
// delete particles and clear the particle list // delete particles and clear the particle list
@ -36,32 +38,32 @@ class DLASystem {
// these are related to the DLA algorithm // these are related to the DLA algorithm
double addCircle; double addCircle;
double killCircle; double killCircle;
// size of grid // size of grid
static const int gridSize = 1600; static const int gridSize = 1600;
int **grid; // this will be a 2d array that stores whether each site is occupied int **grid; // this will be a 2d array that stores whether each site is occupied
// the window draws only part of the grid, viewSize controls how much... // the window draws only part of the grid, viewSize controls how much...
double viewSize; double viewSize;
double drawScale; double drawScale;
// random number generator, class name is rnd, instance is rgen // random number generator, class name is rnd, instance is rgen
rnd rgen; rnd rgen;
// output file (not used at the moment) // output file (not used at the moment)
ofstream logfile; ofstream logfile;
// number of particles at which the simulation will stop // number of particles at which the simulation will stop
// (the value is set in constructor) // (the value is set in constructor)
int endNum; int endNum;
// the values of these variables are set in the constructor // the values of these variables are set in the constructor
double addRatio; // how much bigger the addCircle should be, compared to cluster radius double addRatio; // how much bigger the addCircle should be, compared to cluster radius
double killRatio; // how much bigger is the killCircle, compared to the addCircle double killRatio; // how much bigger is the killCircle, compared to the addCircle
public: public:
// these are public variables and functions // these are public variables and functions
// update the system: if there is an active particle then move it, // update the system: if there is an active particle then move it,
// else create a new particle (on the adding circle) // else create a new particle (on the adding circle)
@ -69,21 +71,22 @@ class DLASystem {
// draw particles as squares // draw particles as squares
void DrawSquares(); void DrawSquares();
// is the simulation running (1) or paused (0) ? // is the simulation running (1) or paused (0) ?
int running; int running;
// slowNotFast is +1 for slow running, 0 for fast // slowNotFast is +1 for slow running, 0 for fast
int slowNotFast; int slowNotFast;
// lastParticleIsActive is +1 if there is an active particle in the system, otherwise 0 // lastParticleIsActive is +1 if there is an active particle in the system, otherwise 0
int lastParticleIsActive; int lastParticleIsActive;
// constructor // constructor
DLASystem(Window *set_win); DLASystem(Window *set_win);
// destructor // destructor
~DLASystem(); ~DLASystem();
// delete all particles and reset // delete all particles and reset
void Reset(); void Reset();
@ -92,20 +95,27 @@ class DLASystem {
// check whether we should stop (eg the cluster has reached the edge of the grid) // check whether we should stop (eg the cluster has reached the edge of the grid)
int checkStop(); int checkStop();
// stop/start the algorithm // stop/start the algorithm
void setRunning() { if ( checkStop()==0 ) running = 1; } void setRunning() { if (checkStop() == 0) running = 1; }
void pauseRunning() { running = 0; } void pauseRunning() { running = 0; }
// set whether it runs fast or slow // set whether it runs fast or slow
void setSlow() { slowNotFast = 1; } void setSlow() { slowNotFast = 1; }
void setFast() { slowNotFast = 0; } void setFast() { slowNotFast = 0; }
void setSuperFast() { slowNotFast = -1; } void setSuperFast() { slowNotFast = -1; }
// set which part of the grid is visible on the screen // set which part of the grid is visible on the screen
// basically the screen shows co-ordinates -vv < x < vv // basically the screen shows co-ordinates -vv < x < vv
// where vv is the input value // where vv is the input value
void setViewSize(double vv) { viewSize = vv; drawScale = 2.0/viewSize; } void setViewSize(double vv) {
viewSize = vv;
drawScale = 2.0 / viewSize;
}
// if the killcircle is almost as big as the view then increase the view // if the killcircle is almost as big as the view then increase the view
void updateViewSize(); void updateViewSize();
@ -113,23 +123,26 @@ class DLASystem {
void viewAddCircle(); void viewAddCircle();
// if pos is outside the cluster radius then set clusterRadius to be the distance to pos. // if pos is outside the cluster radius then set clusterRadius to be the distance to pos.
void updateClusterRadius( double pos[] ); void updateClusterRadius(double pos[]);
// set and read grid entries associated with a given position // set and read grid entries associated with a given position
void setGrid(double pos[], int val); void setGrid(double pos[], int val);
int readGrid(double pos[]); int readGrid(double pos[]);
// return the distance of a given point from the origin // return the distance of a given point from the origin
double distanceFromOrigin(double pos[]) { double distanceFromOrigin(double pos[]) {
return sqrt( pos[0]*pos[0] + pos[1]*pos[1] ); return sqrt(pos[0] * pos[0] + pos[1] * pos[1]);
} }
// set whether there is an active particle in the system or not // set whether there is an active particle in the system or not
void setParticleActive() { lastParticleIsActive = 1; } void setParticleActive() { lastParticleIsActive = 1; }
void setParticleInactive() { lastParticleIsActive = 0; } void setParticleInactive() { lastParticleIsActive = 0; }
// add a particle at pos // add a particle at pos
void addParticle(double pos[]); void addParticle(double pos[]);
// add a particle at a random point on the addCircle // add a particle at a random point on the addCircle
void addParticleOnAddCircle(); void addParticleOnAddCircle();
@ -150,5 +163,6 @@ class DLASystem {
// it would be better for an OOP philosophy to make these member functions for the Window class // it would be better for an OOP philosophy to make these member functions for the Window class
// but we are being a bit lazy here // but we are being a bit lazy here
void setWinBackgroundWhite() { glClearColor(1.0, 1.0, 1.0, 1.0); } void setWinBackgroundWhite() { glClearColor(1.0, 1.0, 1.0, 1.0); }
void setWinBackgroundBlack() { glClearColor(0.0, 0.0, 0.0, 0.0); } void setWinBackgroundBlack() { glClearColor(0.0, 0.0, 0.0, 0.0); }
}; };

View File

@ -1,20 +1,22 @@
#pragma once #pragma once
class Particle { class Particle {
public: public:
static const int dim = 2; // we are in two dimensions static const int dim = 2; // we are in two dimensions
double *pos; // pointer to an array of size dim, to store the position double *pos; // pointer to an array of size dim, to store the position
// default constructor // default constructor
Particle() { Particle() {
pos = new double[dim]; pos = new double[dim];
} }
// constructor, with a specified initial position // constructor, with a specified initial position
Particle(double set_pos[]) { Particle(double set_pos[]) {
pos = new double[dim]; pos = new double[dim];
for (int d=0;d<dim;d++) for (int d = 0; d < dim; d++)
pos[d]=set_pos[d]; pos[d] = set_pos[d];
} }
// destructor // destructor
~Particle() { delete[] pos; } ~Particle() { delete[] pos; }
}; };

View File

@ -1,26 +1,27 @@
#include "Window.h" #include "Window.h"
// constructor // constructor
Window::Window(int set_size[], string &set_title) { Window::Window(int set_size[], string &set_title) {
size[0]=set_size[0]; size[1]=set_size[1]; size[0] = set_size[0];
title=set_title; size[1] = set_size[1];
title = set_title;
locateOnScreen(); locateOnScreen();
glutInitWindowSize(size[0],size[1]); glutInitWindowSize(size[0], size[1]);
glutInitWindowPosition(pos[0],pos[1]); glutInitWindowPosition(pos[0], pos[1]);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow(title.c_str()); glutCreateWindow(title.c_str());
// sets the background to black // sets the background to black
glClearColor(0.0, 0.0, 0.0, 0.0); glClearColor(0.0, 0.0, 0.0, 0.0);
} }
// print a string at a given position, don't worry about how this works... // print a string at a given position, don't worry about how this works...
void Window::displayString( ostringstream &str, double x, double y, GLfloat col[]) { void Window::displayString(ostringstream &str, double x, double y, GLfloat col[]) {
string localString = str.str(); string localString = str.str();
glColor4fv(col); glColor4fv(col);
glRasterPos2d(x,y); glRasterPos2d(x, y);
for (int i = 0; i < localString.length(); i++) { for (int i = 0; i < localString.length(); i++) {
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, localString[i]); glutBitmapCharacter(GLUT_BITMAP_8_BY_13, localString[i]);
} }
} }

View File

@ -8,25 +8,25 @@
using namespace std; using namespace std;
class Window { class Window {
public: public:
string title; string title;
int size[2]; int size[2];
int pos[2]; int pos[2];
void locateOnScreen() { void locateOnScreen() {
// the fx sets where on the screen the window will appear // the fx sets where on the screen the window will appear
// (values should be between 0 and 1) // (values should be between 0 and 1)
double fx[] = { 0.7,0.5 }; double fx[] = {0.7, 0.5};
pos[0] = (glutGet(GLUT_SCREEN_WIDTH) - size[0]) * fx[0]; pos[0] = (glutGet(GLUT_SCREEN_WIDTH) - size[0]) * fx[0];
pos[1] = (glutGet(GLUT_SCREEN_HEIGHT) - size[1]) * fx[1]; pos[1] = (glutGet(GLUT_SCREEN_HEIGHT) - size[1]) * fx[1];
} }
// constructor, size is in pixels // constructor, size is in pixels
Window(int set_size[], string &set_title); Window(int set_size[], string &set_title);
// function which prints a string to the screen, at a given position, with a given color // function which prints a string to the screen, at a given position, with a given color
// note position is "absolute", not easy to get two strings spaced one above each other like this // note position is "absolute", not easy to get two strings spaced one above each other like this
void displayString( ostringstream &str, double x, double y, GLfloat col[]); void displayString(ostringstream &str, double x, double y, GLfloat col[]);
}; };

View File

@ -6,164 +6,167 @@
#include <string> #include <string>
#include "DLASystem.h" #include "DLASystem.h"
#include "Window.h" #include "Window.h"
using namespace std; using namespace std;
// functions which are needed for openGL go into a namespace so that we can identify them // functions which are needed for openGL go into a namespace so that we can identify them
namespace drawFuncs { namespace drawFuncs {
void handleKeypress(unsigned char key, int x, int y); void handleKeypress(unsigned char key, int x, int y);
void display(void);
void update(int val); void display(void);
void introMessage();
void update(int val);
void introMessage();
} }
// this is a global pointer, which is how we access the system itself // this is a global pointer, which is how we access the system itself
DLASystem *sys; DLASystem *sys;
int main(int argc, char **argv) { int main(int argc, char **argv) {
// turn on glut // turn on glut
glutInit(&argc, argv); glutInit(&argc, argv);
int window_size[] = { 480,480 }; int window_size[] = {480, 480};
string window_title("simple DLA simulation"); string window_title("simple DLA simulation");
// create a window // create a window
Window *win = new Window(window_size,window_title); Window *win = new Window(window_size, window_title);
// create the system // create the system
sys = new DLASystem(win); sys = new DLASystem(win);
// this is the seed for the random numbers
int seed = 6;
cout << "setting seed " << seed << endl;
sys->setSeed(seed);
// print the "help" message to the console
drawFuncs::introMessage();
// tell openGL how to redraw the screen and respond to the keyboard
glutDisplayFunc( drawFuncs::display );
glutKeyboardFunc( drawFuncs::handleKeypress );
// tell openGL to do its first update after waiting 10ms
int wait = 10;
int val = 0;
glutTimerFunc(wait, drawFuncs::update, val);
// start the openGL stuff // this is the seed for the random numbers
glutMainLoop(); int seed = 6;
cout << "setting seed " << seed << endl;
sys->setSeed(seed);
return 0; // print the "help" message to the console
drawFuncs::introMessage();
// tell openGL how to redraw the screen and respond to the keyboard
glutDisplayFunc(drawFuncs::display);
glutKeyboardFunc(drawFuncs::handleKeypress);
// tell openGL to do its first update after waiting 10ms
int wait = 10;
int val = 0;
glutTimerFunc(wait, drawFuncs::update, val);
// start the openGL stuff
glutMainLoop();
return 0;
} }
// this is just a help message // this is just a help message
void drawFuncs::introMessage() { void drawFuncs::introMessage() {
cout << "Keys (while in graphics window):" << endl << " q or e to quit (or exit)" << endl; cout << "Keys (while in graphics window):" << endl << " q or e to quit (or exit)" << endl;
cout << " h to print this message (help)" << endl; cout << " h to print this message (help)" << endl;
cout << " u for a single update" << endl; cout << " u for a single update" << endl;
cout << " g to start running (go)" << endl; cout << " g to start running (go)" << endl;
cout << " p to pause running" << endl; cout << " p to pause running" << endl;
cout << " s to run in slow-mode" << endl; cout << " s to run in slow-mode" << endl;
cout << " f to run in fast-mode" << endl; cout << " f to run in fast-mode" << endl;
cout << " r to clear everything (reset)" << endl; cout << " r to clear everything (reset)" << endl;
cout << " z to pause and zoom in" << endl; cout << " z to pause and zoom in" << endl;
cout << " w or b to change background colour to white or black" << endl; cout << " w or b to change background colour to white or black" << endl;
} }
// openGL function deals with the keyboard // openGL function deals with the keyboard
void drawFuncs::handleKeypress(unsigned char key, int x, int y) { void drawFuncs::handleKeypress(unsigned char key, int x, int y) {
switch (key) { switch (key) {
case 'h': case 'h':
drawFuncs::introMessage(); drawFuncs::introMessage();
break; break;
case 'q': case 'q':
case 'e': case 'e':
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
// delete the system // delete the system
delete sys; delete sys;
exit(0); exit(0);
break; break;
case 'p': case 'p':
cout << "pause" << endl; cout << "pause" << endl;
sys->pauseRunning(); sys->pauseRunning();
break; break;
case 'g': case 'g':
cout << "go" << endl; cout << "go" << endl;
sys->setRunning(); sys->setRunning();
glutTimerFunc(0, drawFuncs::update, 0); glutTimerFunc(0, drawFuncs::update, 0);
break; break;
case 's': case 's':
cout << "slow" << endl; cout << "slow" << endl;
sys->setSlow(); sys->setSlow();
break; break;
case 'w': case 'w':
cout << "white" << endl; cout << "white" << endl;
sys->setWinBackgroundWhite(); sys->setWinBackgroundWhite();
break; break;
case 'b': case 'b':
cout << "black" << endl; cout << "black" << endl;
sys->setWinBackgroundBlack(); sys->setWinBackgroundBlack();
break; break;
case 'f': case 'f':
cout << "fast" << endl; cout << "fast" << endl;
sys->setFast(); sys->setFast();
break; break;
case 'r': case 'r':
cout << "reset" << endl; cout << "reset" << endl;
sys->Reset(); sys->Reset();
break; break;
case 'z': case 'z':
cout << "zoom" << endl; cout << "zoom" << endl;
sys->pauseRunning(); sys->pauseRunning();
sys->viewAddCircle(); sys->viewAddCircle();
break; break;
case 'u': case 'u':
cout << "upd" << endl; cout << "upd" << endl;
sys->Update(); sys->Update();
break; break;
} }
// tell openGL to redraw the window // tell openGL to redraw the window
glutPostRedisplay(); glutPostRedisplay();
} }
// this function gets called whenever the algorithm should do its update // this function gets called whenever the algorithm should do its update
void drawFuncs::update(int val) { void drawFuncs::update(int val) {
int wait; // time to wait between updates (milliseconds) int wait; // time to wait between updates (milliseconds)
if ( sys->running ) { if (sys->running) {
if ( sys->slowNotFast == 1) if (sys->slowNotFast == 1)
wait = 10; wait = 10;
else else
wait = 0; wait = 0;
sys->Update(); sys->Update();
// tell openGL to call this funtion again after "wait" milliseconds // tell openGL to call this funtion again after "wait" milliseconds
glutTimerFunc(wait, drawFuncs::update, 0); glutTimerFunc(wait, drawFuncs::update, 0);
} }
} }
// this function redraws the window when necessary // this function redraws the window when necessary
void drawFuncs::display() { void drawFuncs::display() {
// Clear the window or more specifically the frame buffer... // Clear the window or more specifically the frame buffer...
// This happens by replacing all the contents of the frame // This happens by replacing all the contents of the frame
// buffer by the clear color (black in our case) // buffer by the clear color (black in our case)
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
// this puts the camera at the origin (not sure why) with (I think) z axis out of page and y axis up // this puts the camera at the origin (not sure why) with (I think) z axis out of page and y axis up
// there is also the question of the GL perspective which is not set up in any clear way at the moment // there is also the question of the GL perspective which is not set up in any clear way at the moment
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); glLoadIdentity();
gluLookAt(0.0, 0.0, 1.0, /* camera position */ gluLookAt(0.0, 0.0, 1.0, /* camera position */
0.0, 0.0, -1.0, /* point to look at */ 0.0, 0.0, -1.0, /* point to look at */
0.0, 1.0, 0.0); /* up direction */ 0.0, 1.0, 0.0); /* up direction */
//sys->DrawSpheres(); //sys->DrawSpheres();
sys->DrawSquares(); sys->DrawSquares();
// Swap contents of backward and forward frame buffers // Swap contents of backward and forward frame buffers
glutSwapBuffers(); glutSwapBuffers();
} }

45
rnd.h
View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include <random> #include <random>
// ... don't worry how this all works // ... don't worry how this all works
@ -8,26 +9,32 @@
class rnd { class rnd {
private: private:
// nuts and bolts.. should not need to touch this. // nuts and bolts.. should not need to touch this.
std::default_random_engine generator; std::default_random_engine generator;
int genMax; int genMax;
std::uniform_int_distribution<int> *intmax; std::uniform_int_distribution<int> *intmax;
std::uniform_real_distribution<double> *real01; std::uniform_real_distribution<double> *real01;
public: public:
// constructor // constructor
rnd() { rnd() {
genMax = 0x7fffffff; genMax = 0x7fffffff;
//cout << "genMax is " << generator.max() << endl; //cout << "genMax is " << generator.max() << endl;
intmax = new std::uniform_int_distribution<int>(0, genMax); intmax = new std::uniform_int_distribution<int>(0, genMax);
real01 = new std::uniform_real_distribution<double>(0.0, 1.0); real01 = new std::uniform_real_distribution<double>(0.0, 1.0);
} }
// destructor
~rnd() { delete intmax; delete real01; }
// set the random seed // destructor
void setSeed(int seed) { generator.seed(seed); } ~rnd() {
// member functions for generating random double in [0,1] and random integer in [0,max-1] delete intmax;
double random01() { return (*real01)(generator); } delete real01;
int randomInt(int max) { return (*intmax)(generator) % max; } }
// set the random seed
void setSeed(int seed) { generator.seed(seed); }
// member functions for generating random double in [0,1] and random integer in [0,max-1]
double random01() { return (*real01)(generator); }
int randomInt(int max) { return (*intmax)(generator) % max; }
}; };