From 788d9a2de103a6ff5ca5e771ceda011af1ec9ebf Mon Sep 17 00:00:00 2001 From: Gathros <6323830+Gathros@users.noreply.github.com> Date: Fri, 12 Jun 2020 17:38:20 +0100 Subject: [PATCH 1/2] Add IFS.c --- contents/IFS/code/c/IFS.c | 49 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 contents/IFS/code/c/IFS.c diff --git a/contents/IFS/code/c/IFS.c b/contents/IFS/code/c/IFS.c new file mode 100644 index 000000000..7d79d9043 --- /dev/null +++ b/contents/IFS/code/c/IFS.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +struct point { + double x, y; +}; + +double drand() { + return ((double) rand() / (RAND_MAX)); +} + +struct point random_element(struct point *array, size_t n) { + return array[rand() % (int)n]; +} + +void chaos_game(struct point *in, size_t in_n, struct point *out, + size_t out_n) { + + struct point cur_point = {drand(), drand()}; + + for (int i = 0; i < out_n; ++i) { + out[i] = cur_point; + struct point tmp = random_element(in, in_n); + cur_point.x = 0.5 * (cur_point.x + tmp.x); + cur_point.y = 0.5 * (cur_point.y + tmp.y); + } +} + +int main() { + struct point shape_points [3] = {{0.0,0.0}, {0.5,sqrt(0.75)}, {1.0,0.0}}; + struct point out_points[1000]; + + srand(time(NULL)); + + chaos_game(shape_points, 3, out_points, 1000); + + FILE *fp = fopen("out.dat", "w+"); + + for (int i = 0; i < 1000; ++i) { + fprintf(fp, "%f\t%f\n", out_points[i].x, out_points[i].y); + } + + fclose(fp); + + return 0; +} + From 8e165f2805a78aa31ea78b7d81a7fd43979e9d57 Mon Sep 17 00:00:00 2001 From: Gathros <6323830+Gathros@users.noreply.github.com> Date: Fri, 12 Jun 2020 17:42:11 +0100 Subject: [PATCH 2/2] Updating IFS.md --- contents/IFS/IFS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/contents/IFS/IFS.md b/contents/IFS/IFS.md index 7cb36047b..93c2d241f 100644 --- a/contents/IFS/IFS.md +++ b/contents/IFS/IFS.md @@ -132,6 +132,8 @@ Here, instead of tracking children of children, we track a single individual tha [import:39-52, lang:"cpp"](code/c++/IFS.cpp) {% sample lang="py" %} [import:5-12, lang:"python"](code/python/IFS.py) +{% sample lang="c" %} +[import:18-29, lang:"c"](code/c/IFS.c) {% 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: @@ -199,6 +201,8 @@ In addition, we have written the chaos game code to take in a set of points so t [import, lang:"cpp"](code/c++/IFS.cpp) {% sample lang="py" %} [import, lang:"python"](code/python/IFS.py) +{% sample lang="c" %} +[import, lang:"c"](code/c/IFS.c) {% endmethod %} ### Bibliography