[OpenSCAD] Creating pie (pizza slice) shape (need a dynamic length array)

whosawhatsis whosawhatsis at gmail.com
Wed Aug 22 07:10:23 CEST 2012


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 0-360 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:

> 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 <nop.head at gmail.com (mailto:nop.head at gmail.com)> 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 <peter at peteruithoven.nl (mailto:peter at peteruithoven.nl)> 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 <caohaoim at gmail.com (mailto:caohaoim at gmail.com)> 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:(a0-a1)])
> > > > 
> > > > assign(a = ((4 - i) * a0 + i * a1) /(a0-a1))
> > > > 
> > > > 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 <nop.head at gmail.com (mailto:nop.head at gmail.com)> wrote:
> > > > > 
> > > > > Yes hull() allows computed geometry without needing dynamic arrays, but
> > > > > only when convex.
> > > > > 
> > > > > 
> > > > > On 21 August 2012 18:39, Kevin Crowley <caohaoim at gmail.com (mailto:caohaoim at gmail.com)> 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:(a0-a1)])
> > > > > > 
> > > > > > assign(a = ((4 - i) * a0 + i * a1) /(a0-a1))
> > > > > > 
> > > > > > point(R * cos(a), R * sin(a));
> > > > > > 
> > > > > > }
> > > > > > 
> > > > > > }
> > > > > > 
> > > > > > }
> > > > > > 
> > > > > > 
> > > > > > pie_slice(50, 120,90);
> > > > > > 
> > > > > > 
> > > > > > 
> > > > > > On Tue, Aug 21, 2012 at 12:07 PM, nop head <nop.head at gmail.com (mailto:nop.head at gmail.com)> 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 <caohaoim at gmail.com (mailto:caohaoim at gmail.com)> 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:(a0-a1)])
> > > > > > > > 
> > > > > > > > assign(a = ((4 - i) * a0 + i * a1) /(a0-a1))
> > > > > > > > 
> > > > > > > > point(R * cos(a), R * sin(a));
> > > > > > > > 
> > > > > > > > }
> > > > > > > > 
> > > > > > > > }
> > > > > > > > 
> > > > > > > > }
> > > > > > > > 
> > > > > > > > 
> > > > > > > > pie_slice(50, 120,90);
> > > > > > > > 
> > > > > > > > 
> > > > > > > > 
> > > > > > > > On Tue, Aug 21, 2012 at 11:31 AM, nop head <nop.head at gmail.com (mailto:nop.head at gmail.com)>
> > > > > > > > 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 <nop.head at gmail.com (mailto:nop.head at gmail.com)> 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 <alan at lxorguk.ukuu.org.uk (mailto:alan at lxorguk.ukuu.org.uk)> wrote:
> > > > > > > > > > > 
> > > > > > > > > > > On Tue, 21 Aug 2012 16:05:05 +0200
> > > > > > > > > > > Peter Uithoven <peter at peteruithoven.nl (mailto:peter at peteruithoven.nl)> 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
> > > > > > > > > > > OpenSCAD at rocklinux.org (mailto:OpenSCAD at rocklinux.org)
> > > > > > > > > > > http://rocklinux.net/mailman/listinfo/openscad
> > > > > > > > > > > http://openscad.org - https://flattr.com/thing/121566
> > > > > > > > > > > 
> > > > > > > > > > 
> > > > > > > > > > 
> > > > > > > > > 
> > > > > > > > > 
> > > > > > > > > 
> > > > > > > > > _______________________________________________
> > > > > > > > > OpenSCAD mailing list
> > > > > > > > > OpenSCAD at rocklinux.org (mailto:OpenSCAD at rocklinux.org)
> > > > > > > > > http://rocklinux.net/mailman/listinfo/openscad
> > > > > > > > > http://openscad.org - https://flattr.com/thing/121566
> > > > > > > > > 
> > > > > > > > 
> > > > > > > > 
> > > > > > > > 
> > > > > > > > 
> > > > > > > > _______________________________________________
> > > > > > > > OpenSCAD mailing list
> > > > > > > > OpenSCAD at rocklinux.org (mailto:OpenSCAD at rocklinux.org)
> > > > > > > > http://rocklinux.net/mailman/listinfo/openscad
> > > > > > > > http://openscad.org - https://flattr.com/thing/121566
> > > > > > > > 
> > > > > > > 
> > > > > > > 
> > > > > > > 
> > > > > > > 
> > > > > > > _______________________________________________
> > > > > > > OpenSCAD mailing list
> > > > > > > OpenSCAD at rocklinux.org (mailto:OpenSCAD at rocklinux.org)
> > > > > > > http://rocklinux.net/mailman/listinfo/openscad
> > > > > > > http://openscad.org - https://flattr.com/thing/121566
> > > > > > > 
> > > > > > 
> > > > > > 
> > > > > > 
> > > > > > 
> > > > > > _______________________________________________
> > > > > > OpenSCAD mailing list
> > > > > > OpenSCAD at rocklinux.org (mailto:OpenSCAD at rocklinux.org)
> > > > > > http://rocklinux.net/mailman/listinfo/openscad
> > > > > > http://openscad.org - https://flattr.com/thing/121566
> > > > > > 
> > > > > 
> > > > > 
> > > > > 
> > > > > 
> > > > > _______________________________________________
> > > > > OpenSCAD mailing list
> > > > > OpenSCAD at rocklinux.org (mailto:OpenSCAD at rocklinux.org)
> > > > > http://rocklinux.net/mailman/listinfo/openscad
> > > > > http://openscad.org - https://flattr.com/thing/121566
> > > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > _______________________________________________
> > > > OpenSCAD mailing list
> > > > OpenSCAD at rocklinux.org (mailto:OpenSCAD at rocklinux.org)
> > > > http://rocklinux.net/mailman/listinfo/openscad
> > > > http://openscad.org - https://flattr.com/thing/121566
> > > > 
> > > 
> > > 
> > > 
> > > 
> > > _______________________________________________
> > > OpenSCAD mailing list
> > > OpenSCAD at rocklinux.org (mailto:OpenSCAD at rocklinux.org)
> > > http://rocklinux.net/mailman/listinfo/openscad
> > > http://openscad.org - https://flattr.com/thing/121566
> > > 
> > 
> > 
> > 
> > 
> > _______________________________________________
> > OpenSCAD mailing list
> > OpenSCAD at rocklinux.org (mailto:OpenSCAD at rocklinux.org)
> > http://rocklinux.net/mailman/listinfo/openscad
> > http://openscad.org - https://flattr.com/thing/121566
> > 
> 
> _______________________________________________
> OpenSCAD mailing list
> OpenSCAD at rocklinux.org (mailto:OpenSCAD at rocklinux.org)
> http://rocklinux.net/mailman/listinfo/openscad
> http://openscad.org - https://flattr.com/thing/121566
> 
> 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rocklinux.net/pipermail/openscad/attachments/20120822/f04fa685/attachment-0001.html


More information about the OpenSCAD mailing list