Skip to content

Commit

Permalink
Add a variety of masses and radii
Browse files Browse the repository at this point in the history
* Resolve: Add a variety of masses and radii (#6)

* (Revert changes to .idea/codeStyles)
  • Loading branch information
axionbuster authored Feb 12, 2024
1 parent b49e2b4 commit bfef050
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
3 changes: 2 additions & 1 deletion demo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ endif ()
set(CMAKE_WIN32_EXECUTABLE TRUE)

add_executable(grass main.cpp
Table.h)
Table.h
irhall.h)

target_link_libraries(grass raylib)
target_link_libraries(grass dyn)
Expand Down
20 changes: 20 additions & 0 deletions demo/irhall.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef GRASS_IRHALL_H
#define GRASS_IRHALL_H

// Irwin-Hall approximation to the uniform distribution.

namespace phy {

/// @brief Approximate a normal distribution using a function that generates a
/// uniform (0,1) variate each time it is called using the Irwin-Hall
/// approximation (and the central limit theorem).
template <typename F = float, typename A> F irhnormal(A u01) {
F s{};
for (unsigned char i = 0; i < 12; i++)
s += u01();
return s - F(6);
}

} // namespace phy

#endif // GRASS_IRHALL_H
29 changes: 27 additions & 2 deletions demo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
#include <sstream>
#include <vector>

#include <halton.h>

#include "Table.h"
#include "irhall.h"

using namespace phy;

Expand All @@ -19,6 +22,26 @@ static int do_main() {
return std::abs(p.kin.y0) > 5'000.0f;
};

// Quasi-random number generator.
// - Quality not so important.
// - Possibly reduce binary size by reusing code.
// - Possibly help with instruction cache by having simple code.
// - Definitely small state (1 short int vs. compared to, say, mt19937).
dyn::Halton qrng;
auto normal_variate = [&qrng]() {
auto &q = qrng;
// Approximate normal distribution using uniform random variates.
return phy::irhnormal([&q]() { return q.x01(); });
};
// Make a particle at rest at the origin with a random mass and radius.
auto random_particle = [&normal_variate]() {
// Random mass and radius, but not other properties.
auto mass = std::exp(normal_variate());
auto radius = std::exp(2.0f * normal_variate() - 3.0f);
// xy, v, m, r.
return Particle{{0}, {0}, mass, radius};
};

InitWindow(600, 600, "Basic 1,000 particle demo (click to add particles)");

// The physical table (store particles, etc.); backup.
Expand Down Expand Up @@ -158,8 +181,10 @@ static int do_main() {
auto m = GetMousePosition();
auto n = GetScreenToWorld2D(m, cam);
auto o = std::complex<float>(n.x, n.y);
// Position, velocity, mass, radius
table.emplace_back(o, 0.0f, MASS, RADIUS);
auto p = random_particle();
// Set location
p.kin.y0 = o;
table.push_back(p);

interactive.spawned_last_frame = true;
} else {
Expand Down

0 comments on commit bfef050

Please sign in to comment.