discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Restrictions on functions and modules or .other alternative

Jan Öhman
Sun, Mar 27, 2022 11:14 PM

Hi!
First, I thought of writing a module() that performs some calculations and returns a value. (but failed.)Is it impossible to return any values from a module()?
This is a working module()
My wish would have been to return the variable "points" from the module(), instead of create the polygon()

radius = 10;angles = [0, 90];

module sector(radius, angles, fn = 24) {    r = radius / cos(180 / fn);    step = -360 / fn;
    points = concat([[0, 0]],        [for(a = [angles[0] : step : angles[1] - 360])             [r * cos(a), r * sin(a)]        ],        [[r * cos(angles[1]), r * sin(angles[1])]]    );
    difference() {        circle(radius, $fn = fn);        polygon(points);    }}

Then I tried to create a function() that handles some calculations and returns a value
(but failed.)Is it impossible to handle a few calculations in a function()?

can take the same example above - is it possible?

points = function ??? () .....

Is there another option to solve my wish?

Hi! First, I thought of writing a module() that performs some calculations and returns a value. (but failed.)Is it impossible to return any values from a module()? This is a working module() My wish would have been to return the variable "points" from the module(), instead of create the polygon() radius = 10;angles = [0, 90]; module sector(radius, angles, fn = 24) {    r = radius / cos(180 / fn);    step = -360 / fn;     points = concat([[0, 0]],        [for(a = [angles[0] : step : angles[1] - 360])             [r * cos(a), r * sin(a)]        ],        [[r * cos(angles[1]), r * sin(angles[1])]]    );     difference() {        circle(radius, $fn = fn);        polygon(points);    }} Then I tried to create a function() that handles some calculations and returns a value (but failed.)Is it impossible to handle a few calculations in a function()? can take the same example above - is it possible? points = function ??? () ..... Is there another option to solve my wish?
M
MichaelAtOz
Sun, Mar 27, 2022 11:20 PM

Modules create objects ie geometry 2D or 3D objects, that get displayed in preview/render.

The result of Modules can be used in 'Children()' but that is an advanced topic.

Functions return a single result, number, string, vector etc.


From: Jan Öhman [mailto:jan_ohman@yahoo.com]
Sent: Mon, 28 Mar 2022 10:15
To: OpenSCAD General Discussion
Subject: [OpenSCAD] Restrictions on functions and modules or .other alternative

Hi!

First, I thought of writing a module() that performs some calculations and returns a value. (but failed.)

Is it impossible to return any values from a module()?

This is a working module()
My wish would have been to return the variable "points" from the module(), instead of create the polygon()

radius = 10;

angles = [0, 90];

module sector(radius, angles, fn = 24) {

r = radius / cos(180 / fn);

step = -360 / fn;



points = concat([[0, 0]],

    [for(a = [angles[0] : step : angles[1] - 360]) 

        [r * cos(a), r * sin(a)]

    ],

    [[r * cos(angles[1]), r * sin(angles[1])]]

);



difference() {

    circle(radius, $fn = fn);

    polygon(points);

}

}

Then I tried to create a function() that handles some calculations and returns a value

(but failed.)

Is it impossible to handle a few calculations in a function()?

can take the same example above - is it possible?

points = function ??? () .....

Is there another option to solve my wish?

--
This email has been checked for viruses by AVG.
https://www.avg.com

Modules create objects ie geometry 2D or 3D objects, that get displayed in preview/render. The result of Modules can be used in 'Children()' but that is an advanced topic. Functions return a single result, number, string, vector etc. _____ From: Jan Öhman [mailto:jan_ohman@yahoo.com] Sent: Mon, 28 Mar 2022 10:15 To: OpenSCAD General Discussion Subject: [OpenSCAD] Restrictions on functions and modules or .other alternative Hi! First, I thought of writing a module() that performs some calculations and returns a value. (but failed.) Is it impossible to return any values from a module()? This is a working module() My wish would have been to return the variable "points" from the module(), instead of create the polygon() radius = 10; angles = [0, 90]; module sector(radius, angles, fn = 24) { r = radius / cos(180 / fn); step = -360 / fn; points = concat([[0, 0]], [for(a = [angles[0] : step : angles[1] - 360]) [r * cos(a), r * sin(a)] ], [[r * cos(angles[1]), r * sin(angles[1])]] ); difference() { circle(radius, $fn = fn); polygon(points); } } Then I tried to create a function() that handles some calculations and returns a value (but failed.) Is it impossible to handle a few calculations in a function()? can take the same example above - is it possible? points = function ??? () ..... Is there another option to solve my wish? -- This email has been checked for viruses by AVG. https://www.avg.com
FH
Father Horton
Sun, Mar 27, 2022 11:55 PM

What you want to do is possible in a function using let and recursion, but
I'm too tired to figure it out right now.

On Sun, Mar 27, 2022 at 6:21 PM MichaelAtOz oz.at.michael@gmail.com wrote:

Modules create objects ie geometry 2D or 3D objects, that get displayed in
preview/render.

The result of Modules can be used in 'Children()' but that is an advanced
topic.

Functions return a single result, number, string, vector etc.


From: Jan Öhman [mailto:jan_ohman@yahoo.com]
Sent: Mon, 28 Mar 2022 10:15
To: OpenSCAD General Discussion
Subject: [OpenSCAD] Restrictions on functions and modules or .other
alternative

Hi!

First, I thought of writing a module() that performs some calculations and
returns a value. (but failed.)

Is it impossible to return any values from a module()?

This is a working module()
My wish would have been to return the variable "points" from the module(),
instead of create the polygon()

radius = 10;

angles = [0, 90];

module sector(radius, angles, fn = 24) {

 r = radius / cos(180 / fn);

 step = -360 / fn;



 points = concat([[0, 0]],

     [for(a = [angles[0] : step : angles[1] - 360])

         [r * cos(a), r * sin(a)]

     ],

     [[r * cos(angles[1]), r * sin(angles[1])]]

 );



 difference() {

     circle(radius, $fn = fn);

     polygon(points);

 }

}

Then I tried to create a function() that handles some calculations and
returns a value

(but failed.)

Is it impossible to handle a few calculations in a function()?

can take the same example above - is it possible?

points = function ??? () .....

Is there another option to solve my wish?

http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient Virus-free.
www.avg.com
http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient
<#m_7098009986529729143_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

What you want to do is possible in a function using let and recursion, but I'm too tired to figure it out right now. On Sun, Mar 27, 2022 at 6:21 PM MichaelAtOz <oz.at.michael@gmail.com> wrote: > Modules create objects ie geometry 2D or 3D objects, that get displayed in > preview/render. > > The result of Modules can be used in 'Children()' but that is an advanced > topic. > > > > Functions return a single result, number, string, vector etc. > > > ------------------------------ > > *From:* Jan Öhman [mailto:jan_ohman@yahoo.com] > *Sent:* Mon, 28 Mar 2022 10:15 > *To:* OpenSCAD General Discussion > *Subject:* [OpenSCAD] Restrictions on functions and modules or .other > alternative > > > > Hi! > > > > First, I thought of writing a module() that performs some calculations and > returns a value. (but failed.) > > Is it impossible to return any values from a module()? > > This is a working module() > My wish would have been to return the variable "points" from the module(), > instead of create the polygon() > > > > radius = 10; > > angles = [0, 90]; > > > > > > module sector(radius, angles, fn = 24) { > > r = radius / cos(180 / fn); > > step = -360 / fn; > > > > points = concat([[0, 0]], > > [for(a = [angles[0] : step : angles[1] - 360]) > > [r * cos(a), r * sin(a)] > > ], > > [[r * cos(angles[1]), r * sin(angles[1])]] > > ); > > > > difference() { > > circle(radius, $fn = fn); > > polygon(points); > > } > > } > > > > > > > > > > Then I tried to create a function() that handles some calculations and > returns a value > > (but failed.) > > Is it impossible to handle a few calculations in a function()? > > > > can take the same example above - is it possible? > > points = function ??? () ..... > > > > > > Is there another option to solve my wish? > > > <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient> Virus-free. > www.avg.com > <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient> > <#m_7098009986529729143_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
NH
nop head
Mon, Mar 28, 2022 12:23 AM

function sector(radius, angles, fn = 24)  =
let(
r = radius / cos(180 / fn),
step = -360 / fn,
points = concat([[0, 0]],
[for(a = [angles[0] : step : angles[1] - 360])
[r * cos(a), r * sin(a)]
],
[[r * cos(angles[1]), r * sin(angles[1])]]
)
) points;

radius = 10;
angles = [0, 90];

echo(sector(radius, angles));

On Mon, 28 Mar 2022 at 00:55, Father Horton fatherhorton@gmail.com wrote:

What you want to do is possible in a function using let and recursion, but
I'm too tired to figure it out right now.

On Sun, Mar 27, 2022 at 6:21 PM MichaelAtOz oz.at.michael@gmail.com
wrote:

Modules create objects ie geometry 2D or 3D objects, that get displayed
in preview/render.

The result of Modules can be used in 'Children()' but that is an advanced
topic.

Functions return a single result, number, string, vector etc.


From: Jan Öhman [mailto:jan_ohman@yahoo.com]
Sent: Mon, 28 Mar 2022 10:15
To: OpenSCAD General Discussion
Subject: [OpenSCAD] Restrictions on functions and modules or .other
alternative

Hi!

First, I thought of writing a module() that performs some calculations
and returns a value. (but failed.)

Is it impossible to return any values from a module()?

This is a working module()
My wish would have been to return the variable "points" from the
module(), instead of create the polygon()

radius = 10;

angles = [0, 90];

module sector(radius, angles, fn = 24) {

 r = radius / cos(180 / fn);

 step = -360 / fn;



 points = concat([[0, 0]],

     [for(a = [angles[0] : step : angles[1] - 360])

         [r * cos(a), r * sin(a)]

     ],

     [[r * cos(angles[1]), r * sin(angles[1])]]

 );



 difference() {

     circle(radius, $fn = fn);

     polygon(points);

 }

}

Then I tried to create a function() that handles some calculations and
returns a value

(but failed.)

Is it impossible to handle a few calculations in a function()?

can take the same example above - is it possible?

points = function ??? () .....

Is there another option to solve my wish?

http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient Virus-free.
www.avg.com
http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient
<#m_6338052267016586034_m_7098009986529729143_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

function sector(radius, angles, fn = 24) = let( r = radius / cos(180 / fn), step = -360 / fn, points = concat([[0, 0]], [for(a = [angles[0] : step : angles[1] - 360]) [r * cos(a), r * sin(a)] ], [[r * cos(angles[1]), r * sin(angles[1])]] ) ) points; radius = 10; angles = [0, 90]; echo(sector(radius, angles)); On Mon, 28 Mar 2022 at 00:55, Father Horton <fatherhorton@gmail.com> wrote: > What you want to do is possible in a function using let and recursion, but > I'm too tired to figure it out right now. > > On Sun, Mar 27, 2022 at 6:21 PM MichaelAtOz <oz.at.michael@gmail.com> > wrote: > >> Modules create objects ie geometry 2D or 3D objects, that get displayed >> in preview/render. >> >> The result of Modules can be used in 'Children()' but that is an advanced >> topic. >> >> >> >> Functions return a single result, number, string, vector etc. >> >> >> ------------------------------ >> >> *From:* Jan Öhman [mailto:jan_ohman@yahoo.com] >> *Sent:* Mon, 28 Mar 2022 10:15 >> *To:* OpenSCAD General Discussion >> *Subject:* [OpenSCAD] Restrictions on functions and modules or .other >> alternative >> >> >> >> Hi! >> >> >> >> First, I thought of writing a module() that performs some calculations >> and returns a value. (but failed.) >> >> Is it impossible to return any values from a module()? >> >> This is a working module() >> My wish would have been to return the variable "points" from the >> module(), instead of create the polygon() >> >> >> >> radius = 10; >> >> angles = [0, 90]; >> >> >> >> >> >> module sector(radius, angles, fn = 24) { >> >> r = radius / cos(180 / fn); >> >> step = -360 / fn; >> >> >> >> points = concat([[0, 0]], >> >> [for(a = [angles[0] : step : angles[1] - 360]) >> >> [r * cos(a), r * sin(a)] >> >> ], >> >> [[r * cos(angles[1]), r * sin(angles[1])]] >> >> ); >> >> >> >> difference() { >> >> circle(radius, $fn = fn); >> >> polygon(points); >> >> } >> >> } >> >> >> >> >> >> >> >> >> >> Then I tried to create a function() that handles some calculations and >> returns a value >> >> (but failed.) >> >> Is it impossible to handle a few calculations in a function()? >> >> >> >> can take the same example above - is it possible? >> >> points = function ??? () ..... >> >> >> >> >> >> Is there another option to solve my wish? >> >> >> <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient> Virus-free. >> www.avg.com >> <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient> >> <#m_6338052267016586034_m_7098009986529729143_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
JB
Jordan Brown
Mon, Mar 28, 2022 4:39 PM

On 3/27/2022 5:23 PM, nop head wrote:

function sector(radius, angles, fn = 24)  =
    let(
        r = radius / cos(180 / fn),
        step = -360 / fn,
       points = concat([[0, 0]],
            [for(a = [angles[0] : step : angles[1] - 360])
                [r * cos(a), r * sin(a)]
            ],
            [[r * cos(angles[1]), r * sin(angles[1])]]
       )
    ) points;

radius = 10;
angles = [0, 90];

echo(sector(radius, angles));

Simplifying a little - the concat isn't necessary; you can just directly
create the list:

function sector(radius, angles, fn = 24)  =
    let(
        r = radius / cos(180 / fn),
        step = -360 / fn,
        points = [
            [0, 0],
            for(a = [angles[0] : step : angles[1] - 360])
                [r * cos(a), r * sin(a)],
            [r * cos(angles[1]), r * sin(angles[1])]
        ]
    ) points;

radius = 10;
angles = [0, 90];

echo(sector(radius, angles));
On 3/27/2022 5:23 PM, nop head wrote: > function sector(radius, angles, fn = 24)  = >     let( >         r = radius / cos(180 / fn), >         step = -360 / fn, >        points = concat([[0, 0]], >             [for(a = [angles[0] : step : angles[1] - 360]) >                 [r * cos(a), r * sin(a)] >             ], >             [[r * cos(angles[1]), r * sin(angles[1])]] >        ) >     ) points; > > radius = 10; > angles = [0, 90]; > > echo(sector(radius, angles)); Simplifying a little - the concat isn't necessary; you can just directly create the list: function sector(radius, angles, fn = 24) = let( r = radius / cos(180 / fn), step = -360 / fn, points = [ [0, 0], for(a = [angles[0] : step : angles[1] - 360]) [r * cos(a), r * sin(a)], [r * cos(angles[1]), r * sin(angles[1])] ] ) points; radius = 10; angles = [0, 90]; echo(sector(radius, angles));
Jan Öhman
Tue, Mar 29, 2022 10:00 AM

thank you! this is exactly what I was looking for - almost ...
This program (at the bottom) Is a bit modified towards the presented solution and creates two number series with coordinates.
But, how to combine several series? (this doesn't work)points1 =[[10, 10], [10, -0.0197717], [8.74419, 0.0592372], [7.50818, 0.295018], [6.31148, 0.683852], [5.17294, 1.21961], [4.11053, 1.89383], [3.14099, 2.6959], [2.27963, 3.61316], [1.54003, 4.63114], [0.933839, 5.73379], [0.470631, 6.90372], [0.157706, 8.12248], [0, 9.37085]]
points2 =[[10, 50], [-0.0197717, 50], [0.0592372, 51.2558], [0.295018, 52.4918], [0.683852, 53.6885], [1.21961, 54.8271], [1.89383, 55.8895], [2.6959, 56.859], [3.61316, 57.7204], [4.63114, 58.46], [5.73379, 59.0662], [6.90372, 59.5294], [8.12248, 59.8423], [9.37085, 60]]

polygon(points1, points2);

This instructions works as desire .:points =[[10, -0.0197717], [8.74419, 0.0592372], [7.50818, 0.295018], [6.31148, 0.683852], [5.17294, 1.21961], [4.11053, 1.89383], [3.14099, 2.6959], [2.27963, 3.61316], [1.54003, 4.63114], [0.933839, 5.73379], [0.470631, 6.90372], [0.157706, 8.12248], [0, 9.37085],        [-0.0197717, 50], [0.0592372, 51.2558], [0.295018, 52.4918], [0.683852, 53.6885], [1.21961, 54.8271], [1.89383, 55.8895], [2.6959, 56.859], [3.61316, 57.7204], [4.63114, 58.46], [5.73379, 59.0662], [6.90372, 59.5294], [8.12248, 59.8423], [9.37085, 60]];
polygon(points);

My program below
fn = 50;radius1 = 10;angles1 = [0, 90];rotate1 = 1;      // 1 medurs, -1 motursstartAngel1 = "bottom";x1 = 10;y1 = 10;
// points1 = circSect();points1 = circSect(x1, y1, radius1, angles1, startAngel1, rotate1, fn);polygon(points1);

points2 = circSect(10, 50, 10, [0, 90], "left", 1, fn);polygon(points2);

function circSect(x=0, y=0, radius=10, angles=[0,270], startRef="bottom", rotate=1, fn=24)  =    let(        startAngel  = (startRef == "left")   ?   0                    : (startRef == "bottom") ?  90                    : (startRef == "right")  ? 180                    : (startRef == "top")    ? 270                    : undef,                r = radius / cos(180 / fn),        step = 360 / fn,        points = [[x, y],            for(a = [angles[0]-startAngel : step : angles[1]-startAngel])                [-rotate * r * cos(a)+x, r * sin(a)+y]]    ) points;

Den måndag 28 mars 2022 18:39:37 CEST, Jordan Brown <openscad@jordan.maileater.net> skrev:  

On 3/27/2022 5:23 PM, nop head wrote:

function sector(radius, angles, fn = 24)  =
    let(
        r = radius / cos(180 / fn),
        step = -360 / fn,
       points = concat([[0, 0]],
            [for(a = [angles[0] : step : angles[1] - 360])
                [r * cos(a), r * sin(a)]
            ],
            [[r * cos(angles[1]), r * sin(angles[1])]]
       )
    ) points;

radius = 10;
angles = [0, 90];

echo(sector(radius, angles));

Simplifying a little - the concat isn't necessary; you can just directly create the list:

function sector(radius, angles, fn = 24)  =
let(
r = radius / cos(180 / fn),
step = -360 / fn,
points = [
[0, 0],
for(a = [angles[0] : step : angles[1] - 360])
[r * cos(a), r * sin(a)],
[r * cos(angles[1]), r * sin(angles[1])]
]
) points;

radius = 10;
angles = [0, 90];

echo(sector(radius, angles));


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

thank you! this is exactly what I was looking for - almost ... This program (at the bottom) Is a bit modified towards the presented solution and creates two number series with coordinates. But, how to combine several series? (this doesn't work)points1 =[[10, 10], [10, -0.0197717], [8.74419, 0.0592372], [7.50818, 0.295018], [6.31148, 0.683852], [5.17294, 1.21961], [4.11053, 1.89383], [3.14099, 2.6959], [2.27963, 3.61316], [1.54003, 4.63114], [0.933839, 5.73379], [0.470631, 6.90372], [0.157706, 8.12248], [0, 9.37085]] points2 =[[10, 50], [-0.0197717, 50], [0.0592372, 51.2558], [0.295018, 52.4918], [0.683852, 53.6885], [1.21961, 54.8271], [1.89383, 55.8895], [2.6959, 56.859], [3.61316, 57.7204], [4.63114, 58.46], [5.73379, 59.0662], [6.90372, 59.5294], [8.12248, 59.8423], [9.37085, 60]] polygon(points1, points2); This instructions works as desire .:points =[[10, -0.0197717], [8.74419, 0.0592372], [7.50818, 0.295018], [6.31148, 0.683852], [5.17294, 1.21961], [4.11053, 1.89383], [3.14099, 2.6959], [2.27963, 3.61316], [1.54003, 4.63114], [0.933839, 5.73379], [0.470631, 6.90372], [0.157706, 8.12248], [0, 9.37085],        [-0.0197717, 50], [0.0592372, 51.2558], [0.295018, 52.4918], [0.683852, 53.6885], [1.21961, 54.8271], [1.89383, 55.8895], [2.6959, 56.859], [3.61316, 57.7204], [4.63114, 58.46], [5.73379, 59.0662], [6.90372, 59.5294], [8.12248, 59.8423], [9.37085, 60]]; polygon(points); My program below fn = 50;radius1 = 10;angles1 = [0, 90];rotate1 = 1;      // 1 medurs, -1 motursstartAngel1 = "bottom";x1 = 10;y1 = 10; // points1 = circSect();points1 = circSect(x1, y1, radius1, angles1, startAngel1, rotate1, fn);polygon(points1); points2 = circSect(10, 50, 10, [0, 90], "left", 1, fn);polygon(points2); function circSect(x=0, y=0, radius=10, angles=[0,270], startRef="bottom", rotate=1, fn=24)  =    let(        startAngel  = (startRef == "left")   ?   0                    : (startRef == "bottom") ?  90                    : (startRef == "right")  ? 180                    : (startRef == "top")    ? 270                    : undef,                r = radius / cos(180 / fn),        step = 360 / fn,        points = [[x, y],            for(a = [angles[0]-startAngel : step : angles[1]-startAngel])                [-rotate * r * cos(a)+x, r * sin(a)+y]]    ) points; Den måndag 28 mars 2022 18:39:37 CEST, Jordan Brown <openscad@jordan.maileater.net> skrev: On 3/27/2022 5:23 PM, nop head wrote: function sector(radius, angles, fn = 24)  =     let(         r = radius / cos(180 / fn),         step = -360 / fn,        points = concat([[0, 0]],             [for(a = [angles[0] : step : angles[1] - 360])                 [r * cos(a), r * sin(a)]             ],             [[r * cos(angles[1]), r * sin(angles[1])]]        )     ) points; radius = 10; angles = [0, 90]; echo(sector(radius, angles)); Simplifying a little - the concat isn't necessary; you can just directly create the list: function sector(radius, angles, fn = 24) = let( r = radius / cos(180 / fn), step = -360 / fn, points = [ [0, 0], for(a = [angles[0] : step : angles[1] - 360]) [r * cos(a), r * sin(a)], [r * cos(angles[1]), r * sin(angles[1])] ] ) points; radius = 10; angles = [0, 90]; echo(sector(radius, angles)); _______________________________________________ OpenSCAD mailing list To unsubscribe send an email to discuss-leave@lists.openscad.org