Hi OpenSCAD people,
I work a lot in 2D and I really mis a way to create a way to create a pie (pizza slice) shape. A workaround for me is the following function in which I create a circle and then remove parts of it with rectangles, but this is limited to whole quarters.
So my plan was to create a circle with a polygon, but then I need to create a array with a dynamic length (according to $fn). For example, if $fn is 3 I want to create (part of) a circle out of 3 points, but when it's 6 I need 6 points. Is there any way to do this?
Thanks in advance, Best regards, Peter Uithoven

Hi Peter, Here's how I would approach the problem for pie slices of variable angle less than 90 degrees: snip module pie_slice(r=3.0,a=30) { $fn=64; intersection() { circle(r=r); square(r); rotate(a90) square(r); } } pie_slice(r=10,a=15); endsnip Andrew. On 20120821, at 10:05 AM, Peter Uithoven wrote: Hi OpenSCAD people,  "The future is already here. It's just not very evenly distributed"  William Gibson Me: http://clothbot.com/wiki/ 
In reply to this post by Peter Uithoven
> A workaround for me is the following function in which I create a circle and > then remove parts of it with rectangles, but this is limited to whole quarters
If you rotate the rectangles then you can create any angle you need in the circle.
HTH, Ian

In reply to this post by Peter Uithoven
On Tue, 21 Aug 2012 16:05:05 +0200
Peter Uithoven <[hidden email]> wrote: > Hi OpenSCAD people, > > I work a lot in 2D and I really mis a way to create a way to create a pie > (pizza slice) shape. > > A workaround for me is the following function in which I create a circle > and then remove parts of it with rectangles, but this is limited to whole > quarters. Why not just remove a triangle from it ? Alan 
I think this is a general solution for any angle to any angle:
module pie_slice(r, start_angle, end_angle) { R = r * sqrt(2) + 1; a0 = (4 * start_angle + 0 * end_angle) / 4; a1 = (3 * start_angle + 1 * end_angle) / 4; a2 = (2 * start_angle + 2 * end_angle) / 4; a3 = (1 * start_angle + 3 * end_angle) / 4; a4 = (0 * start_angle + 4 * end_angle) / 4; if(end_angle > start_angle) intersection() { circle(r); polygon([ [0,0], [R * cos(a0), R * sin(a0)], [R * cos(a1), R * sin(a1)], [R * cos(a2), R * sin(a2)], [R * cos(a3), R * sin(a3)], [R * cos(a4), R * sin(a4)], [0,0] ]); } } pie_slice(50, 40, 120); It works by covering the wanted bit of the circle with a fan of triangles, each 1/4 of the angle. That means the angle is no more than a right angle so the triangle edge boundary is made outside the circle by increasing the radius by sqrt(2) On 21 August 2012 15:36, Alan Cox <[hidden email]> wrote:

A more concise version but contains an approximation to the point in the centre.
module point(x,y) translate([x,y]) circle(0.01); module pie_slice(r, a0, a1) { $fa = 5; R = r * sqrt(2) + 1; intersection() { circle(r); hull() { point(0,0); for(i = [0:4]) assign(a = ((4  i) * a0 + i * a1) / 4) point(R * cos(a), R * sin(a)); }
} } pie_slice(50, 120, 40); On 21 August 2012 16:55, nop head <[hidden email]> wrote: I think this is a general solution for any angle to any angle: 
I made a small change so that it would do the slice in 1 degree sections regardless of size.
module point(x,y) translate([x,y]) circle(0.01); module pie_slice(r, a0, a1) { $fa = 5; R = r * 1.02; intersection() { circle(r); hull() { point(0,0); for(i = [0:(a0a1)]) assign(a = ((4  i) * a0 + i * a1) /(a0a1)) point(R * cos(a), R * sin(a)); } } } pie_slice(50, 120,90); On Tue, Aug 21, 2012 at 11:31 AM, nop head <[hidden email]> wrote: A more concise version but contains an approximation to the point in the centre. 
What is the benefit? The circumference of the circle is defined by the $fa, not the number of triangle it is intersected with. Doesn't this just slow it down?
On 21 August 2012 17:59, Kevin Crowley <[hidden email]> wrote: I made a small change so that it would do the slice in 1 degree sections regardless of size. 
I adjusted an earlier script so that you can do ellipses as well
$fn = 30;
On Tue, Aug 21, 2012 at 7:07 PM, nop head <[hidden email]> wrote: What is the benefit? The circumference of the circle is defined by the $fa, not the number of triangle it is intersected with. Doesn't this just slow it down? 
In reply to this post by nophead
It does take more time and that is a drawback but what I was working toward without realizing it is that part of your code would let you make slices of pie independent of any circle.
module point(x,y) translate([x,y]) circle(0.01); module pie_slice(R, a0, a1) { $fa = 5; if(a0>a1){ hull() { point(0,0); for(i = [0:(a0a1)]) assign(a = ((4  i) * a0 + i * a1) /(a0a1)) point(R * cos(a), R * sin(a)); } } } pie_slice(50, 120,90); On Tue, Aug 21, 2012 at 12:07 PM, nop head <[hidden email]> wrote: What is the benefit? The circumference of the circle is defined by the $fa, not the number of triangle it is intersected with. Doesn't this just slow it down? 
Yes hull() allows computed geometry without needing dynamic arrays, but only when convex.
On 21 August 2012 18:39, Kevin Crowley <[hidden email]> wrote: It does take more time and that is a drawback but what I was working toward without realizing it is that part of your code would let you make slices of pie independent of any circle. 
This yields just an arc segment which is both vex and cave.
module point(x,y) translate([x,y]) circle(0.01); module pie_slice(R, a0, a1) { $fa = 5; if(a0>a1){ hull() { point(0,0); for(i = [0:(a0a1)]) assign(a = ((4  i) * a0 + i * a1) /(a0a1)) point(R * cos(a), R * sin(a)); } } } difference(){ pie_slice(50, 120,90); circle(49); } On Tue, Aug 21, 2012 at 12:53 PM, nop head <[hidden email]> wrote: Yes hull() allows computed geometry without needing dynamic arrays, but only when convex. 
My OpenSCAD doesn't seem to recognise point, is this added in a dev version?
Best regards, Peter Uithoven
On Tue, Aug 21, 2012 at 9:12 PM, Kevin Crowley <[hidden email]> wrote: This yields just an arc segment which is both vex and cave. 
No it the first line of the script:
module point(x,y) translate([x,y]) circle(0.01);
It is my approximation to a point. If openscad had one it would be useful. Also an operation like hull that would take a set of child points and make then into a polygon would then allow concave polygons with a run time computed number of vertices to be generated and presumably would be a lot faster than hull, which is overkill for making simple polygons and can only make convex shapes.
On 22 August 2012 00:20, Peter Uithoven <[hidden email]> wrote: My OpenSCAD doesn't seem to recognise point, is this added in a dev version? 
Technically openscad (cgal) will create a point when you intersect the
corners of two cubes: module point(){ intersection(){ cube([1,1,1]); translate([1,1,1])cube([1,1,1]); } } Two problems with this though. 1) its not visible in openscad even when show edges is enabled 2) it crashes when used within a hull operation The point is there though, I tested this with rapcad, and it creates a visible point with show edges enabled, however it also crashes rapcad when used in a hull operation ;) Regards Giles On 22 August 2012 01:05, nop head <[hidden email]> wrote: > No it the first line of the script: module point(x,y) translate([x,y]) > circle(0.01); > > It is my approximation to a point. If openscad had one it would be useful. > Also an operation like hull that would take a set of child points and make > then into a polygon would then allow concave polygons with a run time > computed number of vertices to be generated and presumably would be a lot > faster than hull, which is overkill for making simple polygons and can only > make convex shapes. > > > > On 22 August 2012 00:20, Peter Uithoven <[hidden email]> wrote: >> >> My OpenSCAD doesn't seem to recognise point, is this added in a dev >> version? >> >> Best regards, >> Peter Uithoven >> >> >> On Tue, Aug 21, 2012 at 9:12 PM, Kevin Crowley <[hidden email]> wrote: >>> >>> This yields just an arc segment which is both vex and cave. >>> >>> >>> module point(x,y) translate([x,y]) circle(0.01); >>> >>> >>> module pie_slice(R, a0, a1) { >>> >>> $fa = 5; >>> >>> if(a0>a1){ >>> >>> hull() { >>> >>> point(0,0); >>> >>> for(i = [0:(a0a1)]) >>> >>> assign(a = ((4  i) * a0 + i * a1) /(a0a1)) >>> >>> point(R * cos(a), R * sin(a)); >>> >>> } >>> >>> } >>> >>> } >>> >>> >>> difference(){ >>> >>> pie_slice(50, 120,90); >>> >>> circle(49); >>> >>> } >>> >>> >>> >>> On Tue, Aug 21, 2012 at 12:53 PM, nop head <[hidden email]> wrote: >>>> >>>> Yes hull() allows computed geometry without needing dynamic arrays, but >>>> only when convex. >>>> >>>> >>>> On 21 August 2012 18:39, Kevin Crowley <[hidden email]> wrote: >>>>> >>>>> It does take more time and that is a drawback but what I was working >>>>> toward without realizing it is that part of your code would let you make >>>>> slices of pie independent of any circle. >>>>> >>>>> >>>>> module point(x,y) translate([x,y]) circle(0.01); >>>>> >>>>> >>>>> module pie_slice(R, a0, a1) { >>>>> >>>>> $fa = 5; >>>>> >>>>> if(a0>a1){ >>>>> >>>>> hull() { >>>>> >>>>> point(0,0); >>>>> >>>>> for(i = [0:(a0a1)]) >>>>> >>>>> assign(a = ((4  i) * a0 + i * a1) /(a0a1)) >>>>> >>>>> point(R * cos(a), R * sin(a)); >>>>> >>>>> } >>>>> >>>>> } >>>>> >>>>> } >>>>> >>>>> >>>>> pie_slice(50, 120,90); >>>>> >>>>> >>>>> >>>>> On Tue, Aug 21, 2012 at 12:07 PM, nop head <[hidden email]> wrote: >>>>>> >>>>>> What is the benefit? The circumference of the circle is defined by the >>>>>> $fa, not the number of triangle it is intersected with. Doesn't this just >>>>>> slow it down? >>>>>> >>>>>> >>>>>> >>>>>> On 21 August 2012 17:59, Kevin Crowley <[hidden email]> wrote: >>>>>>> >>>>>>> I made a small change so that it would do the slice in 1 degree >>>>>>> sections regardless of size. >>>>>>> >>>>>>> >>>>>>> >>>>>>> module point(x,y) translate([x,y]) circle(0.01); >>>>>>> >>>>>>> >>>>>>> >>>>>>> module pie_slice(r, a0, a1) { >>>>>>> >>>>>>> >>>>>>> $fa = 5; >>>>>>> >>>>>>> R = r * 1.02; >>>>>>> >>>>>>> intersection() { >>>>>>> >>>>>>> circle(r); >>>>>>> >>>>>>> hull() { >>>>>>> >>>>>>> point(0,0); >>>>>>> >>>>>>> for(i = [0:(a0a1)]) >>>>>>> >>>>>>> assign(a = ((4  i) * a0 + i * a1) /(a0a1)) >>>>>>> >>>>>>> point(R * cos(a), R * sin(a)); >>>>>>> >>>>>>> } >>>>>>> >>>>>>> } >>>>>>> >>>>>>> } >>>>>>> >>>>>>> >>>>>>> pie_slice(50, 120,90); >>>>>>> >>>>>>> >>>>>>> >>>>>>> On Tue, Aug 21, 2012 at 11:31 AM, nop head <[hidden email]> >>>>>>> wrote: >>>>>>>> >>>>>>>> A more concise version but contains an approximation to the point in >>>>>>>> the centre. >>>>>>>> >>>>>>>> module point(x,y) translate([x,y]) circle(0.01); >>>>>>>> >>>>>>>> >>>>>>>> module pie_slice(r, a0, a1) { >>>>>>>> >>>>>>>> $fa = 5; >>>>>>>> >>>>>>>> R = r * sqrt(2) + 1; >>>>>>>> >>>>>>>> intersection() { >>>>>>>> >>>>>>>> circle(r); >>>>>>>> >>>>>>>> hull() { >>>>>>>> >>>>>>>> point(0,0); >>>>>>>> >>>>>>>> for(i = [0:4]) >>>>>>>> >>>>>>>> assign(a = ((4  i) * a0 + i * a1) / 4) >>>>>>>> >>>>>>>> point(R * cos(a), R * sin(a)); >>>>>>>> >>>>>>>> } >>>>>>>> >>>>>>>> } >>>>>>>> >>>>>>>> } >>>>>>>> >>>>>>>> >>>>>>>> pie_slice(50, 120, 40); >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> On 21 August 2012 16:55, nop head <[hidden email]> wrote: >>>>>>>>> >>>>>>>>> I think this is a general solution for any angle to any angle: >>>>>>>>> >>>>>>>>> module pie_slice(r, start_angle, end_angle) { >>>>>>>>> >>>>>>>>> R = r * sqrt(2) + 1; >>>>>>>>> >>>>>>>>> a0 = (4 * start_angle + 0 * end_angle) / 4; >>>>>>>>> >>>>>>>>> a1 = (3 * start_angle + 1 * end_angle) / 4; >>>>>>>>> >>>>>>>>> a2 = (2 * start_angle + 2 * end_angle) / 4; >>>>>>>>> >>>>>>>>> a3 = (1 * start_angle + 3 * end_angle) / 4; >>>>>>>>> >>>>>>>>> a4 = (0 * start_angle + 4 * end_angle) / 4; >>>>>>>>> >>>>>>>>> if(end_angle > start_angle) >>>>>>>>> >>>>>>>>> intersection() { >>>>>>>>> >>>>>>>>> circle(r); >>>>>>>>> >>>>>>>>> polygon([ >>>>>>>>> >>>>>>>>> [0,0], >>>>>>>>> >>>>>>>>> [R * cos(a0), R * sin(a0)], >>>>>>>>> >>>>>>>>> [R * cos(a1), R * sin(a1)], >>>>>>>>> >>>>>>>>> [R * cos(a2), R * sin(a2)], >>>>>>>>> >>>>>>>>> [R * cos(a3), R * sin(a3)], >>>>>>>>> >>>>>>>>> [R * cos(a4), R * sin(a4)], >>>>>>>>> >>>>>>>>> [0,0] >>>>>>>>> >>>>>>>>> ]); >>>>>>>>> >>>>>>>>> } >>>>>>>>> >>>>>>>>> } >>>>>>>>> >>>>>>>>> >>>>>>>>> pie_slice(50, 40, 120); >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>> It works by covering the wanted bit of the circle with a fan of >>>>>>>>> triangles, each 1/4 of the angle. That means the angle is no more than a >>>>>>>>> right angle so the triangle edge boundary is made outside the circle by >>>>>>>>> increasing the radius by sqrt(2) >>>>>>>>> >>>>>>>>> >>>>>>>>> On 21 August 2012 15:36, Alan Cox <[hidden email]> wrote: >>>>>>>>>> >>>>>>>>>> On Tue, 21 Aug 2012 16:05:05 +0200 >>>>>>>>>> Peter Uithoven <[hidden email]> wrote: >>>>>>>>>> >>>>>>>>>> > Hi OpenSCAD people, >>>>>>>>>> > >>>>>>>>>> > I work a lot in 2D and I really mis a way to create a way to >>>>>>>>>> > create a pie >>>>>>>>>> > (pizza slice) shape. >>>>>>>>>> > >>>>>>>>>> > A workaround for me is the following function in which I create >>>>>>>>>> > a circle >>>>>>>>>> > and then remove parts of it with rectangles, but this is limited >>>>>>>>>> > to whole >>>>>>>>>> > quarters. >>>>>>>>>> >>>>>>>>>> Why not just remove a triangle from it ? >>>>>>>>>> >>>>>>>>>> Alan >>>>>>>>>> _______________________________________________ >>>>>>>>>> OpenSCAD mailing list >>>>>>>>>> [hidden email] >>>>>>>>>> http://rocklinux.net/mailman/listinfo/openscad >>>>>>>>>> http://openscad.org  https://flattr.com/thing/121566 >>>>>>>>> >>>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> _______________________________________________ >>>>>>>> OpenSCAD mailing list >>>>>>>> [hidden email] >>>>>>>> http://rocklinux.net/mailman/listinfo/openscad >>>>>>>> http://openscad.org  https://flattr.com/thing/121566 >>>>>>> >>>>>>> >>>>>>> >>>>>>> _______________________________________________ >>>>>>> OpenSCAD mailing list >>>>>>> [hidden email] >>>>>>> http://rocklinux.net/mailman/listinfo/openscad >>>>>>> http://openscad.org  https://flattr.com/thing/121566 >>>>>> >>>>>> >>>>>> >>>>>> _______________________________________________ >>>>>> OpenSCAD mailing list >>>>>> [hidden email] >>>>>> http://rocklinux.net/mailman/listinfo/openscad >>>>>> http://openscad.org  https://flattr.com/thing/121566 >>>>> >>>>> >>>>> >>>>> _______________________________________________ >>>>> OpenSCAD mailing list >>>>> [hidden email] >>>>> http://rocklinux.net/mailman/listinfo/openscad >>>>> http://openscad.org  https://flattr.com/thing/121566 >>>> >>>> >>>> >>>> _______________________________________________ >>>> OpenSCAD mailing list >>>> [hidden email] >>>> http://rocklinux.net/mailman/listinfo/openscad >>>> http://openscad.org  https://flattr.com/thing/121566 >>> >>> >>> >>> _______________________________________________ >>> OpenSCAD mailing list >>> [hidden email] >>> http://rocklinux.net/mailman/listinfo/openscad >>> http://openscad.org  https://flattr.com/thing/121566 >> >> >> >> _______________________________________________ >> OpenSCAD mailing list >> [hidden email] >> http://rocklinux.net/mailman/listinfo/openscad >> http://openscad.org  https://flattr.com/thing/121566 > > > > _______________________________________________ > OpenSCAD mailing list > [hidden email] > http://rocklinux.net/mailman/listinfo/openscad > http://openscad.org  https://flattr.com/thing/121566 
All of these solutions seem overcomplicated or imprecise, so I felt compelled to post mine. Btw, the second line makes this work for all angles, not just those in the range of 0360 degrees (not sure that a few of the others work outside that range).
module slice(r = 10, deg = 30) { degn = (deg % 360 > 0) ? deg % 360 : deg % 360 + 360; difference() { circle(r); if (degn > 180) intersection_for(a = [0, 180  degn]) rotate(a) translate([r, 0, 0]) square(r * 2); else union() for(a = [0, 180  degn]) rotate(a) translate([r, 0, 0]) square(r * 2); } } slice(10, 135); On Tuesday, August 21, 2012 at 9:46 PM, Giles Bathgate wrote:

This post has NOT been accepted by the mailing list yet.
In reply to this post by clothbot
Sorry if this is a question with a really obvious answer, but what determines the thickness of the slice in the following code:
module pie_slice(r=3.0,a=30) { $fn=64; intersection() { circle(r=r); square(r); rotate(a90) square(r); } } pie_slice(r=10,a=15); 
Administrator

Hi jon,
a. your reply has not made it to the mailing list yet as it is still flagged "This post has NOT been accepted by the mailing list yet", you need to complete registration b. The pie_slice() is a 2D object (uses square/circle), to give it thickness you need to use linear_extrude() See http://en.wikibooks.org/wiki/OpenSCAD_User_Manual/2D_to_3D_Extrusion#Linear_Extrude
Admin  email* me if you need anything,
or if I've done something stupid... * click on my MichaelAtOz label, there is a link to email me. Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above. The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out! 
This post was updated on .
Here is a way to make a pie also
module pie(radius=90, angle=45) { // Create a pie slice, from [0..360] bigger = radius * 3; partial = angle / 12; intersection() { polygon ( points= [ [0,0], [bigger, 0], [ bigger * cos(partial * 1), bigger * sin(partial * 1)], [ bigger * cos(partial * 2), bigger * sin(partial * 2)], [ bigger * cos(partial * 3), bigger * sin(partial * 3)], [ bigger * cos(partial * 4), bigger * sin(partial * 4)], [ bigger * cos(partial * 5), bigger * sin(partial * 5)], [ bigger * cos(partial * 6), bigger * sin(partial * 6)], [ bigger * cos(partial * 7), bigger * sin(partial * 7)], [ bigger * cos(partial * 8), bigger * sin(partial * 8)], [ bigger * cos(partial * 9), bigger * sin(partial * 9)], [ bigger * cos(partial * 10), bigger * sin(partial * 10)], [ bigger * cos(partial * 11), bigger * sin(partial * 11)], [ bigger * cos(partial * 12), bigger * sin(partial * 12)] ]) ; circle (radius); } } pie (radius = 30, angle = 200); 
In reply to this post by Whosawhatsis
I spotted some weirdness with nophead's first generic solution to the pie slice when using it together with minkowski() to make a rounded slice  it only works for certain values of r:
minkowski() { Slice(45, 0, 60); // doesn't work //Slice(46, 0, 60); // does work circle(r = 3); } module Slice(r, start_angle, end_angle) { R = r * sqrt(2) + 1; a0 = (4 * start_angle + 0 * end_angle) / 4; a1 = (3 * start_angle + 1 * end_angle) / 4; a2 = (2 * start_angle + 2 * end_angle) / 4; a3 = (1 * start_angle + 3 * end_angle) / 4; a4 = (0 * start_angle + 4 * end_angle) / 4; if(end_angle > start_angle) intersection() { circle(r); polygon([ [0,0], [R * cos(a0), R * sin(a0)], [R * cos(a1), R * sin(a1)], [R * cos(a2), R * sin(a2)], [R * cos(a3), R * sin(a3)], [R * cos(a4), R * sin(a4)], [0,0] ]); } } Can anyone explain why? There are no errors in the output window. The problem doesn't manifest (for these values of r anyway!) in the concise version, or whosawhatsis version. PS  is there a better way of quoting code in this mailing list? 
Free forum by Nabble  Edit this page 