discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Re: [OpenSCAD] Novice programmer needs help

DE
David Eccles (gringer)
Sun, Jul 24, 2016 9:26 AM

I guess you want something like a cumulative sum to work out the start points
of each book:

// Maximum shelf length in mm
max_shelf_len = 40;
// Maximum shelf height
max_shelf_ht = 14;
// Maximum shelf depth
max_shelf_dep = 13;
// Minimum shelf depth
min_shelf_dep = 6;
// Minimum book height
min_book_ht = 7;
// Minimum book width in 0.1 mm
min_book_wid = 0.6;
// Maximum book width
max_book_wid = 2;
// Starting X point (should be zero)
shelf_len_x = 0;

function cumSum(x,offset=0,pos=0,sum=0,res=[]) =
(pos == len(x)) ? res :
cumSum(x, offset=offset, pos=pos+1,
sum=sum+offset+x[pos], res=concat(res,sum));

num_books = max_shelf_len / max_book_wid;
book_wid = rands(min_book_wid,max_book_wid,num_books);
book_ht = rands(min_book_ht,max_shelf_ht,num_books);
book_dep = rands(min_shelf_dep,max_shelf_dep,num_books);
book_starts = cumSum(book_wid,offset=1);

for(i=[0:len(book_wid)]){
translate([shelf_len_x+book_starts[i],0,0])
cube([book_wid[i],book_ht[i],book_dep[i]]);
}

--
View this message in context: http://forum.openscad.org/Novice-programmer-needs-help-tp17956p17963.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

I guess you want something like a cumulative sum to work out the start points of each book: // Maximum shelf length in mm max_shelf_len = 40; // Maximum shelf height max_shelf_ht = 14; // Maximum shelf depth max_shelf_dep = 13; // Minimum shelf depth min_shelf_dep = 6; // Minimum book height min_book_ht = 7; // Minimum book width in 0.1 mm min_book_wid = 0.6; // Maximum book width max_book_wid = 2; // Starting X point (should be zero) shelf_len_x = 0; function cumSum(x,offset=0,pos=0,sum=0,res=[]) = (pos == len(x)) ? res : cumSum(x, offset=offset, pos=pos+1, sum=sum+offset+x[pos], res=concat(res,sum)); num_books = max_shelf_len / max_book_wid; book_wid = rands(min_book_wid,max_book_wid,num_books); book_ht = rands(min_book_ht,max_shelf_ht,num_books); book_dep = rands(min_shelf_dep,max_shelf_dep,num_books); book_starts = cumSum(book_wid,offset=1); for(i=[0:len(book_wid)]){ translate([shelf_len_x+book_starts[i],0,0]) cube([book_wid[i],book_ht[i],book_dep[i]]); } -- View this message in context: http://forum.openscad.org/Novice-programmer-needs-help-tp17956p17963.html Sent from the OpenSCAD mailing list archive at Nabble.com.
P
postalpirate
Sun, Jul 24, 2016 1:24 PM

David Eccles, that is really close to what I was looking for. However, I was
hoping to have the "books" stacked next to each other like on a library
shelf. Looking at the code you wrote, I might be way out of my league on
attempting something like this.

--
View this message in context: http://forum.openscad.org/Novice-programmer-needs-help-tp17956p17964.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

David Eccles, that is really close to what I was looking for. However, I was hoping to have the "books" stacked next to each other like on a library shelf. Looking at the code you wrote, I might be way out of my league on attempting something like this. -- View this message in context: http://forum.openscad.org/Novice-programmer-needs-help-tp17956p17964.html Sent from the OpenSCAD mailing list archive at Nabble.com.
W
wolf
Sun, Jul 24, 2016 9:13 PM

Try this, it should be easier to understand:

// create list
FirstList=[for (i=[0:1:6]) i];
LastOne=FirstList[len(FirstList)-1];
echo(len(FirstList),LastOne);

// sum up all elements in list
AddUp=[for (i=[1:1:len(FirstList)-1]) FirstList[i-1]+FirstList[i]];
Sum=AddUp[len(AddUp)-1];
echo(AddUp,Sum);

wolf

--
View this message in context: http://forum.openscad.org/Novice-programmer-needs-help-tp17956p17965.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Try this, it should be easier to understand: // create list FirstList=[for (i=[0:1:6]) i]; LastOne=FirstList[len(FirstList)-1]; echo(len(FirstList),LastOne); // sum up all elements in list AddUp=[for (i=[1:1:len(FirstList)-1]) FirstList[i-1]+FirstList[i]]; Sum=AddUp[len(AddUp)-1]; echo(AddUp,Sum); wolf -- View this message in context: http://forum.openscad.org/Novice-programmer-needs-help-tp17956p17965.html Sent from the OpenSCAD mailing list archive at Nabble.com.
DE
David Eccles (gringer)
Sun, Jul 24, 2016 9:19 PM

Can you explain what you mean by "stacked next to each other"? Most normal
bookshelves don't have books of such varying sizes right next to each other,
so it's difficult for me to work out what the right thing to do should be.

The code I wrote had an "offset", which can be set to zero in order to have
the books touching each other.

--
View this message in context: http://forum.openscad.org/Novice-programmer-needs-help-tp17956p17966.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Can you explain what you mean by "stacked next to each other"? Most normal bookshelves don't have books of such varying sizes right next to each other, so it's difficult for me to work out what the right thing to do should be. The code I wrote had an "offset", which can be set to zero in order to have the books touching each other. -- View this message in context: http://forum.openscad.org/Novice-programmer-needs-help-tp17956p17966.html Sent from the OpenSCAD mailing list archive at Nabble.com.
DE
David Eccles (gringer)
Sun, Jul 24, 2016 9:23 PM

It looks a bit better setting the offset to a smaller value (e.g. 0.2), and
swapping the height and depth:

book_starts = cumSum(book_wid,offset=0.2);

for(i=[0:len(book_wid)]){
translate([shelf_len_x+book_starts[i],0,0])
cube([book_wid[i],book_dep[i],book_ht[i]]);
}

--
View this message in context: http://forum.openscad.org/Novice-programmer-needs-help-tp17956p17967.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

It looks a bit better setting the offset to a smaller value (e.g. 0.2), and swapping the height and depth: book_starts = cumSum(book_wid,offset=0.2); for(i=[0:len(book_wid)]){ translate([shelf_len_x+book_starts[i],0,0]) cube([book_wid[i],book_dep[i],book_ht[i]]); } -- View this message in context: http://forum.openscad.org/Novice-programmer-needs-help-tp17956p17967.html Sent from the OpenSCAD mailing list archive at Nabble.com.
C
cbernhardt
Sun, Jul 24, 2016 10:07 PM

Don't feel alone, my first programming was on an Apple II.
I'm not sure this is what you want, but here is some code that may get you
close to your goal.
change the line that says off=a2.3 to off=a1.5 (or lower) to reduce space
between books

module book(x=1,y=1,z=1)
{ cube([x,y,z]);}

for (a =[0:40])
{
xrn = rands(1,3,1)[0];
yrn = rands(5,10,1)[0];
zrn = rands(6,12,1)[0];
// change off to lower value to reduce space between books
off=a*2.3;
translate([off,0,0])
book(xrn,yrn,zrn);
}

http://forum.openscad.org/file/n17968/books.jpg

--
View this message in context: http://forum.openscad.org/Novice-programmer-needs-help-tp17956p17968.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Don't feel alone, my first programming was on an Apple II. I'm not sure this is what you want, but here is some code that may get you close to your goal. change the line that says off=a*2.3 to off=a*1.5 (or lower) to reduce space between books module book(x=1,y=1,z=1) { cube([x,y,z]);} for (a =[0:40]) { xrn = rands(1,3,1)[0]; yrn = rands(5,10,1)[0]; zrn = rands(6,12,1)[0]; // change off to lower value to reduce space between books off=a*2.3; translate([off,0,0]) book(xrn,yrn,zrn); } <http://forum.openscad.org/file/n17968/books.jpg> -- View this message in context: http://forum.openscad.org/Novice-programmer-needs-help-tp17956p17968.html Sent from the OpenSCAD mailing list archive at Nabble.com.