source-academy/modules

[CSG]: include Sierpinsky example in samples

Closed this issue · 1 comments

Feature

Include the Sierpinsky sample in the sample snippets and documentation. Here is the program:

https://share.sourceacademy.org/adkx8

As part of recent changes to CSG, the pyramid primitive that the Sierpinski snippet makes use of has been changed. I have yet to submit a PR for these changes, but will do so soon.

The changes also include adding your snippet to the CSG samples folder, although the snippet is the one sent on Telegram which appears to be different from this more updated version. I had tweaked the impacted part of the snippet due to the change to the pyramid primitive, in order to achieve a similar result as before.

I could try to incorporate the changes of this newer snippet into the existing sample and have that as part of my PR. Once the PR gets merged, it should resolve this issue.

One thing I noticed in the new snippet is that the n=2 sphere version of the fractal appears to load indefinitely for me. I do not think it is just due to computational expense. I suspect some kind of bug with the union function due to the unioned Shapes not actually touching each other. I suspect so because reducing the translation amount between the spheres allows the Shape to render almost instantly:

image

This issue may be related to #227. So perhaps the issue lies somewhere in our code, our patch file, or the JSCAD library.

Current snippet pasted below for reference:

// Source §1
// Prof Martin's Sierpinski triangle fractal

import { union, translate, scale, render, pyramid }
from 'csg';

const r = 1; // vertical stretch factor
const shape = scale(pyramid('#edd4c8'), 1, 1, r);

function repeat(n, trans, s) {
    return n === 0
            ? s
            : repeat(n - 1, trans, trans(s));
}

function sierpinski(o) {
    const t1 = translate(o,  0.5,  0.5, -r);
    const t2 = translate(o, -0.5,  0.5, -r);
    const t3 = translate(o,  0.5, -0.5, -r);
    const t4 = translate(o, -0.5, -0.5, -r);
    const s = union(o,
                    union(union(t1, t2),
                            union(t3, t4)));
    const s_scaled = scale(s, 0.5, 0.5, 0.5);
    return s_scaled;
}

render(repeat(5, sierpinski, shape));

image