God I hate CPP

This commit is contained in:
Joshua Coles 2023-02-20 20:46:41 +00:00
parent 6afaf0c82d
commit 7d37960439
3 changed files with 22 additions and 32 deletions

View File

@ -34,17 +34,17 @@ void DLASystem::clearParticles() {
// set the value of a grid cell for a particular position
// note the position has the initial particle at (0,0)
// but this corresponds to the middle of the grid array ie grid[ halfGrid ][ halfGrid ]
void DLASystem::setGrid(double pos[], int val) {
void DLASystem::setGrid(std::array<double, 2> pos, int val) {
*index_grid(pos) = val;
}
// read the grid cell for a given position
int DLASystem::readGrid(double pos[]) {
int DLASystem::readGrid(std::array<double, 2> pos) {
return *index_grid(pos);
}
// add a particle to the system at a specific position
void DLASystem::addParticle(double pos[]) {
void DLASystem::addParticle(std::array<double, 2> pos) {
// create a new particle
Particle *p = new Particle(pos);
// push_back means "add this to the end of the list"
@ -58,10 +58,8 @@ void DLASystem::addParticle(double pos[]) {
// if we hit an occupied site then we do nothing except print a message
// (this should never happen)
void DLASystem::addParticleOnAddCircle() {
double pos[2];
double theta = rgen.random01() * 2 * M_PI;
pos[0] = ceil(addCircle * cos(theta));
pos[1] = ceil(addCircle * sin(theta));
std::array<double, 2> pos{ceil(addCircle * cos(theta)), ceil(addCircle * sin(theta))};
if (readGrid(pos) == 0) {
addParticle(pos);
} else {
@ -72,7 +70,7 @@ void DLASystem::addParticleOnAddCircle() {
// send back the position of a neighbour of a given grid cell
// NOTE: there is no check that the neighbour is inside the grid,
// this has to be done separately...
void DLASystem::setPosNeighbour(double setpos[], double pos[], int val) {
void DLASystem::setPosNeighbour(std::array<double, 2> &setpos, const std::array<double, 2> pos, int val) {
switch (val) {
case 0:
setpos[0] = pos[0] + 1.0;
@ -95,7 +93,7 @@ void DLASystem::setPosNeighbour(double setpos[], double pos[], int val) {
// when we add a particle to the cluster, we should update the cluster radius
// and the sizes of the addCircle and the killCircle
void DLASystem::updateClusterRadius(double pos[]) {
void DLASystem::updateClusterRadius(std::array<double, 2> pos) {
double newRadius = distanceFromOrigin(pos);
if (newRadius > clusterRadius) {
clusterRadius = newRadius;
@ -122,7 +120,7 @@ void DLASystem::updateClusterRadius(double pos[]) {
// make a random move of the last particle in the particleList
void DLASystem::moveLastParticle() {
int direction = rgen.randomInt(4); // pick a random number in the range 0-3, which direction do we hop?
double newpos[2];
std::array<double, 2> newpos{};
Particle *lastP = particleList[this->particleList.size() - 1];
@ -154,7 +152,7 @@ int DLASystem::checkStick() {
int result = 0;
// loop over neighbours
for (int i = 0; i < 4; i++) {
double checkpos[2];
std::array<double, 2> checkpos;
setPosNeighbour(checkpos, lastP->pos, i);
// if the neighbour is occupied...
if (readGrid(checkpos) == 1)
@ -188,7 +186,7 @@ DLASystem::DLASystem(Config config)
// }
// Add particle to the centre of the grid to start
double pos[] = {0.0, 0.0};
std::array<double, 2> pos{0.0, 0.0};
addParticle(pos);
// Make sure to include the central location in our csv.

View File

@ -62,7 +62,7 @@ private:
int frame;
int *index_grid(double pos[]) {
int *index_grid(std::array<double, 2> pos) {
int halfGrid = gridSize / 2;
int i = (int) (pos[0] + halfGrid);
int j = (int) (pos[1] + halfGrid);
@ -97,15 +97,15 @@ public:
void pauseRunning() { running = false; }
// if pos is outside the cluster radius then set clusterRadius to be the distance to pos.
void updateClusterRadius(double pos[]);
void updateClusterRadius(std::array<double, 2> pos);
// set and read grid entries associated with a given position
void setGrid(double pos[], int val);
void setGrid(std::array<double, 2> pos, int val);
int readGrid(double pos[]);
int readGrid(std::array<double, 2> pos);
// return the distance of a given point from the origin
double distanceFromOrigin(double pos[]) {
double distanceFromOrigin(std::array<double, 2> pos) {
return sqrt(pos[0] * pos[0] + pos[1] * pos[1]);
}
@ -115,14 +115,14 @@ public:
void setParticleInactive() { lastParticleIsActive = 0; }
// add a particle at pos
void addParticle(double pos[]);
void addParticle(std::array<double, 2> pos);
// add a particle at a random point on the addCircle
void addParticleOnAddCircle();
// assign setpos to the position of a neighbour of pos
// which neighbour we look at is determined by val (=0,1,2,3)
void setPosNeighbour(double setpos[], double pos[], int val);
void setPosNeighbour(std::array<double, 2> &setpos, std::array<double, 2> pos, int val);
// this attempts to move the last particle in the List to a random neighbour
// if the neighbour is occupied then nothing happens

View File

@ -1,22 +1,14 @@
#pragma once
#include <array>
class Particle {
public:
static const int dim = 2; // we are in two dimensions
double *pos; // pointer to an array of size dim, to store the position
std::array<double, dim> pos{};
// default constructor
Particle() {
pos = new double[dim];
// constructor, with a specified initial pos
explicit Particle(std::array<double, dim> pos) {
this->pos[0] = pos[0];
this->pos[1] = pos[1];
}
// 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];
}
// destructor
~Particle() { delete[] pos; }
};