incrementing a variable and other confusions for a procedural coder

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

incrementing a variable and other confusions for a procedural coder

khackbarth

I’ve been using OpenSCAD for about two years and I just realized that I don’t know how to something simple like increment a variable or pass a value from one part of my code to another.

 

Case in point.  I have a vector of vectors: x = [[3,5],[1,6],[6,1],[8,4]]  I’d like to count the number of times that the first element of each sub vector exceeds 2.

 

I think I can iterate through x with a for loop and ask whether x[i][0] > 1, but I don’t know what to do with the result of that comparison. k=0 then k=k+1 isn’t supported in the language.  And even if it was, k is undefined outside of the loop.

 

A function may be helpful, but it looks like a function is just a line of code that has been set aside for modularity.  Search can iterate across a vector but any comparison has to be equality, nothing as fancy as “greater than”.

 

Ken


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: incrementing a variable and other confusions for a procedural coder

nophead
A recursive function  can do what you want.

function count_more_than(v, n = 2, i = 0, count = 0) = i >= len(v) ? count : count_more_than(v, n, i + 1, count = v[i][0] > n ? count + 1 : count);

x = [[3,5],[1,6],[6,1],[8,4]];

echo(count_more_than(x));




On Fri, 27 Nov 2020 at 22:54, <[hidden email]> wrote:

I’ve been using OpenSCAD for about two years and I just realized that I don’t know how to something simple like increment a variable or pass a value from one part of my code to another.

 

Case in point.  I have a vector of vectors: x = [[3,5],[1,6],[6,1],[8,4]]  I’d like to count the number of times that the first element of each sub vector exceeds 2.

 

I think I can iterate through x with a for loop and ask whether x[i][0] > 1, but I don’t know what to do with the result of that comparison. k=0 then k=k+1 isn’t supported in the language.  And even if it was, k is undefined outside of the loop.

 

A function may be helpful, but it looks like a function is just a line of code that has been set aside for modularity.  Search can iterate across a vector but any comparison has to be equality, nothing as fancy as “greater than”.

 

Ken

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: incrementing a variable and other confusions for a procedural coder

RevarBat
Don’t even need to go recursive for this. 
len([for (x=v) if(x[0]>n) 1])

On Nov 27, 2020, at 3:05 PM, nop head <[hidden email]> wrote:


A recursive function  can do what you want.

function count_more_than(v, n = 2, i = 0, count = 0) = i >= len(v) ? count : count_more_than(v, n, i + 1, count = v[i][0] > n ? count + 1 : count);

x = [[3,5],[1,6],[6,1],[8,4]];

echo(count_more_than(x));




On Fri, 27 Nov 2020 at 22:54, <[hidden email]> wrote:

I’ve been using OpenSCAD for about two years and I just realized that I don’t know how to something simple like increment a variable or pass a value from one part of my code to another.

 

Case in point.  I have a vector of vectors: x = [[3,5],[1,6],[6,1],[8,4]]  I’d like to count the number of times that the first element of each sub vector exceeds 2.

 

I think I can iterate through x with a for loop and ask whether x[i][0] > 1, but I don’t know what to do with the result of that comparison. k=0 then k=k+1 isn’t supported in the language.  And even if it was, k is undefined outside of the loop.

 

A function may be helpful, but it looks like a function is just a line of code that has been set aside for modularity.  Search can iterate across a vector but any comparison has to be equality, nothing as fancy as “greater than”.

 

Ken

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: incrementing a variable and other confusions for a procedural coder

skypuppy

Revar, you must be a C programmer!


On 11/27/20 5:39 PM, Revar Desmera wrote:
Don’t even need to go recursive for this. 
len([for (x=v) if(x[0]>n) 1])

On Nov 27, 2020, at 3:05 PM, nop head [hidden email] wrote:


A recursive function  can do what you want.

function count_more_than(v, n = 2, i = 0, count = 0) = i >= len(v) ? count : count_more_than(v, n, i + 1, count = v[i][0] > n ? count + 1 : count);

x = [[3,5],[1,6],[6,1],[8,4]];

echo(count_more_than(x));




On Fri, 27 Nov 2020 at 22:54, <[hidden email]> wrote:

I’ve been using OpenSCAD for about two years and I just realized that I don’t know how to something simple like increment a variable or pass a value from one part of my code to another.

 

Case in point.  I have a vector of vectors: x = [[3,5],[1,6],[6,1],[8,4]]  I’d like to count the number of times that the first element of each sub vector exceeds 2.

 

I think I can iterate through x with a for loop and ask whether x[i][0] > 1, but I don’t know what to do with the result of that comparison. k=0 then k=k+1 isn’t supported in the language.  And even if it was, k is undefined outside of the loop.

 

A function may be helpful, but it looks like a function is just a line of code that has been set aside for modularity.  Search can iterate across a vector but any comparison has to be equality, nothing as fancy as “greater than”.

 

Ken

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: incrementing a variable and other confusions for a procedural coder

RevarBat
Yes, C and twenty or thirty other languages. Plus a few domain specific languages I wrote. 
 
-Revar


On Nov 27, 2020, at 5:01 PM, David <[hidden email]> wrote:



Revar, you must be a C programmer!


On 11/27/20 5:39 PM, Revar Desmera wrote:
Don’t even need to go recursive for this. 
len([for (x=v) if(x[0]>n) 1])

On Nov 27, 2020, at 3:05 PM, nop head [hidden email] wrote:


A recursive function  can do what you want.

function count_more_than(v, n = 2, i = 0, count = 0) = i >= len(v) ? count : count_more_than(v, n, i + 1, count = v[i][0] > n ? count + 1 : count);

x = [[3,5],[1,6],[6,1],[8,4]];

echo(count_more_than(x));




On Fri, 27 Nov 2020 at 22:54, <[hidden email]> wrote:

I’ve been using OpenSCAD for about two years and I just realized that I don’t know how to something simple like increment a variable or pass a value from one part of my code to another.

 

Case in point.  I have a vector of vectors: x = [[3,5],[1,6],[6,1],[8,4]]  I’d like to count the number of times that the first element of each sub vector exceeds 2.

 

I think I can iterate through x with a for loop and ask whether x[i][0] > 1, but I don’t know what to do with the result of that comparison. k=0 then k=k+1 isn’t supported in the language.  And even if it was, k is undefined outside of the loop.

 

A function may be helpful, but it looks like a function is just a line of code that has been set aside for modularity.  Search can iterate across a vector but any comparison has to be equality, nothing as fancy as “greater than”.

 

Ken

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

What am I doing wrong?

skypuppy
In reply to this post by skypuppy
Trying to make a mount for a 3D printer sensor.  Constructing all the
basic stuff from large basic blocks.

What's wrong with there I am?  The subtraction is one doing what I would
like it to do.  Something having to do with the faces is my guess but
actually, I'm clueless.  :(

The last construction I thought would be a cutaway, leaving the vertical
arm (I think) of the BL Touch mount for a Creality CR-10 S5 printer.  I
could include the STL if the non-parameterized version of need be
if/when I can find one, probably on thingiverse.

This is from scratch with nothing streamlined and no efficiency attempts
in the code yet.  Just straight hunt-and-peck so far.

Thanks,

David



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: What am I doing wrong?

skypuppy
Found something very similar to what I'm trying to do.  File attached
this time.  :)

David


On 12/4/20 11:52 PM, David wrote:

> Trying to make a mount for a 3D printer sensor.  Constructing all the
> basic stuff from large basic blocks.
>
> What's wrong with there I am?  The subtraction is one doing what I
> would like it to do.  Something having to do with the faces is my
> guess but actually, I'm clueless.  :(
>
> The last construction I thought would be a cutaway, leaving the
> vertical arm (I think) of the BL Touch mount for a Creality CR-10 S5
> printer.  I could include the STL if the non-parameterized version of
> need be if/when I can find one, probably on thingiverse.
>
> This is from scratch with nothing streamlined and no efficiency
> attempts in the code yet.  Just straight hunt-and-peck so far.
>
> Thanks,
>
> David
>
>
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

BlTouch+Mount+For+Ender+3+(remix)+-+no+supports+needed.zip (852K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: What am I doing wrong?

lar3ry
In reply to this post by skypuppy
Hard to help without seeing your code.

On 4 Dec 2020 at 23:52, David wrote:

> Trying to make a mount for a 3D printer sensor.  Constructing all the
> basic stuff from large basic blocks.
>
> What's wrong with there I am?  The subtraction is one doing what I would
> like it to do.  Something having to do with the faces is my guess but
> actually, I'm clueless.  :(
>
> The last construction I thought would be a cutaway, leaving the vertical
> arm (I think) of the BL Touch mount for a Creality CR-10 S5 printer.  I
> could include the STL if the non-parameterized version of need be
> if/when I can find one, probably on thingiverse.
>
> This is from scratch with nothing streamlined and no efficiency attempts
> in the code yet.  Just straight hunt-and-peck so far.


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: What am I doing wrong?

skypuppy
Thought I had.  Sorry.  Source is a tad overdone.  <sigh> F6 shows it
sorta correctly now.  Still some slots and mount hole for the sensor to
"punch out."  That vertical part did not display correctly until just
today.


// Attempt at a BL Touch (BLT) mounting bracket that fits my CR-10 S5.
// Hope to do it all in variables.  :)

mbd = 4;    // mounting bolt diameters
bltbd = 3;   // mounting blt diameters
mbt = 4;    // mounting base thickness
armt = 6;   // arm thickness
arml = 30;  // total arm length
armoffseth = 21.76;   // from bottom of bracket.  too low!
mountfw = 34.67;     // mount full width
armoffsetl = 18;       // arm offset left
mountfh = 37;      // mount full height
mounttbhc = 4.19;     // mount top bolt hole center, from right
mounttbhbr = 4.4;    // mount top bolt hole center, from bottom
mountbbhcr = 22;    // mount bottom bolt hole center, from right
mountbbbcb = 32;    // mount bottom bolt hole center, from bottom
armbhacr = 23.38;    // arm bolt holes (all) center, from right
// all arm bolt holes measured from base, and numbered from base
armbh1offset = 8.54;   // arm bolt hole 1 center, from base
armbh2offset = 19;     // arm bolt hole 2 center, from base
armbh3offset = 27.66;   // arm bolt hole 3 center, from base
armtop = 9;             // distanct from top of mount to top of arm

// full size base, arm, and total volume

difference () {
     cube ([mountfh, mountfw, arml]);   //full volume
     cube ([28, armoffsetl, arml]);          // subtract lower right block
     translate([31.4, mounttbhc, 0])         // set location for bolt hole 1
         cylinder(h=mountfh, d=bltbd);           // subtract bolt hole 1
     translate([13, 21.85, 0])                   // set location for
bolt hole 2
         cylinder(h=mountfh, d=bltbd);           // subtract bolt hole 2
     translate([0, armoffsetl, mbd])         // set location to remove
bottom left chunk
        cube([22, 16.7, arml]);                  // subtract that chunk

    translate([mountfh - armtop, 0, mbd])    // set location to remove
top chunk
       // cube([mountfh - armtop, mountfw, mountfh]);      // remove top
chunk
         cube([mountfh - armtop, mountfw, mountfh]);
}
     //    translate([28.25, mountfw, mbd]);             // set location
to remove top chunk
     //    cube([9, mountfw, mountfh]);
//translate ([40, 40, 0])
//    cylinder(h=40, d=15);

On 12/5/20 12:10 AM, [hidden email] wrote:

> Hard to help without seeing your code.
>
> On 4 Dec 2020 at 23:52, David wrote:
>
>> Trying to make a mount for a 3D printer sensor.  Constructing all the
>> basic stuff from large basic blocks.
>>
>> What's wrong with there I am?  The subtraction is one doing what I would
>> like it to do.  Something having to do with the faces is my guess but
>> actually, I'm clueless.  :(
>>
>> The last construction I thought would be a cutaway, leaving the vertical
>> arm (I think) of the BL Touch mount for a Creality CR-10 S5 printer.  I
>> could include the STL if the non-parameterized version of need be
>> if/when I can find one, probably on thingiverse.
>>
>> This is from scratch with nothing streamlined and no efficiency attempts
>> in the code yet.  Just straight hunt-and-peck so far.
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: What am I doing wrong?

lar3ry
Well, it's definitely not the way I would go about it, so here's what I would
do.
You'll have to play with the values, and you are certainly welcome to put
them into variables.
Note the pound sign (#). It will help you see where the line puts the
object.

difference() {
        union() {
                translate([0,18,0])
                        cube([37,16.67,4]);
                translate([22,18,0])
                        cube([6,16.67,30]);
                translate([28,0,0])
                        cube([9,19,4]);
        }
        translate([13,22,-.001])
# cylinder(h=6,d=3);
        translate([31,4,-.001])
# cylinder(h=6,d=3);
}
 



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: What am I doing wrong?

skypuppy
It's obviously not optimized yet.  As mentioned, it's kinda baselined
peek and poke just to get the function crystalized in my head.  Does the
snipped you wrote draw the entire (to this point) construction?


On 12/5/20 12:54 AM, lar3ry wrote:

> Well, it's definitely not the way I would go about it, so here's what I would
> do.
> You'll have to play with the values, and you are certainly welcome to put
> them into variables.
> Note the pound sign (#). It will help you see where the line puts the
> object.
>
> difference() {
> union() {
> translate([0,18,0])
> cube([37,16.67,4]);
> translate([22,18,0])
> cube([6,16.67,30]);
> translate([28,0,0])
> cube([9,19,4]);
> }
> translate([13,22,-.001])
> # cylinder(h=6,d=3);
> translate([31,4,-.001])
> # cylinder(h=6,d=3);
> }
>  
>
>
>
> --
> Sent from: http://forum.openscad.org/
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: What am I doing wrong?

skypuppy
In reply to this post by lar3ry
What function does the # indicate in OpenSCAD?

And what does the -0.001 do for us?

David


On 12/5/20 12:54 AM, lar3ry wrote:

> Well, it's definitely not the way I would go about it, so here's what I would
> do.
> You'll have to play with the values, and you are certainly welcome to put
> them into variables.
> Note the pound sign (#). It will help you see where the line puts the
> object.
>
> difference() {
> union() {
> translate([0,18,0])
> cube([37,16.67,4]);
> translate([22,18,0])
> cube([6,16.67,30]);
> translate([28,0,0])
> cube([9,19,4]);
> }
> translate([13,22,-.001])
> # cylinder(h=6,d=3);
> translate([31,4,-.001])
> # cylinder(h=6,d=3);
> }
>  
>
>
>
> --
> Sent from: http://forum.openscad.org/
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: What am I doing wrong?

lar3ry
skypuppy wrote
> It's obviously not optimized yet.  As mentioned, it's kinda baselined
> peek and poke just to get the function crystalized in my head.  Does the
> snipped you wrote draw the entire (to this point) construction?
>
> What function does the # indicate in OpenSCAD?
>
> And what does the -0.001 do for us?

You have made it (to my way of thinking) more complicated by starting off
with a large cube, putting all your values into variables, and cutting away
a lot of things.

Yes, my program draws the entire (to this point) construction. I did not
bother trying to figure out the exact values, but rather created an STL
file, imported it, and then created an approximate duplicate by overlaying
cubes and subtracting cylinders, each one using the # character to see when
I had the dimensions close enough.

When you put the # in front of a line, it highlights the part drawn in
translucent red so you can see what the line is doing, when you preview the
drawing. Just put the program into OpenSCAD and you'll see. Try putting it
in front of the first cube(). I purposely made the cube the size I made it
to show you something.

The -.001 moves the bottom of the cylinder .001 below the bottom of both
cubes. try replacing one of them with a 0, and rotate the preview until you
can see the bottom. You'll see a difference in the two holes. The shimmering
is caused by having two surfaces in the same plane. In either case, the hole
will be fine when it's rendered.

Just paste that into OpenSCAD and play with it.




--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org