From what I remember, writing functions in OpenSCAD is awkward, when
you try to write it as you would in any other language.
You have to think different - as silly as that sounds:
radius = 10;
start = 0;
stop =90;
points = test();
echo(points);
polygon(concat([[0, 0]], points));
function sincos(r,a) = [r * cos(a), r * sin(a)];
function test(radius = 10, start=0, stop=90) =
[ for(a = [start:1:stop])
sincos(radius, a)];
sincos is a silly name, pointOnCircle would be more accurate.
test offcourse is also just an example - rewritting it to
radius = 10; //[5:5:20]
start = 0; //[-180:5:180]
stop =90; //[0:5:360]
points = pointsOnCircle(radius=radius, start=start, stop=stop);
polygon(concat([[0, 0]], points));
function pointOnCircle(r,a) = [r * cos(a), r * sin(a)];
function pointsOnCircle(radius = 10, start=0, stop=90) =
[ for(a = [start:1:stop])
pointOnCircle(radius, a)];
That looks much nicer.
I want to give some pointers:
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Introduction
OpenSCAD provides:
*functions*, which return values.
*modules*, which perform actions, but do not return values.
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Functions
function name ( parameters ) = value ;
please note that you can note use { } - coming from any general purpose
language, not having { } is akward.
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/List_Comprehensions#for
Very specifically take note of the following:
"List comprehension for" is not the same thing as "flow control for".
http://openscad.org/cheatsheet/index.html
With Kind regards,
Michael Frey
Jan,
Functions have an '=' followed by an expression and ';' not '{' & '}'.
They are a single (multiline possible) expression unlike some other languages which can have statements.
If you want an assignment equivalent, like your "angles = [start, stop];" use list comprehensions:
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language#List_Comprehensions
Modules have statements inside '{' & '}' separated by ';'.
From: Michael Frey [mailto:michael.frey@gmx.ch]
Sent: Mon, 28 Mar 2022 03:05
To: discuss@lists.openscad.org
Subject: [OpenSCAD] Re: Create - formula (or module) - difficult
From what I remember, writing functions in OpenSCAD is awkward, when you try to write it as you would in any other language.
You have to think different - as silly as that sounds:
radius = 10;
start = 0;
stop =90;
points = test();
echo(points);
polygon(concat([[0, 0]], points));
function sincos(r,a) = [r * cos(a), r * sin(a)];
function test(radius = 10, start=0, stop=90) =
[ for(a = [start:1:stop])
sincos(radius, a)];
sincos is a silly name, pointOnCircle would be more accurate.
test offcourse is also just an example - rewritting it to
radius = 10; //[5:5:20]
start = 0; //[-180:5:180]
stop =90; //[0:5:360]
points = pointsOnCircle(radius=radius, start=start, stop=stop);
polygon(concat([[0, 0]], points));
function pointOnCircle(r,a) = [r * cos(a), r * sin(a)];
function pointsOnCircle(radius = 10, start=0, stop=90) =
[ for(a = [start:1:stop])
pointOnCircle(radius, a)];
That looks much nicer.
I want to give some pointers:
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Introduction
OpenSCAD provides:
functions, which return values.
modules, which perform actions, but do not return values.
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Functions
function name ( parameters ) = value ;
please note that you can note use { } - coming from any general purpose language, not having { } is akward.
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/List_Comprehensions#for
Very specifically take note of the following:
"List comprehension for" is not the same thing as "flow control for".
http://openscad.org/cheatsheet/index.html
With Kind regards,
Michael Frey
--
This email has been checked for viruses by AVG.
https://www.avg.com
Note also that you can use the let statement in functions to do a lot of
calculations.
On Sun, Mar 27, 2022 at 6:13 PM MichaelAtOz oz.at.michael@gmail.com wrote:
Jan,
Functions have an '=' followed by an expression and ';' not '{' & '}'.
They are a single (multiline possible) expression unlike some other
languages which can have statements.
If you want an assignment equivalent, like your "angles = [start, stop];"
use list comprehensions:
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language#List_Comprehensions
Modules have statements inside '{' & '}' separated by ';'.
From: Michael Frey [mailto:michael.frey@gmx.ch]
Sent: Mon, 28 Mar 2022 03:05
To: discuss@lists.openscad.org
Subject: [OpenSCAD] Re: Create - formula (or module) - difficult
From what I remember, writing functions in OpenSCAD is awkward, when you
try to write it as you would in any other language.
You have to think different - as silly as that sounds:
radius = 10;
start = 0;
stop =90;
points = test();
echo(points);
polygon(concat([[0, 0]], points));
function sincos(r,a) = [r * cos(a), r * sin(a)];
function test(radius = 10, start=0, stop=90) =
[ for(a = [start:1:stop])
sincos(radius, a)];
sincos is a silly name, pointOnCircle would be more accurate.
test offcourse is also just an example - rewritting it to
radius = 10; //[5:5:20]
start = 0; //[-180:5:180]
stop =90; //[0:5:360]
points = pointsOnCircle(radius=radius, start=start, stop=stop);
polygon(concat([[0, 0]], points));
function pointOnCircle(r,a) = [r * cos(a), r * sin(a)];
function pointsOnCircle(radius = 10, start=0, stop=90) =
[ for(a = [start:1:stop])
pointOnCircle(radius, a)];
That looks much nicer.
I want to give some pointers:
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Introduction
OpenSCAD provides:
functions, which return values.
modules, which perform actions, but do not return values.
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Functions
function name ( parameters ) = value ;
please note that you can note use { } - coming from any general purpose
language, not having { } is akward.
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/List_Comprehensions#for
Very specifically take note of the following:
"List comprehension for" is not the same thing as "flow control for".
http://openscad.org/cheatsheet/index.html
With Kind regards,
Michael Frey
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_6153464501484684987_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org