Skip to content

Instantly share code, notes, and snippets.

View Ben1980's full-sized avatar

Benjamin Mahr Ben1980

View GitHub Profile
@Ben1980
Ben1980 / initialFolderStructure.txt
Last active February 12, 2019 20:24
Folder structure of n-body-problem project at https://thoughts-on-cpp.com
C:.
| .gitignore
| CMakeLists.txt (1)
| LICENSE
| README.md
|
+---build (2)
+---cmake (3)
| FindCatch.cmake
|
@Ben1980
Ben1980 / rootCMakeLists.txt
Created February 12, 2019 20:44
Root CMakeLists.txt file of n-body-project
cmake_minimum_required(VERSION 3.1...3.13)
if(${CMAKE_VERSION} VERSION_LESS 3.12)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
endif()
project(gravity VERSION 0.1.0
DESCRIPTION "N-Body-Problem project of https://thoughts-on-cpp.com"
LANGUAGES CXX)
@Ben1980
Ben1980 / solverCMakeLists.txt
Last active February 13, 2019 20:03
Solver CMakeLists.txt file of n-body-project
add_library(solver
SHARED
src/solver.cpp)
target_include_directories(solver
PUBLIC
include)
add_executable(solverTest
test/test-main.cpp
@Ben1980
Ben1980 / build.txt
Last active February 13, 2019 20:09
Standard CMake build commands
mkdir build //(1) We need a build directory
cd build
cmake .. //(2) Generating the the artifacts to build, if necesarry download Catch2 via -DDOWNLOAD_CATCH=1 parameter
make //(3) Build the binaries on linux, for windows you might need different commands, e.g. msbuild gravity.sln for a visual studio based build
ctest //(4) Running tests
@Ben1980
Ben1980 / constProblemWithPointers.h
Last active February 14, 2019 20:59
Problem with const pointers
class MyClass {
public:
int * getArray() const {
arr[0]++;
return arr;
}
void setFirstVal(int val) { arr[0] = val; }
private:
int * arr = new int[10];
struct Complex {
double real;
double imaginary;
};
@Ben1980
Ben1980 / stdsort.h
Last active February 21, 2019 20:50
template<class RandomIt, class Compare>
constexpr void sort(RandomIt first, RandomIt last, Compare comp); //constexpr since C++ 20
std::vector<Complex> vec = { { 3, 1},
{ 1, 9 },
{ 8, 2 },
{ 3, 14 }};
double Complex::*var;
auto comparator = [&var](const Complex &a, const Complex &b) { return a.*var < b.*var; };
var = &Complex::real; //Sorting for real numbers
std::sort(vec.begin(), vec.end(), comparator);
TEST_CASE( "Explicit euler algorithm with two point mass", "[euler]" ) {
Solver solver;
ParticleBuilder particleBuilder;
Particle particleA = particleBuilder
.position()
.mass()
.build();
double Inverse(double value) { return -value; }
Particle GenerateStandardParticle(double xPosition, double yPosition)
{
ParticleBuilder particleBuilder;
return particleBuilder
.position({xPosition, yPosition})
.mass(1e10)
.build();