discuss@lists.openscad.org

OpenSCAD general discussion

View all threads

not adding overlapping (or too near) points?

BB
Bruno Boettcher
Tue, Sep 7, 2021 1:41 PM

Hello!

stumbling on the "its not a programming language" hurdle again....
i compute sections, atm a simple trigonometric function, but i want to add
them only if the distance to the last one is big enough,
to avoid the overlapping error....
i got this in perl:

@result = [];
$lastval = -1000000;#some stupid number to start

for(my $w=$sw; $w <$ew; $w += $ew/$fn)·
{
$acty = $z-$off+$rsin($w);
if($lastval ==-1000000 || abs($acty) > abs($lastval)+$prec)·
{
$lastval = $acty;
push(@result,[$b-$r
sin(0+$w), $h,$acty]);
}
}

that works, and i can generate my polygon data into a file, and then
include that file in openscad, but this lacks elegancy.... is there a way
to achieve this purely in openscad?
i started to write it in openscad, but was clueless how to store the last
pushed value?

--
ciao
Bruno

---==========
http://nohkumado.eu/, http://bboett.free.frhttp://aikido.nohkumado.eu/,
http://bboett.free.fr
http://aikido.zorn.free.fr

Hello! stumbling on the "its not a programming language" hurdle again.... i compute sections, atm a simple trigonometric function, but i want to add them only if the distance to the last one is big enough, to avoid the overlapping error.... i got this in perl: @result = []; $lastval = -1000000;#some stupid number to start for(my $w=$sw; $w <$ew; $w += $ew/$fn)· { $acty = $z-$off+$r*sin($w); if($lastval ==-1000000 || abs($acty) > abs($lastval)+$prec)· { $lastval = $acty; push(@result,[$b-$r*sin(0+$w), $h,$acty]); } } that works, and i can generate my polygon data into a file, and then include that file in openscad, but this lacks elegancy.... is there a way to achieve this purely in openscad? i started to write it in openscad, but was clueless how to store the last pushed value? -- ciao Bruno =========================================== http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, <http://bboett.free.fr> http://aikido.zorn.free.fr
FH
Father Horton
Tue, Sep 7, 2021 3:13 PM

I think a better way to do this is to calculate the smallest angle that
gives an acceptable size:

angle = acos((2r^2-prec^2)/(4r)) (from the law of cosines with a and b both
equal to r).

And then use that as the step in your loop.

Otherwise, what you do by iteration in a procedural language is done in a
functional language by recursive functions or by list comprehension. See
the section on List Comprehension in the manual.

On Tue, Sep 7, 2021 at 8:41 AM Bruno Boettcher bboett@gmail.com wrote:

Hello!

stumbling on the "its not a programming language" hurdle again....
i compute sections, atm a simple trigonometric function, but i want to add
them only if the distance to the last one is big enough,
to avoid the overlapping error....
i got this in perl:

@result = [];
$lastval = -1000000;#some stupid number to start

for(my $w=$sw; $w <$ew; $w += $ew/$fn)·
{
$acty = $z-$off+$rsin($w);
if($lastval ==-1000000 || abs($acty) > abs($lastval)+$prec)·
{
$lastval = $acty;
push(@result,[$b-$r
sin(0+$w), $h,$acty]);
}
}

that works, and i can generate my polygon data into a file, and then
include that file in openscad, but this lacks elegancy.... is there a way
to achieve this purely in openscad?
i started to write it in openscad, but was clueless how to store the last
pushed value?

--
ciao
Bruno

---==========
http://nohkumado.eu/, http://bboett.free.frhttp://aikido.nohkumado.eu/,
http://bboett.free.fr
http://aikido.zorn.free.fr


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

I think a better way to do this is to calculate the smallest angle that gives an acceptable size: angle = acos((2r^2-prec^2)/(4r)) (from the law of cosines with a and b both equal to r). And then use that as the step in your loop. Otherwise, what you do by iteration in a procedural language is done in a functional language by recursive functions or by list comprehension. See the section on List Comprehension in the manual. On Tue, Sep 7, 2021 at 8:41 AM Bruno Boettcher <bboett@gmail.com> wrote: > Hello! > > stumbling on the "its not a programming language" hurdle again.... > i compute sections, atm a simple trigonometric function, but i want to add > them only if the distance to the last one is big enough, > to avoid the overlapping error.... > i got this in perl: > > @result = []; > $lastval = -1000000;#some stupid number to start > > for(my $w=$sw; $w <$ew; $w += $ew/$fn)· > { > $acty = $z-$off+$r*sin($w); > if($lastval ==-1000000 || abs($acty) > abs($lastval)+$prec)· > { > $lastval = $acty; > push(@result,[$b-$r*sin(0+$w), $h,$acty]); > } > } > > that works, and i can generate my polygon data into a file, and then > include that file in openscad, but this lacks elegancy.... is there a way > to achieve this purely in openscad? > i started to write it in openscad, but was clueless how to store the last > pushed value? > > -- > ciao > Bruno > > =========================================== > http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, > <http://bboett.free.fr> > http://aikido.zorn.free.fr > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
AM
Adrian Mariano
Tue, Sep 7, 2021 3:53 PM

So you are doing some computation in a loop and you want to add values only
when they exceed some threshold gap space?  The way to do this is to use
recursion.  Here's an example where I have used very simple test case of
f(i) =i and just requiring the gap bigger than 4, so the result just counts
by 5.  There are obviously better ways to count by 5, but if f() is some
complex function then you need to do something like this.

function f(i) = i;

function make_list(list=[f(0)], i=1) = i>100 ? list : make_list([each list,
if (f(i)>4+list[len(list)-1]) f(i)], i+1);

echo(make_list());

So make_list starts off with list set to a single element list.  It checks
if it's done, and if so it returns the list.  If it's not done then it
assembles a new list which includes the new value only if the length
condition holds.  It then recurses with the new list and with the
incremented value of  i.  If you don't want to compute f twice you can put
in "let(fi=f(i))".

On Tue, Sep 7, 2021 at 9:41 AM Bruno Boettcher bboett@gmail.com wrote:

Hello!

stumbling on the "its not a programming language" hurdle again....
i compute sections, atm a simple trigonometric function, but i want to add
them only if the distance to the last one is big enough,
to avoid the overlapping error....
i got this in perl:

@result = [];
$lastval = -1000000;#some stupid number to start

for(my $w=$sw; $w <$ew; $w += $ew/$fn)·
{
$acty = $z-$off+$rsin($w);
if($lastval ==-1000000 || abs($acty) > abs($lastval)+$prec)·
{
$lastval = $acty;
push(@result,[$b-$r
sin(0+$w), $h,$acty]);
}
}

that works, and i can generate my polygon data into a file, and then
include that file in openscad, but this lacks elegancy.... is there a way
to achieve this purely in openscad?
i started to write it in openscad, but was clueless how to store the last
pushed value?

--
ciao
Bruno

---==========
http://nohkumado.eu/, http://bboett.free.frhttp://aikido.nohkumado.eu/,
http://bboett.free.fr
http://aikido.zorn.free.fr


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

So you are doing some computation in a loop and you want to add values only when they exceed some threshold gap space? The way to do this is to use recursion. Here's an example where I have used very simple test case of f(i) =i and just requiring the gap bigger than 4, so the result just counts by 5. There are obviously better ways to count by 5, but if f() is some complex function then you need to do something like this. function f(i) = i; function make_list(list=[f(0)], i=1) = i>100 ? list : make_list([each list, if (f(i)>4+list[len(list)-1]) f(i)], i+1); echo(make_list()); So make_list starts off with list set to a single element list. It checks if it's done, and if so it returns the list. If it's not done then it assembles a new list which includes the new value only if the length condition holds. It then recurses with the new list and with the incremented value of i. If you don't want to compute f twice you can put in "let(fi=f(i))". On Tue, Sep 7, 2021 at 9:41 AM Bruno Boettcher <bboett@gmail.com> wrote: > Hello! > > stumbling on the "its not a programming language" hurdle again.... > i compute sections, atm a simple trigonometric function, but i want to add > them only if the distance to the last one is big enough, > to avoid the overlapping error.... > i got this in perl: > > @result = []; > $lastval = -1000000;#some stupid number to start > > for(my $w=$sw; $w <$ew; $w += $ew/$fn)· > { > $acty = $z-$off+$r*sin($w); > if($lastval ==-1000000 || abs($acty) > abs($lastval)+$prec)· > { > $lastval = $acty; > push(@result,[$b-$r*sin(0+$w), $h,$acty]); > } > } > > that works, and i can generate my polygon data into a file, and then > include that file in openscad, but this lacks elegancy.... is there a way > to achieve this purely in openscad? > i started to write it in openscad, but was clueless how to store the last > pushed value? > > -- > ciao > Bruno > > =========================================== > http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, > <http://bboett.free.fr> > http://aikido.zorn.free.fr > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
BB
Bruno Boettcher
Thu, Sep 9, 2021 12:52 PM

Hello!
thanks for the pointers and the example!

still trying , for the moment, with a simple circle function, to see how to
handle arrays and arguments, and indeed, i stumble now on the arguments...:
function f(w,r) =  [rsin(w), rcos(w)];

function make_list(rad=5,list=[f(0,r=rad)], i=1)= i>5 ?·
list :·
make_list(rad=rad,[each list, let(fi = f(i,rad)) if (abs(abs(fi[1])
-abs(list[len(list)-1][1])) >.01) fi], i+1);
echo("make list ",make_list(rad = 10));

i get WARNING: Ignoring unknown variable 'rad' , so seems the variable is
instanciated only after the declaration of the function....

so i tried to have a starting value:
starta = f(0,r=10);
echo("make list ",make_list(rad = 10, list=starta));

but then the error gets:

WARNING: abs() parameter could not be converted: argument 0: expected
number, found undefined <59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad>

<59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad>

so still not working :(

following the example with i, the rad value should now have a value in the
function body??

what do i do wrong?

ciao

Bruno

Am Di., 7. Sept. 2021 um 17:54 Uhr schrieb Adrian Mariano <avm4@cornell.edu

:

So you are doing some computation in a loop and you want to add values
only when they exceed some threshold gap space?  The way to do this is to
use recursion.  Here's an example where I have used very simple test case
of f(i) =i and just requiring the gap bigger than 4, so the result just
counts by 5.  There are obviously better ways to count by 5, but if f() is
some complex function then you need to do something like this.

function f(i) = i;

function make_list(list=[f(0)], i=1) = i>100 ? list : make_list([each
list, if (f(i)>4+list[len(list)-1]) f(i)], i+1);

echo(make_list());

So make_list starts off with list set to a single element list.  It checks
if it's done, and if so it returns the list.  If it's not done then it
assembles a new list which includes the new value only if the length
condition holds.  It then recurses with the new list and with the
incremented value of  i.  If you don't want to compute f twice you can put
in "let(fi=f(i))".

On Tue, Sep 7, 2021 at 9:41 AM Bruno Boettcher bboett@gmail.com wrote:

Hello!

stumbling on the "its not a programming language" hurdle again....
i compute sections, atm a simple trigonometric function, but i want to
add them only if the distance to the last one is big enough,
to avoid the overlapping error....
i got this in perl:

@result = [];
$lastval = -1000000;#some stupid number to start

for(my $w=$sw; $w <$ew; $w += $ew/$fn)·
{
$acty = $z-$off+$rsin($w);
if($lastval ==-1000000 || abs($acty) > abs($lastval)+$prec)·
{
$lastval = $acty;
push(@result,[$b-$r
sin(0+$w), $h,$acty]);
}
}

that works, and i can generate my polygon data into a file, and then
include that file in openscad, but this lacks elegancy.... is there a way
to achieve this purely in openscad?
i started to write it in openscad, but was clueless how to store the last
pushed value?

--
ciao
Bruno

---==========
http://nohkumado.eu/, http://bboett.free.frhttp://aikido.nohkumado.eu/,
http://bboett.free.fr
http://aikido.zorn.free.fr


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

Hello! thanks for the pointers and the example! still trying , for the moment, with a simple circle function, to see how to handle arrays and arguments, and indeed, i stumble now on the arguments...: function f(w,r) = [r*sin(w), r*cos(w)]; function make_list(rad=5,list=[f(0,r=rad)], i=1)= i>5 ?· list :· make_list(rad=rad,[each list, let(fi = f(i,rad)) if (abs(abs(fi[1]) -abs(list[len(list)-1][1])) >.01) fi], i+1); echo("make list ",make_list(rad = 10)); i get WARNING: Ignoring unknown variable 'rad' , so seems the variable is instanciated only after the declaration of the function.... so i tried to have a starting value: starta = f(0,r=10); echo("make list ",make_list(rad = 10, list=starta)); but then the error gets: WARNING: abs() parameter could not be converted: argument 0: expected number, found undefined <59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad> <59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad> so still not working :( following the example with i, the rad value should now have a value in the function body?? what do i do wrong? ciao Bruno Am Di., 7. Sept. 2021 um 17:54 Uhr schrieb Adrian Mariano <avm4@cornell.edu >: > So you are doing some computation in a loop and you want to add values > only when they exceed some threshold gap space? The way to do this is to > use recursion. Here's an example where I have used very simple test case > of f(i) =i and just requiring the gap bigger than 4, so the result just > counts by 5. There are obviously better ways to count by 5, but if f() is > some complex function then you need to do something like this. > > function f(i) = i; > > function make_list(list=[f(0)], i=1) = i>100 ? list : make_list([each > list, if (f(i)>4+list[len(list)-1]) f(i)], i+1); > > echo(make_list()); > > So make_list starts off with list set to a single element list. It checks > if it's done, and if so it returns the list. If it's not done then it > assembles a new list which includes the new value only if the length > condition holds. It then recurses with the new list and with the > incremented value of i. If you don't want to compute f twice you can put > in "let(fi=f(i))". > > On Tue, Sep 7, 2021 at 9:41 AM Bruno Boettcher <bboett@gmail.com> wrote: > >> Hello! >> >> stumbling on the "its not a programming language" hurdle again.... >> i compute sections, atm a simple trigonometric function, but i want to >> add them only if the distance to the last one is big enough, >> to avoid the overlapping error.... >> i got this in perl: >> >> @result = []; >> $lastval = -1000000;#some stupid number to start >> >> for(my $w=$sw; $w <$ew; $w += $ew/$fn)· >> { >> $acty = $z-$off+$r*sin($w); >> if($lastval ==-1000000 || abs($acty) > abs($lastval)+$prec)· >> { >> $lastval = $acty; >> push(@result,[$b-$r*sin(0+$w), $h,$acty]); >> } >> } >> >> that works, and i can generate my polygon data into a file, and then >> include that file in openscad, but this lacks elegancy.... is there a way >> to achieve this purely in openscad? >> i started to write it in openscad, but was clueless how to store the last >> pushed value? >> >> -- >> ciao >> Bruno >> >> =========================================== >> http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, >> <http://bboett.free.fr> >> http://aikido.zorn.free.fr >> _______________________________________________ >> 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 > -- ciao Bruno =========================================== http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, <http://bboett.free.fr> http://aikido.zorn.free.fr
LM
Leonard Martin Struttmann
Thu, Sep 9, 2021 3:31 PM

Does this do what you want?

//----------------------------
function f(w,r) =  [rsin(w), rcos(w)];

function make_list( rad=5, i=1, list ) =
let( fi=f(i,rad) )
i>5? list :

abs(abs(fi[1]) - abs(list[len(list)-1][1])) >.01?
make_list( rad, i+1, [ each list, fi ] ):
make_list( rad, i+1, list );

echo("make_list", make_list( 10, 1, [f(0,10)] ));

On Thu, Sep 9, 2021 at 7:53 AM Bruno Boettcher bboett@gmail.com wrote:

Hello!
thanks for the pointers and the example!

still trying , for the moment, with a simple circle function, to see how
to handle arrays and arguments, and indeed, i stumble now on the
arguments...:
function f(w,r) =  [rsin(w), rcos(w)];

function make_list(rad=5,list=[f(0,r=rad)], i=1)= i>5 ?·
list :·
make_list(rad=rad,[each list, let(fi = f(i,rad)) if (abs(abs(fi[1])
-abs(list[len(list)-1][1])) >.01) fi], i+1);
echo("make list ",make_list(rad = 10));

i get WARNING: Ignoring unknown variable 'rad' , so seems the variable is
instanciated only after the declaration of the function....

so i tried to have a starting value:
starta = f(0,r=10);
echo("make list ",make_list(rad = 10, list=starta));

but then the error gets:

WARNING: abs() parameter could not be converted: argument 0: expected
number, found undefined
http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad

http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad

so still not working :(

following the example with i, the rad value should now have a value in the
function body??

what do i do wrong?

ciao

Bruno

Am Di., 7. Sept. 2021 um 17:54 Uhr schrieb Adrian Mariano <
avm4@cornell.edu>:

So you are doing some computation in a loop and you want to add values
only when they exceed some threshold gap space?  The way to do this is to
use recursion.  Here's an example where I have used very simple test case
of f(i) =i and just requiring the gap bigger than 4, so the result just
counts by 5.  There are obviously better ways to count by 5, but if f() is
some complex function then you need to do something like this.

function f(i) = i;

function make_list(list=[f(0)], i=1) = i>100 ? list : make_list([each
list, if (f(i)>4+list[len(list)-1]) f(i)], i+1);

echo(make_list());

So make_list starts off with list set to a single element list.  It
checks if it's done, and if so it returns the list.  If it's not done then
it assembles a new list which includes the new value only if the length
condition holds.  It then recurses with the new list and with the
incremented value of  i.  If you don't want to compute f twice you can put
in "let(fi=f(i))".

On Tue, Sep 7, 2021 at 9:41 AM Bruno Boettcher bboett@gmail.com wrote:

Hello!

stumbling on the "its not a programming language" hurdle again....
i compute sections, atm a simple trigonometric function, but i want to
add them only if the distance to the last one is big enough,
to avoid the overlapping error....
i got this in perl:

@result = [];
$lastval = -1000000;#some stupid number to start

for(my $w=$sw; $w <$ew; $w += $ew/$fn)·
{
$acty = $z-$off+$rsin($w);
if($lastval ==-1000000 || abs($acty) > abs($lastval)+$prec)·
{
$lastval = $acty;
push(@result,[$b-$r
sin(0+$w), $h,$acty]);
}
}

that works, and i can generate my polygon data into a file, and then
include that file in openscad, but this lacks elegancy.... is there a way
to achieve this purely in openscad?
i started to write it in openscad, but was clueless how to store the
last pushed value?

--
ciao
Bruno

---==========
http://nohkumado.eu/, http://bboett.free.frhttp://aikido.nohkumado.eu/,
http://bboett.free.fr
http://aikido.zorn.free.fr


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

Does this do what you want? //---------------------------- function f(w,r) = [r*sin(w), r*cos(w)]; function make_list( rad=5, i=1, list ) = let( fi=f(i,rad) ) i>5? list : abs(abs(fi[1]) - abs(list[len(list)-1][1])) >.01? make_list( rad, i+1, [ each list, fi ] ): make_list( rad, i+1, list ); echo("make_list", make_list( 10, 1, [f(0,10)] )); On Thu, Sep 9, 2021 at 7:53 AM Bruno Boettcher <bboett@gmail.com> wrote: > Hello! > thanks for the pointers and the example! > > still trying , for the moment, with a simple circle function, to see how > to handle arrays and arguments, and indeed, i stumble now on the > arguments...: > function f(w,r) = [r*sin(w), r*cos(w)]; > > function make_list(rad=5,list=[f(0,r=rad)], i=1)= i>5 ?· > list :· > make_list(rad=rad,[each list, let(fi = f(i,rad)) if (abs(abs(fi[1]) > -abs(list[len(list)-1][1])) >.01) fi], i+1); > echo("make list ",make_list(rad = 10)); > > i get WARNING: Ignoring unknown variable 'rad' , so seems the variable is > instanciated only after the declaration of the function.... > > so i tried to have a starting value: > starta = f(0,r=10); > echo("make list ",make_list(rad = 10, list=starta)); > > but then the error gets: > > WARNING: abs() parameter could not be converted: argument 0: expected > number, found undefined > <http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad> > > > <http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad> > > so still not working :( > > > following the example with i, the rad value should now have a value in the > function body?? > > > what do i do wrong? > > > ciao > > Bruno > > > > Am Di., 7. Sept. 2021 um 17:54 Uhr schrieb Adrian Mariano < > avm4@cornell.edu>: > >> So you are doing some computation in a loop and you want to add values >> only when they exceed some threshold gap space? The way to do this is to >> use recursion. Here's an example where I have used very simple test case >> of f(i) =i and just requiring the gap bigger than 4, so the result just >> counts by 5. There are obviously better ways to count by 5, but if f() is >> some complex function then you need to do something like this. >> >> function f(i) = i; >> >> function make_list(list=[f(0)], i=1) = i>100 ? list : make_list([each >> list, if (f(i)>4+list[len(list)-1]) f(i)], i+1); >> >> echo(make_list()); >> >> So make_list starts off with list set to a single element list. It >> checks if it's done, and if so it returns the list. If it's not done then >> it assembles a new list which includes the new value only if the length >> condition holds. It then recurses with the new list and with the >> incremented value of i. If you don't want to compute f twice you can put >> in "let(fi=f(i))". >> >> On Tue, Sep 7, 2021 at 9:41 AM Bruno Boettcher <bboett@gmail.com> wrote: >> >>> Hello! >>> >>> stumbling on the "its not a programming language" hurdle again.... >>> i compute sections, atm a simple trigonometric function, but i want to >>> add them only if the distance to the last one is big enough, >>> to avoid the overlapping error.... >>> i got this in perl: >>> >>> @result = []; >>> $lastval = -1000000;#some stupid number to start >>> >>> for(my $w=$sw; $w <$ew; $w += $ew/$fn)· >>> { >>> $acty = $z-$off+$r*sin($w); >>> if($lastval ==-1000000 || abs($acty) > abs($lastval)+$prec)· >>> { >>> $lastval = $acty; >>> push(@result,[$b-$r*sin(0+$w), $h,$acty]); >>> } >>> } >>> >>> that works, and i can generate my polygon data into a file, and then >>> include that file in openscad, but this lacks elegancy.... is there a way >>> to achieve this purely in openscad? >>> i started to write it in openscad, but was clueless how to store the >>> last pushed value? >>> >>> -- >>> ciao >>> Bruno >>> >>> =========================================== >>> http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, >>> <http://bboett.free.fr> >>> http://aikido.zorn.free.fr >>> _______________________________________________ >>> 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 >> > > > -- > ciao > Bruno > > =========================================== > http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, > <http://bboett.free.fr> > http://aikido.zorn.free.fr > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
BB
Bruno Boettcher
Thu, Sep 9, 2021 4:16 PM

indeed... but why???
its only another way to
write it down, isn't it??

Am Do., 9. Sept. 2021 um 17:32 Uhr schrieb Leonard Martin Struttmann <
lenstruttmann@gmail.com>:

Does this do what you want?

//----------------------------
function f(w,r) =  [rsin(w), rcos(w)];

function make_list( rad=5, i=1, list ) =
let( fi=f(i,rad) )
i>5? list :

abs(abs(fi[1]) - abs(list[len(list)-1][1])) >.01?
make_list( rad, i+1, [ each list, fi ] ):
make_list( rad, i+1, list );

echo("make_list", make_list( 10, 1, [f(0,10)] ));

On Thu, Sep 9, 2021 at 7:53 AM Bruno Boettcher bboett@gmail.com wrote:

Hello!
thanks for the pointers and the example!

still trying , for the moment, with a simple circle function, to see how
to handle arrays and arguments, and indeed, i stumble now on the
arguments...:
function f(w,r) =  [rsin(w), rcos(w)];

function make_list(rad=5,list=[f(0,r=rad)], i=1)= i>5 ?·
list :·
make_list(rad=rad,[each list, let(fi = f(i,rad)) if (abs(abs(fi[1])
-abs(list[len(list)-1][1])) >.01) fi], i+1);
echo("make list ",make_list(rad = 10));

i get WARNING: Ignoring unknown variable 'rad' , so seems the variable is
instanciated only after the declaration of the function....

so i tried to have a starting value:
starta = f(0,r=10);
echo("make list ",make_list(rad = 10, list=starta));

but then the error gets:

WARNING: abs() parameter could not be converted: argument 0: expected
number, found undefined
http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad

http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad

so still not working :(

following the example with i, the rad value should now have a value in
the function body??

what do i do wrong?

ciao

Bruno

Am Di., 7. Sept. 2021 um 17:54 Uhr schrieb Adrian Mariano <
avm4@cornell.edu>:

So you are doing some computation in a loop and you want to add values
only when they exceed some threshold gap space?  The way to do this is to
use recursion.  Here's an example where I have used very simple test case
of f(i) =i and just requiring the gap bigger than 4, so the result just
counts by 5.  There are obviously better ways to count by 5, but if f() is
some complex function then you need to do something like this.

function f(i) = i;

function make_list(list=[f(0)], i=1) = i>100 ? list : make_list([each
list, if (f(i)>4+list[len(list)-1]) f(i)], i+1);

echo(make_list());

So make_list starts off with list set to a single element list.  It
checks if it's done, and if so it returns the list.  If it's not done then
it assembles a new list which includes the new value only if the length
condition holds.  It then recurses with the new list and with the
incremented value of  i.  If you don't want to compute f twice you can put
in "let(fi=f(i))".

On Tue, Sep 7, 2021 at 9:41 AM Bruno Boettcher bboett@gmail.com wrote:

Hello!

stumbling on the "its not a programming language" hurdle again....
i compute sections, atm a simple trigonometric function, but i want to
add them only if the distance to the last one is big enough,
to avoid the overlapping error....
i got this in perl:

@result = [];
$lastval = -1000000;#some stupid number to start

for(my $w=$sw; $w <$ew; $w += $ew/$fn)·
{
$acty = $z-$off+$rsin($w);
if($lastval ==-1000000 || abs($acty) > abs($lastval)+$prec)·
{
$lastval = $acty;
push(@result,[$b-$r
sin(0+$w), $h,$acty]);
}
}

that works, and i can generate my polygon data into a file, and then
include that file in openscad, but this lacks elegancy.... is there a way
to achieve this purely in openscad?
i started to write it in openscad, but was clueless how to store the
last pushed value?

--
ciao
Bruno

---==========
http://nohkumado.eu/, http://bboett.free.frhttp://aikido.nohkumado.eu/,
http://bboett.free.fr
http://aikido.zorn.free.fr


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


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

indeed... but why??? its only another way to write it down, isn't it?? Am Do., 9. Sept. 2021 um 17:32 Uhr schrieb Leonard Martin Struttmann < lenstruttmann@gmail.com>: > Does this do what you want? > > //---------------------------- > function f(w,r) = [r*sin(w), r*cos(w)]; > > function make_list( rad=5, i=1, list ) = > let( fi=f(i,rad) ) > i>5? list : > > abs(abs(fi[1]) - abs(list[len(list)-1][1])) >.01? > make_list( rad, i+1, [ each list, fi ] ): > make_list( rad, i+1, list ); > > echo("make_list", make_list( 10, 1, [f(0,10)] )); > > > On Thu, Sep 9, 2021 at 7:53 AM Bruno Boettcher <bboett@gmail.com> wrote: > >> Hello! >> thanks for the pointers and the example! >> >> still trying , for the moment, with a simple circle function, to see how >> to handle arrays and arguments, and indeed, i stumble now on the >> arguments...: >> function f(w,r) = [r*sin(w), r*cos(w)]; >> >> function make_list(rad=5,list=[f(0,r=rad)], i=1)= i>5 ?· >> list :· >> make_list(rad=rad,[each list, let(fi = f(i,rad)) if (abs(abs(fi[1]) >> -abs(list[len(list)-1][1])) >.01) fi], i+1); >> echo("make list ",make_list(rad = 10)); >> >> i get WARNING: Ignoring unknown variable 'rad' , so seems the variable is >> instanciated only after the declaration of the function.... >> >> so i tried to have a starting value: >> starta = f(0,r=10); >> echo("make list ",make_list(rad = 10, list=starta)); >> >> but then the error gets: >> >> WARNING: abs() parameter could not be converted: argument 0: expected >> number, found undefined >> <http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad> >> >> >> <http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad> >> >> so still not working :( >> >> >> following the example with i, the rad value should now have a value in >> the function body?? >> >> >> what do i do wrong? >> >> >> ciao >> >> Bruno >> >> >> >> Am Di., 7. Sept. 2021 um 17:54 Uhr schrieb Adrian Mariano < >> avm4@cornell.edu>: >> >>> So you are doing some computation in a loop and you want to add values >>> only when they exceed some threshold gap space? The way to do this is to >>> use recursion. Here's an example where I have used very simple test case >>> of f(i) =i and just requiring the gap bigger than 4, so the result just >>> counts by 5. There are obviously better ways to count by 5, but if f() is >>> some complex function then you need to do something like this. >>> >>> function f(i) = i; >>> >>> function make_list(list=[f(0)], i=1) = i>100 ? list : make_list([each >>> list, if (f(i)>4+list[len(list)-1]) f(i)], i+1); >>> >>> echo(make_list()); >>> >>> So make_list starts off with list set to a single element list. It >>> checks if it's done, and if so it returns the list. If it's not done then >>> it assembles a new list which includes the new value only if the length >>> condition holds. It then recurses with the new list and with the >>> incremented value of i. If you don't want to compute f twice you can put >>> in "let(fi=f(i))". >>> >>> On Tue, Sep 7, 2021 at 9:41 AM Bruno Boettcher <bboett@gmail.com> wrote: >>> >>>> Hello! >>>> >>>> stumbling on the "its not a programming language" hurdle again.... >>>> i compute sections, atm a simple trigonometric function, but i want to >>>> add them only if the distance to the last one is big enough, >>>> to avoid the overlapping error.... >>>> i got this in perl: >>>> >>>> @result = []; >>>> $lastval = -1000000;#some stupid number to start >>>> >>>> for(my $w=$sw; $w <$ew; $w += $ew/$fn)· >>>> { >>>> $acty = $z-$off+$r*sin($w); >>>> if($lastval ==-1000000 || abs($acty) > abs($lastval)+$prec)· >>>> { >>>> $lastval = $acty; >>>> push(@result,[$b-$r*sin(0+$w), $h,$acty]); >>>> } >>>> } >>>> >>>> that works, and i can generate my polygon data into a file, and then >>>> include that file in openscad, but this lacks elegancy.... is there a way >>>> to achieve this purely in openscad? >>>> i started to write it in openscad, but was clueless how to store the >>>> last pushed value? >>>> >>>> -- >>>> ciao >>>> Bruno >>>> >>>> =========================================== >>>> http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, >>>> <http://bboett.free.fr> >>>> http://aikido.zorn.free.fr >>>> _______________________________________________ >>>> 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 >>> >> >> >> -- >> ciao >> Bruno >> >> =========================================== >> http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, >> <http://bboett.free.fr> >> http://aikido.zorn.free.fr >> _______________________________________________ >> 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 > -- ciao Bruno =========================================== http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, <http://bboett.free.fr> http://aikido.zorn.free.fr
LM
Leonard Martin Struttmann
Thu, Sep 9, 2021 4:29 PM

Actually, I was unsure of how your version worked, so I started from
scratch with a simple recursive function, then added your bit in.

I think that this error:

echo("make list ",make_list(rad = 10, list=starta));

but then the error gets:

WARNING: abs() parameter could not be converted: argument 0: expected
number, found undefined
http://59%2C/home/bboett/3dmodel/tesla_cad/tesla_fob.scad

...was because "list=starta" only created a single point, not a list that
contained a single point. Try using "list=[starta]".

http://59%2C/home/bboett/3dmodel/tesla_cad/tesla_fob.scad

On Thu, Sep 9, 2021 at 11:17 AM Bruno Boettcher bboett@gmail.com wrote:

indeed... but why???
its only another way to
write it down, isn't it??

Am Do., 9. Sept. 2021 um 17:32 Uhr schrieb Leonard Martin Struttmann <
lenstruttmann@gmail.com>:

Does this do what you want?

//----------------------------
function f(w,r) =  [rsin(w), rcos(w)];

function make_list( rad=5, i=1, list ) =
let( fi=f(i,rad) )
i>5? list :

abs(abs(fi[1]) - abs(list[len(list)-1][1])) >.01?
make_list( rad, i+1, [ each list, fi ] ):
make_list( rad, i+1, list );

echo("make_list", make_list( 10, 1, [f(0,10)] ));

On Thu, Sep 9, 2021 at 7:53 AM Bruno Boettcher bboett@gmail.com wrote:

Hello!
thanks for the pointers and the example!

still trying , for the moment, with a simple circle function, to see how
to handle arrays and arguments, and indeed, i stumble now on the
arguments...:
function f(w,r) =  [rsin(w), rcos(w)];

function make_list(rad=5,list=[f(0,r=rad)], i=1)= i>5 ?·
list :·
make_list(rad=rad,[each list, let(fi = f(i,rad)) if (abs(abs(fi[1])
-abs(list[len(list)-1][1])) >.01) fi], i+1);
echo("make list ",make_list(rad = 10));

i get WARNING: Ignoring unknown variable 'rad' , so seems the variable
is instanciated only after the declaration of the function....

so i tried to have a starting value:
starta = f(0,r=10);
echo("make list ",make_list(rad = 10, list=starta));

but then the error gets:

WARNING: abs() parameter could not be converted: argument 0: expected
number, found undefined
http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad

http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad

so still not working :(

following the example with i, the rad value should now have a value in
the function body??

what do i do wrong?

ciao

Bruno

Am Di., 7. Sept. 2021 um 17:54 Uhr schrieb Adrian Mariano <
avm4@cornell.edu>:

So you are doing some computation in a loop and you want to add values
only when they exceed some threshold gap space?  The way to do this is to
use recursion.  Here's an example where I have used very simple test case
of f(i) =i and just requiring the gap bigger than 4, so the result just
counts by 5.  There are obviously better ways to count by 5, but if f() is
some complex function then you need to do something like this.

function f(i) = i;

function make_list(list=[f(0)], i=1) = i>100 ? list : make_list([each
list, if (f(i)>4+list[len(list)-1]) f(i)], i+1);

echo(make_list());

So make_list starts off with list set to a single element list.  It
checks if it's done, and if so it returns the list.  If it's not done then
it assembles a new list which includes the new value only if the length
condition holds.  It then recurses with the new list and with the
incremented value of  i.  If you don't want to compute f twice you can put
in "let(fi=f(i))".

On Tue, Sep 7, 2021 at 9:41 AM Bruno Boettcher bboett@gmail.com
wrote:

Hello!

stumbling on the "its not a programming language" hurdle again....
i compute sections, atm a simple trigonometric function, but i want to
add them only if the distance to the last one is big enough,
to avoid the overlapping error....
i got this in perl:

@result = [];
$lastval = -1000000;#some stupid number to start

for(my $w=$sw; $w <$ew; $w += $ew/$fn)·
{
$acty = $z-$off+$rsin($w);
if($lastval ==-1000000 || abs($acty) > abs($lastval)+$prec)·
{
$lastval = $acty;
push(@result,[$b-$r
sin(0+$w), $h,$acty]);
}
}

that works, and i can generate my polygon data into a file, and then
include that file in openscad, but this lacks elegancy.... is there a way
to achieve this purely in openscad?
i started to write it in openscad, but was clueless how to store the
last pushed value?

--
ciao
Bruno

---==========
http://nohkumado.eu/, http://bboett.free.frhttp://aikido.nohkumado.eu/,
http://bboett.free.fr
http://aikido.zorn.free.fr


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


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

Actually, I was unsure of how your version worked, so I started from scratch with a simple recursive function, then added your bit in. I think that this error: echo("make list ",make_list(rad = 10, list=starta)); but then the error gets: WARNING: abs() parameter could not be converted: argument 0: expected number, found undefined <http://59%2C/home/bboett/3dmodel/tesla_cad/tesla_fob.scad> ...was because "list=starta" only created a single point, not a list that contained a single point. Try using "list=[starta]". <http://59%2C/home/bboett/3dmodel/tesla_cad/tesla_fob.scad> On Thu, Sep 9, 2021 at 11:17 AM Bruno Boettcher <bboett@gmail.com> wrote: > indeed... but why??? > its only another way to > write it down, isn't it?? > > Am Do., 9. Sept. 2021 um 17:32 Uhr schrieb Leonard Martin Struttmann < > lenstruttmann@gmail.com>: > >> Does this do what you want? >> >> //---------------------------- >> function f(w,r) = [r*sin(w), r*cos(w)]; >> >> function make_list( rad=5, i=1, list ) = >> let( fi=f(i,rad) ) >> i>5? list : >> >> abs(abs(fi[1]) - abs(list[len(list)-1][1])) >.01? >> make_list( rad, i+1, [ each list, fi ] ): >> make_list( rad, i+1, list ); >> >> echo("make_list", make_list( 10, 1, [f(0,10)] )); >> >> >> On Thu, Sep 9, 2021 at 7:53 AM Bruno Boettcher <bboett@gmail.com> wrote: >> >>> Hello! >>> thanks for the pointers and the example! >>> >>> still trying , for the moment, with a simple circle function, to see how >>> to handle arrays and arguments, and indeed, i stumble now on the >>> arguments...: >>> function f(w,r) = [r*sin(w), r*cos(w)]; >>> >>> function make_list(rad=5,list=[f(0,r=rad)], i=1)= i>5 ?· >>> list :· >>> make_list(rad=rad,[each list, let(fi = f(i,rad)) if (abs(abs(fi[1]) >>> -abs(list[len(list)-1][1])) >.01) fi], i+1); >>> echo("make list ",make_list(rad = 10)); >>> >>> i get WARNING: Ignoring unknown variable 'rad' , so seems the variable >>> is instanciated only after the declaration of the function.... >>> >>> so i tried to have a starting value: >>> starta = f(0,r=10); >>> echo("make list ",make_list(rad = 10, list=starta)); >>> >>> but then the error gets: >>> >>> WARNING: abs() parameter could not be converted: argument 0: expected >>> number, found undefined >>> <http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad> >>> >>> >>> <http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad> >>> >>> so still not working :( >>> >>> >>> following the example with i, the rad value should now have a value in >>> the function body?? >>> >>> >>> what do i do wrong? >>> >>> >>> ciao >>> >>> Bruno >>> >>> >>> >>> Am Di., 7. Sept. 2021 um 17:54 Uhr schrieb Adrian Mariano < >>> avm4@cornell.edu>: >>> >>>> So you are doing some computation in a loop and you want to add values >>>> only when they exceed some threshold gap space? The way to do this is to >>>> use recursion. Here's an example where I have used very simple test case >>>> of f(i) =i and just requiring the gap bigger than 4, so the result just >>>> counts by 5. There are obviously better ways to count by 5, but if f() is >>>> some complex function then you need to do something like this. >>>> >>>> function f(i) = i; >>>> >>>> function make_list(list=[f(0)], i=1) = i>100 ? list : make_list([each >>>> list, if (f(i)>4+list[len(list)-1]) f(i)], i+1); >>>> >>>> echo(make_list()); >>>> >>>> So make_list starts off with list set to a single element list. It >>>> checks if it's done, and if so it returns the list. If it's not done then >>>> it assembles a new list which includes the new value only if the length >>>> condition holds. It then recurses with the new list and with the >>>> incremented value of i. If you don't want to compute f twice you can put >>>> in "let(fi=f(i))". >>>> >>>> On Tue, Sep 7, 2021 at 9:41 AM Bruno Boettcher <bboett@gmail.com> >>>> wrote: >>>> >>>>> Hello! >>>>> >>>>> stumbling on the "its not a programming language" hurdle again.... >>>>> i compute sections, atm a simple trigonometric function, but i want to >>>>> add them only if the distance to the last one is big enough, >>>>> to avoid the overlapping error.... >>>>> i got this in perl: >>>>> >>>>> @result = []; >>>>> $lastval = -1000000;#some stupid number to start >>>>> >>>>> for(my $w=$sw; $w <$ew; $w += $ew/$fn)· >>>>> { >>>>> $acty = $z-$off+$r*sin($w); >>>>> if($lastval ==-1000000 || abs($acty) > abs($lastval)+$prec)· >>>>> { >>>>> $lastval = $acty; >>>>> push(@result,[$b-$r*sin(0+$w), $h,$acty]); >>>>> } >>>>> } >>>>> >>>>> that works, and i can generate my polygon data into a file, and then >>>>> include that file in openscad, but this lacks elegancy.... is there a way >>>>> to achieve this purely in openscad? >>>>> i started to write it in openscad, but was clueless how to store the >>>>> last pushed value? >>>>> >>>>> -- >>>>> ciao >>>>> Bruno >>>>> >>>>> =========================================== >>>>> http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, >>>>> <http://bboett.free.fr> >>>>> http://aikido.zorn.free.fr >>>>> _______________________________________________ >>>>> 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 >>>> >>> >>> >>> -- >>> ciao >>> Bruno >>> >>> =========================================== >>> http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, >>> <http://bboett.free.fr> >>> http://aikido.zorn.free.fr >>> _______________________________________________ >>> 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 >> > > > -- > ciao > Bruno > > =========================================== > http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, > <http://bboett.free.fr> > http://aikido.zorn.free.fr > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
RP
Ronaldo Persiano
Thu, Sep 9, 2021 4:56 PM

In your first trial the argument list is initialized with:

list=[f(0,r=rad)]

but rad is undefined at that point. In cases like that, I suggest
something like:

function make_list(rad=5,list, i=1) =
let( list = is_undef(list) ? [f(0,r=rad)] : list)
i>5 ?·list :·
make_list(rad=rad,[each list, let(fi = f(i,rad)) if (abs(abs(fi[1])
-abs(list[len(list)-1][1])) >.01) fi], i+1);

Your last trial has a subtil mistake: it explicitly sets rad but doesn't
name the other arguments. OpenSCAD in those cases has weird behavior, IMO.
To be safe, either name all arguments or none. So, the following has no
error:

function make_list(rad=5,list, i=1) =
i>5 ? list :
make_list(rad,
[ each list,
let( fi=f(i,rad) )
if(abs(abs(fi[1])-abs(list[len(list)-1][1])) > .01) fi
],
i+1);

Em qui., 9 de set. de 2021 às 17:17, Bruno Boettcher bboett@gmail.com
escreveu:

indeed... but why???
its only another way to
write it down, isn't it??

Am Do., 9. Sept. 2021 um 17:32 Uhr schrieb Leonard Martin Struttmann <
lenstruttmann@gmail.com>:

Does this do what you want?

//----------------------------
function f(w,r) =  [rsin(w), rcos(w)];

function make_list( rad=5, i=1, list ) =
let( fi=f(i,rad) )
i>5? list :

abs(abs(fi[1]) - abs(list[len(list)-1][1])) >.01?
make_list( rad, i+1, [ each list, fi ] ):
make_list( rad, i+1, list );

echo("make_list", make_list( 10, 1, [f(0,10)] ));

On Thu, Sep 9, 2021 at 7:53 AM Bruno Boettcher bboett@gmail.com wrote:

Hello!
thanks for the pointers and the example!

still trying , for the moment, with a simple circle function, to see how
to handle arrays and arguments, and indeed, i stumble now on the
arguments...:
function f(w,r) =  [rsin(w), rcos(w)];

function make_list(rad=5,list=[f(0,r=rad)], i=1)= i>5 ?·
list :·
make_list(rad=rad,[each list, let(fi = f(i,rad)) if (abs(abs(fi[1])
-abs(list[len(list)-1][1])) >.01) fi], i+1);
echo("make list ",make_list(rad = 10));

i get WARNING: Ignoring unknown variable 'rad' , so seems the variable
is instanciated only after the declaration of the function....

so i tried to have a starting value:
starta = f(0,r=10);
echo("make list ",make_list(rad = 10, list=starta));

but then the error gets:

WARNING: abs() parameter could not be converted: argument 0: expected
number, found undefined
http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad

http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad

so still not working :(

following the example with i, the rad value should now have a value in
the function body??

what do i do wrong?

ciao

Bruno

Am Di., 7. Sept. 2021 um 17:54 Uhr schrieb Adrian Mariano <
avm4@cornell.edu>:

So you are doing some computation in a loop and you want to add values
only when they exceed some threshold gap space?  The way to do this is to
use recursion.  Here's an example where I have used very simple test case
of f(i) =i and just requiring the gap bigger than 4, so the result just
counts by 5.  There are obviously better ways to count by 5, but if f() is
some complex function then you need to do something like this.

function f(i) = i;

function make_list(list=[f(0)], i=1) = i>100 ? list : make_list([each
list, if (f(i)>4+list[len(list)-1]) f(i)], i+1);

echo(make_list());

So make_list starts off with list set to a single element list.  It
checks if it's done, and if so it returns the list.  If it's not done then
it assembles a new list which includes the new value only if the length
condition holds.  It then recurses with the new list and with the
incremented value of  i.  If you don't want to compute f twice you can put
in "let(fi=f(i))".

On Tue, Sep 7, 2021 at 9:41 AM Bruno Boettcher bboett@gmail.com
wrote:

Hello!

stumbling on the "its not a programming language" hurdle again....
i compute sections, atm a simple trigonometric function, but i want to
add them only if the distance to the last one is big enough,
to avoid the overlapping error....
i got this in perl:

@result = [];
$lastval = -1000000;#some stupid number to start

for(my $w=$sw; $w <$ew; $w += $ew/$fn)·
{
$acty = $z-$off+$rsin($w);
if($lastval ==-1000000 || abs($acty) > abs($lastval)+$prec)·
{
$lastval = $acty;
push(@result,[$b-$r
sin(0+$w), $h,$acty]);
}
}

that works, and i can generate my polygon data into a file, and then
include that file in openscad, but this lacks elegancy.... is there a way
to achieve this purely in openscad?
i started to write it in openscad, but was clueless how to store the
last pushed value?

--
ciao
Bruno

---==========
http://nohkumado.eu/, http://bboett.free.frhttp://aikido.nohkumado.eu/,
http://bboett.free.fr
http://aikido.zorn.free.fr


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


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

In your first trial the argument `list` is initialized with: list=[f(0,r=rad)] but `rad` is undefined at that point. In cases like that, I suggest something like: function make_list(rad=5,list, i=1) = let( list = is_undef(list) ? [f(0,r=rad)] : list) i>5 ?·list :· make_list(rad=rad,[each list, let(fi = f(i,rad)) if (abs(abs(fi[1]) -abs(list[len(list)-1][1])) >.01) fi], i+1); Your last trial has a subtil mistake: it explicitly sets `rad` but doesn't name the other arguments. OpenSCAD in those cases has weird behavior, IMO. To be safe, either name all arguments or none. So, the following has no error: function make_list(rad=5,list, i=1) = i>5 ? list : make_list(rad, [ each list, let( fi=f(i,rad) ) if(abs(abs(fi[1])-abs(list[len(list)-1][1])) > .01) fi ], i+1); Em qui., 9 de set. de 2021 às 17:17, Bruno Boettcher <bboett@gmail.com> escreveu: > indeed... but why??? > its only another way to > write it down, isn't it?? > > Am Do., 9. Sept. 2021 um 17:32 Uhr schrieb Leonard Martin Struttmann < > lenstruttmann@gmail.com>: > >> Does this do what you want? >> >> //---------------------------- >> function f(w,r) = [r*sin(w), r*cos(w)]; >> >> function make_list( rad=5, i=1, list ) = >> let( fi=f(i,rad) ) >> i>5? list : >> >> abs(abs(fi[1]) - abs(list[len(list)-1][1])) >.01? >> make_list( rad, i+1, [ each list, fi ] ): >> make_list( rad, i+1, list ); >> >> echo("make_list", make_list( 10, 1, [f(0,10)] )); >> >> >> On Thu, Sep 9, 2021 at 7:53 AM Bruno Boettcher <bboett@gmail.com> wrote: >> >>> Hello! >>> thanks for the pointers and the example! >>> >>> still trying , for the moment, with a simple circle function, to see how >>> to handle arrays and arguments, and indeed, i stumble now on the >>> arguments...: >>> function f(w,r) = [r*sin(w), r*cos(w)]; >>> >>> function make_list(rad=5,list=[f(0,r=rad)], i=1)= i>5 ?· >>> list :· >>> make_list(rad=rad,[each list, let(fi = f(i,rad)) if (abs(abs(fi[1]) >>> -abs(list[len(list)-1][1])) >.01) fi], i+1); >>> echo("make list ",make_list(rad = 10)); >>> >>> i get WARNING: Ignoring unknown variable 'rad' , so seems the variable >>> is instanciated only after the declaration of the function.... >>> >>> so i tried to have a starting value: >>> starta = f(0,r=10); >>> echo("make list ",make_list(rad = 10, list=starta)); >>> >>> but then the error gets: >>> >>> WARNING: abs() parameter could not be converted: argument 0: expected >>> number, found undefined >>> <http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad> >>> >>> >>> <http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad> >>> >>> so still not working :( >>> >>> >>> following the example with i, the rad value should now have a value in >>> the function body?? >>> >>> >>> what do i do wrong? >>> >>> >>> ciao >>> >>> Bruno >>> >>> >>> >>> Am Di., 7. Sept. 2021 um 17:54 Uhr schrieb Adrian Mariano < >>> avm4@cornell.edu>: >>> >>>> So you are doing some computation in a loop and you want to add values >>>> only when they exceed some threshold gap space? The way to do this is to >>>> use recursion. Here's an example where I have used very simple test case >>>> of f(i) =i and just requiring the gap bigger than 4, so the result just >>>> counts by 5. There are obviously better ways to count by 5, but if f() is >>>> some complex function then you need to do something like this. >>>> >>>> function f(i) = i; >>>> >>>> function make_list(list=[f(0)], i=1) = i>100 ? list : make_list([each >>>> list, if (f(i)>4+list[len(list)-1]) f(i)], i+1); >>>> >>>> echo(make_list()); >>>> >>>> So make_list starts off with list set to a single element list. It >>>> checks if it's done, and if so it returns the list. If it's not done then >>>> it assembles a new list which includes the new value only if the length >>>> condition holds. It then recurses with the new list and with the >>>> incremented value of i. If you don't want to compute f twice you can put >>>> in "let(fi=f(i))". >>>> >>>> On Tue, Sep 7, 2021 at 9:41 AM Bruno Boettcher <bboett@gmail.com> >>>> wrote: >>>> >>>>> Hello! >>>>> >>>>> stumbling on the "its not a programming language" hurdle again.... >>>>> i compute sections, atm a simple trigonometric function, but i want to >>>>> add them only if the distance to the last one is big enough, >>>>> to avoid the overlapping error.... >>>>> i got this in perl: >>>>> >>>>> @result = []; >>>>> $lastval = -1000000;#some stupid number to start >>>>> >>>>> for(my $w=$sw; $w <$ew; $w += $ew/$fn)· >>>>> { >>>>> $acty = $z-$off+$r*sin($w); >>>>> if($lastval ==-1000000 || abs($acty) > abs($lastval)+$prec)· >>>>> { >>>>> $lastval = $acty; >>>>> push(@result,[$b-$r*sin(0+$w), $h,$acty]); >>>>> } >>>>> } >>>>> >>>>> that works, and i can generate my polygon data into a file, and then >>>>> include that file in openscad, but this lacks elegancy.... is there a way >>>>> to achieve this purely in openscad? >>>>> i started to write it in openscad, but was clueless how to store the >>>>> last pushed value? >>>>> >>>>> -- >>>>> ciao >>>>> Bruno >>>>> >>>>> =========================================== >>>>> http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, >>>>> <http://bboett.free.fr> >>>>> http://aikido.zorn.free.fr >>>>> _______________________________________________ >>>>> 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 >>>> >>> >>> >>> -- >>> ciao >>> Bruno >>> >>> =========================================== >>> http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, >>> <http://bboett.free.fr> >>> http://aikido.zorn.free.fr >>> _______________________________________________ >>> 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 >> > > > -- > ciao > Bruno > > =========================================== > http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, > <http://bboett.free.fr> > http://aikido.zorn.free.fr > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
BB
Bruno Boettcher
Thu, Sep 9, 2021 5:14 PM

ah! thanks all!
yes .... should have checked the proper types..
you are absolutely right Leonard!
and thanks Ronaldo, the check is elegant!
i think i will go with named arguments in future, seems safer :D

Am Do., 9. Sept. 2021 um 18:57 Uhr schrieb Ronaldo Persiano <
rcmpersiano@gmail.com>:

In your first trial the argument list is initialized with:

list=[f(0,r=rad)]

but rad is undefined at that point. In cases like that, I suggest
something like:

function make_list(rad=5,list, i=1) =
let( list = is_undef(list) ? [f(0,r=rad)] : list)
i>5 ?·list :·
make_list(rad=rad,[each list, let(fi = f(i,rad)) if (abs(abs(fi[1])
-abs(list[len(list)-1][1])) >.01) fi], i+1);

Your last trial has a subtil mistake: it explicitly sets rad but doesn't
name the other arguments. OpenSCAD in those cases has weird behavior, IMO.
To be safe, either name all arguments or none. So, the following has no
error:

function make_list(rad=5,list, i=1) =
i>5 ? list :
make_list(rad,
[ each list,
let( fi=f(i,rad) )
if(abs(abs(fi[1])-abs(list[len(list)-1][1])) > .01) fi
],
i+1);

Em qui., 9 de set. de 2021 às 17:17, Bruno Boettcher bboett@gmail.com
escreveu:

indeed... but why???
its only another way to
write it down, isn't it??

Am Do., 9. Sept. 2021 um 17:32 Uhr schrieb Leonard Martin Struttmann <
lenstruttmann@gmail.com>:

Does this do what you want?

//----------------------------
function f(w,r) =  [rsin(w), rcos(w)];

function make_list( rad=5, i=1, list ) =
let( fi=f(i,rad) )
i>5? list :

abs(abs(fi[1]) - abs(list[len(list)-1][1])) >.01?
make_list( rad, i+1, [ each list, fi ] ):
make_list( rad, i+1, list );

echo("make_list", make_list( 10, 1, [f(0,10)] ));

On Thu, Sep 9, 2021 at 7:53 AM Bruno Boettcher bboett@gmail.com wrote:

Hello!
thanks for the pointers and the example!

still trying , for the moment, with a simple circle function, to see
how to handle arrays and arguments, and indeed, i stumble now on the
arguments...:
function f(w,r) =  [rsin(w), rcos(w)];

function make_list(rad=5,list=[f(0,r=rad)], i=1)= i>5 ?·
list :·
make_list(rad=rad,[each list, let(fi = f(i,rad)) if (abs(abs(fi[1])
-abs(list[len(list)-1][1])) >.01) fi], i+1);
echo("make list ",make_list(rad = 10));

i get WARNING: Ignoring unknown variable 'rad' , so seems the variable
is instanciated only after the declaration of the function....

so i tried to have a starting value:
starta = f(0,r=10);
echo("make list ",make_list(rad = 10, list=starta));

but then the error gets:

WARNING: abs() parameter could not be converted: argument 0: expected
number, found undefined
http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad

http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad

so still not working :(

following the example with i, the rad value should now have a value in
the function body??

what do i do wrong?

ciao

Bruno

Am Di., 7. Sept. 2021 um 17:54 Uhr schrieb Adrian Mariano <
avm4@cornell.edu>:

So you are doing some computation in a loop and you want to add values
only when they exceed some threshold gap space?  The way to do this is to
use recursion.  Here's an example where I have used very simple test case
of f(i) =i and just requiring the gap bigger than 4, so the result just
counts by 5.  There are obviously better ways to count by 5, but if f() is
some complex function then you need to do something like this.

function f(i) = i;

function make_list(list=[f(0)], i=1) = i>100 ? list : make_list([each
list, if (f(i)>4+list[len(list)-1]) f(i)], i+1);

echo(make_list());

So make_list starts off with list set to a single element list.  It
checks if it's done, and if so it returns the list.  If it's not done then
it assembles a new list which includes the new value only if the length
condition holds.  It then recurses with the new list and with the
incremented value of  i.  If you don't want to compute f twice you can put
in "let(fi=f(i))".

On Tue, Sep 7, 2021 at 9:41 AM Bruno Boettcher bboett@gmail.com
wrote:

Hello!

stumbling on the "its not a programming language" hurdle again....
i compute sections, atm a simple trigonometric function, but i want
to add them only if the distance to the last one is big enough,
to avoid the overlapping error....
i got this in perl:

@result = [];
$lastval = -1000000;#some stupid number to start

for(my $w=$sw; $w <$ew; $w += $ew/$fn)·
{
$acty = $z-$off+$rsin($w);
if($lastval ==-1000000 || abs($acty) > abs($lastval)+$prec)·
{
$lastval = $acty;
push(@result,[$b-$r
sin(0+$w), $h,$acty]);
}
}

that works, and i can generate my polygon data into a file, and then
include that file in openscad, but this lacks elegancy.... is there a way
to achieve this purely in openscad?
i started to write it in openscad, but was clueless how to store the
last pushed value?

--
ciao
Bruno

---==========
http://nohkumado.eu/, http://bboett.free.frhttp://aikido.nohkumado.eu/,
http://bboett.free.fr
http://aikido.zorn.free.fr


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


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

ah! thanks all! yes .... should have checked the proper types.. you are absolutely right Leonard! and thanks Ronaldo, the check is elegant! i think i will go with named arguments in future, seems safer :D Am Do., 9. Sept. 2021 um 18:57 Uhr schrieb Ronaldo Persiano < rcmpersiano@gmail.com>: > In your first trial the argument `list` is initialized with: > > list=[f(0,r=rad)] > > but `rad` is undefined at that point. In cases like that, I suggest > something like: > > function make_list(rad=5,list, i=1) = > let( list = is_undef(list) ? [f(0,r=rad)] : list) > i>5 ?·list :· > make_list(rad=rad,[each list, let(fi = f(i,rad)) if (abs(abs(fi[1]) > -abs(list[len(list)-1][1])) >.01) fi], i+1); > > > Your last trial has a subtil mistake: it explicitly sets `rad` but doesn't > name the other arguments. OpenSCAD in those cases has weird behavior, IMO. > To be safe, either name all arguments or none. So, the following has no > error: > > function make_list(rad=5,list, i=1) = > i>5 ? list : > make_list(rad, > [ each list, > let( fi=f(i,rad) ) > if(abs(abs(fi[1])-abs(list[len(list)-1][1])) > .01) fi > ], > i+1); > > > > > > Em qui., 9 de set. de 2021 às 17:17, Bruno Boettcher <bboett@gmail.com> > escreveu: > >> indeed... but why??? >> its only another way to >> write it down, isn't it?? >> >> Am Do., 9. Sept. 2021 um 17:32 Uhr schrieb Leonard Martin Struttmann < >> lenstruttmann@gmail.com>: >> >>> Does this do what you want? >>> >>> //---------------------------- >>> function f(w,r) = [r*sin(w), r*cos(w)]; >>> >>> function make_list( rad=5, i=1, list ) = >>> let( fi=f(i,rad) ) >>> i>5? list : >>> >>> abs(abs(fi[1]) - abs(list[len(list)-1][1])) >.01? >>> make_list( rad, i+1, [ each list, fi ] ): >>> make_list( rad, i+1, list ); >>> >>> echo("make_list", make_list( 10, 1, [f(0,10)] )); >>> >>> >>> On Thu, Sep 9, 2021 at 7:53 AM Bruno Boettcher <bboett@gmail.com> wrote: >>> >>>> Hello! >>>> thanks for the pointers and the example! >>>> >>>> still trying , for the moment, with a simple circle function, to see >>>> how to handle arrays and arguments, and indeed, i stumble now on the >>>> arguments...: >>>> function f(w,r) = [r*sin(w), r*cos(w)]; >>>> >>>> function make_list(rad=5,list=[f(0,r=rad)], i=1)= i>5 ?· >>>> list :· >>>> make_list(rad=rad,[each list, let(fi = f(i,rad)) if (abs(abs(fi[1]) >>>> -abs(list[len(list)-1][1])) >.01) fi], i+1); >>>> echo("make list ",make_list(rad = 10)); >>>> >>>> i get WARNING: Ignoring unknown variable 'rad' , so seems the variable >>>> is instanciated only after the declaration of the function.... >>>> >>>> so i tried to have a starting value: >>>> starta = f(0,r=10); >>>> echo("make list ",make_list(rad = 10, list=starta)); >>>> >>>> but then the error gets: >>>> >>>> WARNING: abs() parameter could not be converted: argument 0: expected >>>> number, found undefined >>>> <http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad> >>>> >>>> >>>> <http://59,/home/bboett/3dmodel/tesla_cad/tesla_fob.scad> >>>> >>>> so still not working :( >>>> >>>> >>>> following the example with i, the rad value should now have a value in >>>> the function body?? >>>> >>>> >>>> what do i do wrong? >>>> >>>> >>>> ciao >>>> >>>> Bruno >>>> >>>> >>>> >>>> Am Di., 7. Sept. 2021 um 17:54 Uhr schrieb Adrian Mariano < >>>> avm4@cornell.edu>: >>>> >>>>> So you are doing some computation in a loop and you want to add values >>>>> only when they exceed some threshold gap space? The way to do this is to >>>>> use recursion. Here's an example where I have used very simple test case >>>>> of f(i) =i and just requiring the gap bigger than 4, so the result just >>>>> counts by 5. There are obviously better ways to count by 5, but if f() is >>>>> some complex function then you need to do something like this. >>>>> >>>>> function f(i) = i; >>>>> >>>>> function make_list(list=[f(0)], i=1) = i>100 ? list : make_list([each >>>>> list, if (f(i)>4+list[len(list)-1]) f(i)], i+1); >>>>> >>>>> echo(make_list()); >>>>> >>>>> So make_list starts off with list set to a single element list. It >>>>> checks if it's done, and if so it returns the list. If it's not done then >>>>> it assembles a new list which includes the new value only if the length >>>>> condition holds. It then recurses with the new list and with the >>>>> incremented value of i. If you don't want to compute f twice you can put >>>>> in "let(fi=f(i))". >>>>> >>>>> On Tue, Sep 7, 2021 at 9:41 AM Bruno Boettcher <bboett@gmail.com> >>>>> wrote: >>>>> >>>>>> Hello! >>>>>> >>>>>> stumbling on the "its not a programming language" hurdle again.... >>>>>> i compute sections, atm a simple trigonometric function, but i want >>>>>> to add them only if the distance to the last one is big enough, >>>>>> to avoid the overlapping error.... >>>>>> i got this in perl: >>>>>> >>>>>> @result = []; >>>>>> $lastval = -1000000;#some stupid number to start >>>>>> >>>>>> for(my $w=$sw; $w <$ew; $w += $ew/$fn)· >>>>>> { >>>>>> $acty = $z-$off+$r*sin($w); >>>>>> if($lastval ==-1000000 || abs($acty) > abs($lastval)+$prec)· >>>>>> { >>>>>> $lastval = $acty; >>>>>> push(@result,[$b-$r*sin(0+$w), $h,$acty]); >>>>>> } >>>>>> } >>>>>> >>>>>> that works, and i can generate my polygon data into a file, and then >>>>>> include that file in openscad, but this lacks elegancy.... is there a way >>>>>> to achieve this purely in openscad? >>>>>> i started to write it in openscad, but was clueless how to store the >>>>>> last pushed value? >>>>>> >>>>>> -- >>>>>> ciao >>>>>> Bruno >>>>>> >>>>>> =========================================== >>>>>> http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, >>>>>> <http://bboett.free.fr> >>>>>> http://aikido.zorn.free.fr >>>>>> _______________________________________________ >>>>>> 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 >>>>> >>>> >>>> >>>> -- >>>> ciao >>>> Bruno >>>> >>>> =========================================== >>>> http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, >>>> <http://bboett.free.fr> >>>> http://aikido.zorn.free.fr >>>> _______________________________________________ >>>> 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 >>> >> >> >> -- >> ciao >> Bruno >> >> =========================================== >> http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, >> <http://bboett.free.fr> >> http://aikido.zorn.free.fr >> _______________________________________________ >> 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 > -- ciao Bruno =========================================== http://nohkumado.eu/, <http://bboett.free.fr>http://aikido.nohkumado.eu/, <http://bboett.free.fr> http://aikido.zorn.free.fr