# Creating "pie slice" in OpenSCAD?

When making a cylinder, sometimes I need to only take a pie slice. I'm currently using this neat trick to make pie slices for angles under 90 degrees. However, I have need of a few angles over 90 but under 180 degrees. Is there a way to generalize/extend this to work for these bigger angles?

module pie_slice(r=3.0,a=30) {
intersection() {
circle(r=r);
square(r);
rotate(a-90) square(r);
}
}

pie_slice(r=10,a=15);

• I think the easiest would be to use difference. Circle minus two cubes with sides 2*R. (Or one of size 2*R and one of size R.) Commented Jul 20, 2019 at 22:19
• IIUC, this is similar to my current workaround (I've posted below for reference). I use union instead of intersection. Unfortunately, you have to examine how many degrees the caller wants and use one or the other - I have yet to find a universal method
– Hamy
Commented Jul 20, 2019 at 22:33
• Think of it this way: you have two rectangles that cover each half of the circle. Then you open a gap between them of the angle. That should work for any angle between 0 and 180. Commented Jul 20, 2019 at 22:38
• Oh snap, that sounds perfect. I'll give it a try, thanks
– Hamy
Commented Jul 20, 2019 at 22:46
• Or use a module that can make pie slices, e.g. Chamfers-for-OpenSCAD.
– 0scar
Commented Jul 21, 2019 at 6:58

This is what I use:

module pieSlice(a, r, h){
rotate_extrude(angle=a) square([r,h]);
}
pieSlice(110,20,3);


My current workaround is to use union instead of intersection. Unfortunately, that means I have to use an if clause which makes the code have two paths instead of one clean approach. Also, unlike the above method, this does not result in a clean cylindrical shape but must instead by combined with a proper cylinder to get the final pie slice

   size = length + 2;
if (angle_deg <= 90) {
translate([0,0,-1])
intersection() {
cube(size);
rotate(angle_deg-90) cube(size);
}
} else if (angle_deg <= 180) {
translate([0,0,-1])
union() {
cube(size);
rotate(angle_deg-90) cube(size);
}
} else {
echo(str("FAILURE - Angle cannot exceed 180"));
}



Although generating complex shapes by combining primitive OpenSCAD shapes is a well-established tradition, and is often all that is needed, it would be more elegant in this case to generate a pie slice directly using the polygon function and a list comprehension.

module pie_slice(r=3.0, a=30) {
polygon(points=[
[0, 0],
for(theta=0; theta<a; theta=theta+$fa) [r*cos(theta), r*sin(theta)], [r*cos(a), r*sin(a)] ]); }  Note that the above code is a little crude, since it does no error checking, but it works. It uses the $fa special variable for the step angle.