# Math behind logical operators on 3D boxes / cuboids

**URL:** <https://discourse.processing.org/t/math-behind-logical-operators-on-3d-boxes-cuboids/32318>\
**Category:** Project Guidance\
**Created:** [September 19, 2021, 5:15pm UTC](https://discourse.processing.org/t/math-behind-logical-operators-on-3d-boxes-cuboids/32318 "2021-09-19T17:15:51Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 22, 2021, 10:41am UTC](https://discourse.processing.org/t/math-behind-logical-operators-on-3d-boxes-cuboids/32318/6 "2021-09-22T10:41:05Z")

</div>

> [@jeremydouglass](#):
>
> @Chrisir also made a demo – I’m not sure, but he may also use JCSG in some of his movies?

That’s right

for example [Some Movies I made - #14 by Chrisir](https://discourse.processing.org/t/some-movies-i-made/13306/14)

In one corner of the stage there is a light gray cube that’s hollowed out. CSG

Code for the moebius strip that also features that CUBE: [https://github.com/Kango/3DSketches/tree/master/Moebius4](https://github.com/Kango/3DSketches/tree/master/Moebius4)

You need the 2 jars and then init

```auto

//https://stackoverflow.com/questions/56999816/is-it-possible-to-use-jcsg-library-with-processing
//https://discourse.processing.org/t/csg-constructive-solid-geometry/12693

// the PShape reference which will contain the converted 
PShape csgResult;

void initCSG() {

  // JCSG sample code:
  // we use cube and sphere as base geometries
  CSG cube = new Cube(2).toCSG();
  CSG sphere = new Sphere(1.25).toCSG();

  // perform difference 
  CSG cubeMinusSphere = cube.difference(sphere);  

  // Convert CSG to PShape -> Note: CSG units are small so we scale them up so the shapes are visible in Processing
  csgResult = CSGToPShape(cubeMinusSphere, 45);

  println("use mouse to rotate");
}

//-----------------------------------------------------------------------------

// re-usable function to convert a CSG mesh to a Processing PShape
PShape CSGToPShape(CSG mesh, float scale) {
  // allocate a PShape group
  PShape csgResult = createShape(GROUP);
  // for each CSG polygon (Note: these can have 3,4 or more vertices)
  for (Polygon p : mesh.getPolygons()) {
    // make a child PShape
    PShape polyShape = createShape();
    // begin setting vertices to it
    polyShape.beginShape();
    // for each vertex in the polygon
    for (Vertex v : p.vertices) {
      // add each (scaled) polygon vertex 
      polyShape.vertex((float)v.pos.getX() * scale, (float)v.pos.getY() * scale, (float)v.pos.getZ() * scale);
    }
    // finish this polygon
    polyShape.endShape();
    //append the child PShape to the parent
    csgResult.addChild(polyShape);
  }
  return csgResult;
}
//

```

* * *

AND display

```auto

  void showCSG() {
    pushMatrix(); 
    csgResult.setStroke(false); 
    translate (410, GENERAL_Y_HEIGHT-47, 404);
    shape(csgResult);
    popMatrix();
  }

```

---

_[View the full topic](https://discourse.processing.org/t/math-behind-logical-operators-on-3d-boxes-cuboids/32318)._
