This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ParticleBuilder { | |
public: | |
ParticleBuilder() : mMass(0) {} | |
ParticleBuilder & position(const Vector2D &position); | |
ParticleBuilder & velocity(const Vector2D &velocity); | |
ParticleBuilder & acceleration(const Vector2D &acceleration); | |
ParticleBuilder & mass(double mass); | |
Particle build() const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
solverTest is a Catch v2.6.1 host application. | |
Run with -? for options | |
------------------------------------------------------------------------------- | |
Explicit euler algorithm with two point mass | |
Two still standing point mass are attracting each other in x-direction | |
------------------------------------------------------------------------------- | |
/mnt/c/Develop/gravity/solverTest/src/solverTest.cpp:39 | |
............................................................................... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TEST_CASE( "Test Vector2D operators and functions", "[vector]" ) { | |
const Vector2D vecA = { 1, 1 }; | |
const Vector2D vecB = { 3, 3 }; | |
SECTION( "Comparing vectors" ) { | |
const Vector2D expected = vecA; | |
REQUIRE(vecA == expected); | |
} | |
SECTION( "Length of a vector" ) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Vector2D { | |
double x; | |
double y; | |
Vector2D() : x(0), y(0) {} | |
Vector2D(double x, double y) : x(x), y(y) {} | |
bool operator==(const Vector2D &rhs) const; | |
bool operator!=(const Vector2D &rhs) const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool Vector2D::operator==(const Vector2D &rhs) const { | |
auto equalZero = std::numeric_limits<double>::min(); | |
return fabs(x - rhs.x) <= equalZero && | |
fabs(y - rhs.y) <= equalZero; | |
} | |
bool Vector2D::operator!=(const Vector2D &rhs) const { | |
return !(rhs == *this); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
double Inverse(double value) { return -value; } | |
Particle GenerateStandardParticle(double xPosition, double yPosition) { | |
ParticleBuilder particleBuilder; | |
return particleBuilder | |
.position({xPosition, yPosition}) | |
.mass(1e10) | |
.build(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ParticleBuilder { | |
public: | |
ParticleBuilder() : mMass(0) {} | |
ParticleBuilder & position(const Vector2D &position); | |
ParticleBuilder & velocity(const Vector2D &velocity); | |
ParticleBuilder & acceleration(const Vector2D &acceleration); | |
ParticleBuilder & mass(double mass); | |
Particle build() const; | |
std::vector<Particle> build(size_t numberOfParticles); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ParticleBuilder & ParticleBuilder::position(const Vector2D &position) | |
{ | |
mPosition = position; | |
return *this; | |
} | |
ParticleBuilder & ParticleBuilder::velocity(const Vector2D &velocity) | |
{ | |
mVelocity = velocity; | |
return *this; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Particle { | |
public: | |
Particle(); | |
Particle(double mass, const Vector2D &acceleration, const Vector2D &velocity, const Vector2D &position); | |
bool operator==(const Particle &rhs) const; | |
bool operator!=(const Particle &rhs) const; | |
double getMass() const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solver { | |
public: | |
explicit Solver(double mEpsilon); | |
std::vector<Particle> solve(const std::vector<Particle> &particles) const; | |
private: | |
std::vector<Particle> calculateAcceleration(const std::vector<Particle> &particles) const; | |
std::vector<Particle> calculateVelocity(const std::vector<Particle> &particles) const; | |
std::vector<Particle> calculatePosition(const std::vector<Particle> &particles) const; |