|
| 1 | +#include <cmath> |
| 2 | +#include <fstream> |
| 3 | +#include <random> |
| 4 | +#include <vector> |
| 5 | + |
| 6 | +// Simple X-Y point structure, along with some operators |
| 7 | +struct Point { |
| 8 | + double x, y; |
| 9 | +}; |
| 10 | + |
| 11 | +Point operator+(Point lhs, Point rhs) { return {lhs.x + rhs.x, lhs.y + rhs.y}; } |
| 12 | +Point operator*(double k, Point pt) { return {k * pt.x, k * pt.y}; } |
| 13 | +Point operator*(Point pt, double k) { return k * pt; } |
| 14 | + |
| 15 | +using PointVector = std::vector<Point>; |
| 16 | + |
| 17 | +// Returns a pseudo-random number generator |
| 18 | +std::default_random_engine& rng() { |
| 19 | + // Initialize static pseudo-random engine with non-deterministic random seed |
| 20 | + static std::default_random_engine randEngine(std::random_device{}()); |
| 21 | + return randEngine; |
| 22 | +} |
| 23 | + |
| 24 | +// Returns a random double in [0, 1) |
| 25 | +double drand() { |
| 26 | + return std::uniform_real_distribution<double>(0.0, 1.0)(rng()); |
| 27 | +} |
| 28 | + |
| 29 | +// Returns a random integer in [0, numElems-1] |
| 30 | +std::size_t randrange(std::size_t numElems) { |
| 31 | + return std::uniform_int_distribution<std::size_t>(0, numElems - 1)(rng()); |
| 32 | +} |
| 33 | + |
| 34 | +// Return a random point from the non-empty PointVector |
| 35 | +Point choose(const PointVector& points) { |
| 36 | + return points[randrange(points.size())]; |
| 37 | +} |
| 38 | + |
| 39 | +// This is a function to simulate a "chaos game" |
| 40 | +PointVector chaosGame(int numOutputPoints, const PointVector& inputPoints) { |
| 41 | + // Choose first point randomly |
| 42 | + Point curPoint = {drand(), drand()}; |
| 43 | + |
| 44 | + // For each output point, compute midpoint to random input point |
| 45 | + PointVector outputPoints(numOutputPoints); |
| 46 | + for (auto& outPoint : outputPoints) { |
| 47 | + outPoint = curPoint; |
| 48 | + curPoint = 0.5 * (curPoint + choose(inputPoints)); |
| 49 | + } |
| 50 | + |
| 51 | + return outputPoints; |
| 52 | +} |
| 53 | + |
| 54 | +int main() { |
| 55 | + // This will generate a Sierpinski triangle with a chaos game of n points for |
| 56 | + // an initial triangle with three points on the vertices of an equilateral |
| 57 | + // triangle. |
| 58 | + PointVector inputPoints = {{0.0, 0.0}, {0.5, std::sqrt(0.75)}, {1.0, 0.0}}; |
| 59 | + auto outputPoints = chaosGame(10000, inputPoints); |
| 60 | + |
| 61 | + // It will output the file sierpinski.dat, which can be plotted after |
| 62 | + std::ofstream ofs("sierpinski.dat"); |
| 63 | + for (auto pt : outputPoints) |
| 64 | + ofs << pt.x << '\t' << pt.y << '\n'; |
| 65 | +} |
0 commit comments