Skip to content
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

[CSG] Add Empty Shape #290

Merged

Conversation

joeng03
Copy link
Contributor

@joeng03 joeng03 commented Mar 13, 2024

Description

Fixes #289

Note: yarn lint --fix was run so some of the CSG files were also linted accordingly.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

// Menger sponge, see
// https://en.wikipedia.org/wiki/Menger_sponge

import { union, subtract,intersect, translate, rotate, scale, render, 
         cube, blue,rounded_cube, empty_shape }
from 'csg';

const c = cube('#88ccff');

const d = scale(cube(blue), 1.5, 1.5, 1.5);

const empty = empty_shape();

function beside(s1, s2) {
    return union(scale(s1, 0.5, 1, 1),
                 translate(scale(s2, 0.5, 1, 1),
                           0.5, 0, 0));
}
function beside_frac(f, s1, s2) {
    return union(scale(s1, f, 1, 1),
                 translate(scale(s2, 1 - f, 1, 1),
                           f, 0, 0));
}
function before(s1, s2) {
    return union(scale(s1, 1, 0.5, 1),
                 translate(scale(s2, 1, 0.5, 1),
                           0, 0.5, 0));
}
function before_frac(f, s1, s2) {
    return union(scale(s1, 1, f, 1),
                 translate(scale(s2, 1, 1 - f, 1),
                           0, f, 0));
}

function below(s1, s2) {
    return union(scale(s1, 1, 1, 0.5),
                 translate(scale(s2, 1, 1, 0.5),
                           0, 0, 0.5));
}
function below_frac(f, s1, s2) {
    return union(scale(s1, 1, 1, f),
                 translate(scale(s2, 1, 1, 1 - f),
                           0, 0, f));
}

function menger(s, n) {
    if (n === 1) {
       return s;
    } else {
       const f = 1/3;
       const m = menger(s, n - 1);
       const outer_row = before_frac(f, m,
                               before(m, m));
       const inner_row = before_frac(f, m,
                               before(empty, m));
       const outer_layer = beside_frac(f, outer_row,
                                 beside(inner_row,
                                        outer_row));
       const inner_layer = beside_frac(f, inner_row,
                                 beside(empty,
                                        inner_row));
                                
       return below_frac(f, outer_layer,
                         below(inner_layer, 
                               outer_layer));
    }
}

const menger_sponge = menger(c, 4);

render(menger_sponge);

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@martin-henz martin-henz merged commit 122b3b8 into source-academy:master Mar 14, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CSG]: empty shape
3 participants