Skip to content

Instantly share code, notes, and snippets.

@arnholm
Last active May 6, 2019 07:57
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/2ad1cb525803695c73de37b84e480d4a to your computer and use it in GitHub Desktop.
Save arnholm/2ad1cb525803695c73de37b84e480d4a to your computer and use it in GitHub Desktop.
AngelCAD code for Wikipedia CSG model

AngelCAD code for the model at https://en.wikipedia.org/wiki/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);
}
@arnholm
Copy link
Author

arnholm commented Sep 28, 2017

wikipedia_csg

@arnholm
Copy link
Author

arnholm commented Sep 28, 2017

Result of AngelCAD code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment