Skip to content

Instantly share code, notes, and snippets.

@arnholm
Last active August 16, 2019 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnholm/b5696e1820223be9fbdf85404ab28357 to your computer and use it in GitHub Desktop.
Save arnholm/b5696e1820223be9fbdf85404ab28357 to your computer and use it in GitHub Desktop.
csg_wikipedia - Constructive Solid Geometry
// AngelCAD code sample: csg_wikipedia.as
// https://en.wikipedia.org/wiki/Constructive_solid_geometry
// https://arnholm.github.io/angelcad-docs/

shape@ main_shape()
{
   // intersection of cube and sphere
   cube@       cu = cube(size:45,center:true);
   sphere@     sp = sphere(r:30);
   solid@    cusp = cu & sp;     // '&' = intersection

   // union of 3 cylinders
   cylinder@  cy1 = cylinder(r:17,h:60,center:true);
   solid@     cy2 = rotate_x(deg:90)*cy1;
   solid@     cy3 = rotate_y(deg:90)*cy1; 
   solid@ cyl_all = cy1+cy2+cy3; // '+' = union

   // difference of the 2 above
   return cusp - cyl_all;        // '-' = difference
}

void main()
{
   shape@ obj = main_shape();
   obj.write_xcsg(GetInputFullPath(),secant_tolerance:-1);
}
Display the source blob
Display the rendered blob
Raw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment