Format
This commit is contained in:
parent
f79763eee4
commit
496c1022db
@ -6,11 +6,11 @@
|
||||
|
||||
// colors
|
||||
namespace colours {
|
||||
GLfloat blue[] = { 0.1, 0.3, 0.9, 1.0 }; // blue
|
||||
GLfloat red[] = { 1.0, 0.2, 0.1, 0.2 }; // red
|
||||
GLfloat green[] = { 0.3, 0.6, 0.3, 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 blue[] = {0.1, 0.3, 0.9, 1.0}; // blue
|
||||
GLfloat red[] = {1.0, 0.2, 0.1, 0.2}; // red
|
||||
GLfloat green[] = {0.3, 0.6, 0.3, 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
|
||||
}
|
||||
|
||||
|
||||
@ -56,10 +56,10 @@ void DLASystem::Reset() {
|
||||
|
||||
// setup initial condition and parameters
|
||||
addCircle = 10;
|
||||
killCircle = 2.0*addCircle;
|
||||
killCircle = 2.0 * addCircle;
|
||||
clusterRadius = 0.0;
|
||||
// add a single particle at the origin
|
||||
double pos[] = { 0.0, 0.0 };
|
||||
double pos[] = {0.0, 0.0};
|
||||
addParticle(pos);
|
||||
|
||||
// set the view
|
||||
@ -73,13 +73,13 @@ void DLASystem::Reset() {
|
||||
// but this corresponds to the middle of the grid array ie grid[ halfGrid ][ halfGrid ]
|
||||
void DLASystem::setGrid(double pos[], int val) {
|
||||
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
|
||||
int DLASystem::readGrid(double pos[]) {
|
||||
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:
|
||||
@ -90,14 +90,13 @@ int DLASystem::checkStop() {
|
||||
cout << "STOP" << endl;
|
||||
glutPostRedisplay(); // update display
|
||||
return 1;
|
||||
}
|
||||
else return 0;
|
||||
} else return 0;
|
||||
}
|
||||
|
||||
// add a particle to the system at a specific position
|
||||
void DLASystem::addParticle(double pos[]) {
|
||||
// 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"
|
||||
particleList.push_back(p);
|
||||
numParticles++;
|
||||
@ -147,14 +146,14 @@ void DLASystem::setPosNeighbour(double setpos[], double pos[], int val) {
|
||||
// if the view is smaller than the kill circle then increase the view area (zoom out)
|
||||
void DLASystem::updateViewSize() {
|
||||
double mult = 1.2;
|
||||
if (viewSize < 2.0*killCircle) {
|
||||
if (viewSize < 2.0 * killCircle) {
|
||||
setViewSize(viewSize * mult);
|
||||
}
|
||||
}
|
||||
|
||||
// set the view to be the size of the add circle (ie zoom in on the cluster)
|
||||
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
|
||||
@ -213,8 +212,7 @@ void DLASystem::moveLastParticle() {
|
||||
logfile << numParticles << " " << clusterRadius << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// 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)
|
||||
cout << "reject " << rr << endl;
|
||||
@ -248,7 +246,7 @@ DLASystem::DLASystem(Window *set_win) {
|
||||
endNum = 1000;
|
||||
|
||||
// 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++) {
|
||||
grid[i] = new int[gridSize];
|
||||
}
|
||||
@ -279,7 +277,6 @@ DLASystem::~DLASystem() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
// this draws the system
|
||||
void DLASystem::DrawSquares() {
|
||||
|
||||
@ -294,10 +291,10 @@ void DLASystem::DrawSquares() {
|
||||
glColor4fv(colours::green);
|
||||
else
|
||||
glColor4fv(colours::blue);
|
||||
glRectd(drawScale*(vec[0] - halfSize),
|
||||
drawScale*(vec[1] - halfSize),
|
||||
drawScale*(vec[0] + halfSize),
|
||||
drawScale*(vec[1] + halfSize));
|
||||
glRectd(drawScale * (vec[0] - halfSize),
|
||||
drawScale * (vec[1] - halfSize),
|
||||
drawScale * (vec[0] + halfSize),
|
||||
drawScale * (vec[1] + halfSize));
|
||||
glPopMatrix();
|
||||
}
|
||||
|
||||
|
||||
28
DLASystem.h
28
DLASystem.h
@ -5,7 +5,9 @@
|
||||
#include <fstream>
|
||||
#include <stdio.h>
|
||||
#include <vector>
|
||||
|
||||
#define _USE_MATH_DEFINES
|
||||
|
||||
#include <math.h>
|
||||
#include <random>
|
||||
#include <string>
|
||||
@ -19,13 +21,13 @@ using namespace std;
|
||||
|
||||
|
||||
class DLASystem {
|
||||
private:
|
||||
private:
|
||||
// these are private variables and functions that the user will not see
|
||||
|
||||
Window *win; // window in which the system is running
|
||||
|
||||
// list of particles
|
||||
vector<Particle*> particleList;
|
||||
vector<Particle *> particleList;
|
||||
int numParticles;
|
||||
|
||||
// delete particles and clear the particle list
|
||||
@ -60,7 +62,7 @@ class DLASystem {
|
||||
double killRatio; // how much bigger is the killCircle, compared to the addCircle
|
||||
|
||||
|
||||
public:
|
||||
public:
|
||||
// these are public variables and functions
|
||||
|
||||
// update the system: if there is an active particle then move it,
|
||||
@ -81,6 +83,7 @@ class DLASystem {
|
||||
|
||||
// constructor
|
||||
DLASystem(Window *set_win);
|
||||
|
||||
// destructor
|
||||
~DLASystem();
|
||||
|
||||
@ -94,17 +97,24 @@ class DLASystem {
|
||||
int checkStop();
|
||||
|
||||
// stop/start the algorithm
|
||||
void setRunning() { if ( checkStop()==0 ) running = 1; }
|
||||
void setRunning() { if (checkStop() == 0) running = 1; }
|
||||
|
||||
void pauseRunning() { running = 0; }
|
||||
|
||||
// set whether it runs fast or slow
|
||||
void setSlow() { slowNotFast = 1; }
|
||||
|
||||
void setFast() { slowNotFast = 0; }
|
||||
|
||||
void setSuperFast() { slowNotFast = -1; }
|
||||
|
||||
// set which part of the grid is visible on the screen
|
||||
// basically the screen shows co-ordinates -vv < x < vv
|
||||
// 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
|
||||
void updateViewSize();
|
||||
@ -113,23 +123,26 @@ class DLASystem {
|
||||
void viewAddCircle();
|
||||
|
||||
// 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
|
||||
void setGrid(double pos[], int val);
|
||||
|
||||
int readGrid(double pos[]);
|
||||
|
||||
// return the distance of a given point from the origin
|
||||
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
|
||||
void setParticleActive() { lastParticleIsActive = 1; }
|
||||
|
||||
void setParticleInactive() { lastParticleIsActive = 0; }
|
||||
|
||||
// add a particle at pos
|
||||
void addParticle(double pos[]);
|
||||
|
||||
// add a particle at a random point on the addCircle
|
||||
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
|
||||
// but we are being a bit lazy here
|
||||
void setWinBackgroundWhite() { glClearColor(1.0, 1.0, 1.0, 1.0); }
|
||||
|
||||
void setWinBackgroundBlack() { glClearColor(0.0, 0.0, 0.0, 0.0); }
|
||||
};
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
class Particle {
|
||||
public:
|
||||
public:
|
||||
static const int dim = 2; // we are in two dimensions
|
||||
double *pos; // pointer to an array of size dim, to store the position
|
||||
|
||||
@ -9,12 +9,14 @@ class Particle {
|
||||
Particle() {
|
||||
pos = new double[dim];
|
||||
}
|
||||
|
||||
// constructor, with a specified initial position
|
||||
Particle(double set_pos[]) {
|
||||
pos = new double[dim];
|
||||
for (int d=0;d<dim;d++)
|
||||
pos[d]=set_pos[d];
|
||||
for (int d = 0; d < dim; d++)
|
||||
pos[d] = set_pos[d];
|
||||
}
|
||||
|
||||
// destructor
|
||||
~Particle() { delete[] pos; }
|
||||
};
|
||||
|
||||
13
Window.cpp
13
Window.cpp
@ -2,12 +2,13 @@
|
||||
|
||||
// constructor
|
||||
Window::Window(int set_size[], string &set_title) {
|
||||
size[0]=set_size[0]; size[1]=set_size[1];
|
||||
title=set_title;
|
||||
size[0] = set_size[0];
|
||||
size[1] = set_size[1];
|
||||
title = set_title;
|
||||
|
||||
locateOnScreen();
|
||||
glutInitWindowSize(size[0],size[1]);
|
||||
glutInitWindowPosition(pos[0],pos[1]);
|
||||
glutInitWindowSize(size[0], size[1]);
|
||||
glutInitWindowPosition(pos[0], pos[1]);
|
||||
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
|
||||
glutCreateWindow(title.c_str());
|
||||
|
||||
@ -16,10 +17,10 @@ Window::Window(int set_size[], string &set_title) {
|
||||
}
|
||||
|
||||
// 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();
|
||||
glColor4fv(col);
|
||||
glRasterPos2d(x,y);
|
||||
glRasterPos2d(x, y);
|
||||
for (int i = 0; i < localString.length(); i++) {
|
||||
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, localString[i]);
|
||||
}
|
||||
|
||||
6
Window.h
6
Window.h
@ -8,7 +8,7 @@
|
||||
using namespace std;
|
||||
|
||||
class Window {
|
||||
public:
|
||||
public:
|
||||
string title;
|
||||
int size[2];
|
||||
int pos[2];
|
||||
@ -16,7 +16,7 @@ class Window {
|
||||
void locateOnScreen() {
|
||||
// the fx sets where on the screen the window will appear
|
||||
// (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[1] = (glutGet(GLUT_SCREEN_HEIGHT) - size[1]) * fx[1];
|
||||
}
|
||||
@ -26,7 +26,7 @@ class Window {
|
||||
|
||||
// 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
|
||||
void displayString( ostringstream &str, double x, double y, GLfloat col[]);
|
||||
void displayString(ostringstream &str, double x, double y, GLfloat col[]);
|
||||
|
||||
|
||||
};
|
||||
|
||||
15
mainDLA.cpp
15
mainDLA.cpp
@ -13,8 +13,11 @@ using namespace std;
|
||||
// functions which are needed for openGL go into a namespace so that we can identify them
|
||||
namespace drawFuncs {
|
||||
void handleKeypress(unsigned char key, int x, int y);
|
||||
|
||||
void display(void);
|
||||
|
||||
void update(int val);
|
||||
|
||||
void introMessage();
|
||||
}
|
||||
|
||||
@ -25,11 +28,11 @@ int main(int argc, char **argv) {
|
||||
// turn on glut
|
||||
glutInit(&argc, argv);
|
||||
|
||||
int window_size[] = { 480,480 };
|
||||
int window_size[] = {480, 480};
|
||||
string window_title("simple DLA simulation");
|
||||
|
||||
// create a window
|
||||
Window *win = new Window(window_size,window_title);
|
||||
Window *win = new Window(window_size, window_title);
|
||||
|
||||
// create the system
|
||||
sys = new DLASystem(win);
|
||||
@ -43,8 +46,8 @@ int main(int argc, char **argv) {
|
||||
drawFuncs::introMessage();
|
||||
|
||||
// tell openGL how to redraw the screen and respond to the keyboard
|
||||
glutDisplayFunc( drawFuncs::display );
|
||||
glutKeyboardFunc( drawFuncs::handleKeypress );
|
||||
glutDisplayFunc(drawFuncs::display);
|
||||
glutKeyboardFunc(drawFuncs::handleKeypress);
|
||||
|
||||
// tell openGL to do its first update after waiting 10ms
|
||||
int wait = 10;
|
||||
@ -131,8 +134,8 @@ void drawFuncs::handleKeypress(unsigned char key, int x, int y) {
|
||||
void drawFuncs::update(int val) {
|
||||
int wait; // time to wait between updates (milliseconds)
|
||||
|
||||
if ( sys->running ) {
|
||||
if ( sys->slowNotFast == 1)
|
||||
if (sys->running) {
|
||||
if (sys->slowNotFast == 1)
|
||||
wait = 10;
|
||||
else
|
||||
wait = 0;
|
||||
|
||||
9
rnd.h
9
rnd.h
@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <random>
|
||||
|
||||
// ... don't worry how this all works
|
||||
@ -22,12 +23,18 @@ public:
|
||||
intmax = new std::uniform_int_distribution<int>(0, genMax);
|
||||
real01 = new std::uniform_real_distribution<double>(0.0, 1.0);
|
||||
}
|
||||
|
||||
// destructor
|
||||
~rnd() { delete intmax; delete real01; }
|
||||
~rnd() {
|
||||
delete intmax;
|
||||
delete real01;
|
||||
}
|
||||
|
||||
// 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; }
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user