Skip to content

Implement IFS in C++ #701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ This file lists everyone, who contributed to this repo and wanted to show up her
- dovisutu
- Antetokounpo
- Akash Dhiman
- Vincent Zalzal
4 changes: 4 additions & 0 deletions contents/IFS/IFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ Here, instead of tracking children of children, we track a single individual tha
{% method %}
{% sample lang="jl" %}
[import:4-17, lang:"julia"](code/julia/IFS.jl)
{% sample lang="cpp" %}
[import:39-52, lang:"cpp"](code/c++/IFS.cpp)
{% endmethod %}

If we set the initial points to the on the equilateral triangle we saw before, we can see the Sierpinski triangle again after a few thousand iterations, as shown below:
Expand Down Expand Up @@ -180,6 +182,8 @@ In addition, we have written the chaos game code to take in a set of points so t
{% method %}
{% sample lang="jl" %}
[import, lang:"julia"](code/julia/IFS.jl)
{% sample lang="cpp" %}
[import, lang:"cpp"](code/c++/IFS.cpp)
{% endmethod %}

### Bibliography
Expand Down
65 changes: 65 additions & 0 deletions contents/IFS/code/c++/IFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <cmath>
#include <fstream>
#include <random>
#include <vector>

// Simple X-Y point structure, along with some operators
struct Point {
double x, y;
};

Point operator+(Point lhs, Point rhs) { return {lhs.x + rhs.x, lhs.y + rhs.y}; }
Point operator*(double k, Point pt) { return {k * pt.x, k * pt.y}; }
Point operator*(Point pt, double k) { return k * pt; }

using PointVector = std::vector<Point>;

// Returns a pseudo-random number generator
std::default_random_engine& rng() {
// Initialize static pseudo-random engine with non-deterministic random seed
static std::default_random_engine randEngine(std::random_device{}());
return randEngine;
}

// Returns a random double in [0, 1)
double drand() {
return std::uniform_real_distribution<double>(0.0, 1.0)(rng());
}

// Returns a random integer in [0, numElems-1]
std::size_t randrange(std::size_t numElems) {
return std::uniform_int_distribution<std::size_t>(0, numElems - 1)(rng());
}

// Return a random point from the non-empty PointVector
Point choose(const PointVector& points) {
return points[randrange(points.size())];
}

// This is a function to simulate a "chaos game"
PointVector chaosGame(int numOutputPoints, const PointVector& inputPoints) {
// Choose first point randomly
Point curPoint = {drand(), drand()};

// For each output point, compute midpoint to random input point
PointVector outputPoints(numOutputPoints);
for (auto& outPoint : outputPoints) {
outPoint = curPoint;
curPoint = 0.5 * (curPoint + choose(inputPoints));
}

return outputPoints;
}

int main() {
// This will generate a Sierpinski triangle with a chaos game of n points for
// an initial triangle with three points on the vertices of an equilateral
// triangle.
PointVector inputPoints = {{0.0, 0.0}, {0.5, std::sqrt(0.75)}, {1.0, 0.0}};
auto outputPoints = chaosGame(10000, inputPoints);

// It will output the file sierpinski.dat, which can be plotted after
std::ofstream ofs("sierpinski.dat");
for (auto pt : outputPoints)
ofs << pt.x << '\t' << pt.y << '\n';
}