[OpenSCAD] function for vector sizeof/length?

William Adams william_a_adams at msn.com
Fri Nov 11 00:01:09 CET 2011


It would still be nice to know the length of an array. function lerp1(u, a, b) = (1-u)*a + u*b;function lerp2(u, a, b) = [lerp1(u, a[0], b[0]), lerp1(u, a[1], b[1])];function lerp3(u, a, b) = [lerp1(u, a[0], b[0]), lerp1(u, a[1], b[1]), lerp1(u, a[2], b[2])]; function lerp(u, a, b) = len(a) == 1 ? [lerp1(u, a[0], b[0])] :    len(a) == 2 ? lerp2(u, a, b) :    len(a) == 3 ? lerp3(u, a, b) :    [] Here, I want to know the length of the array so I can determine which function to call.  I use lerp here for simplicity.  You don't really needit in this case, but I have run across many situations where it would be desirable. Also, it would be beneficial because then the empty array '[]' could serve as kind of a 'null' value.  I don't want to return '-1' as a return value, I want to return an array that has no entries in it.  Or, if the system had a 'null', or 'undef' that I could reference, that would be good as well. At any rate, knowing the length of an array could have some very good uses. -- William

===============================

- Shaping clay is easier than digging it out of the ground.


http://blog.nanotechstyles.com
http://www.thingiverse.com/WilliamAAdams
https://github.com/Wiladams
 

 > From: andrew at plumb.org
> Date: Thu, 10 Nov 2011 15:22:06 -0500
> To: andrew at plumb.org
> CC: openscad at rocklinux.org
> Subject: Re: [OpenSCAD] function for vector sizeof/length?
> 
> s/sublet/subtle/
> 
> On 2011-11-10, at 3:20 PM, Andrew Plumb wrote:
> 
> > Just the sublet language thing I was looking for.
> > 
> > Thanks!
> > 
> > Andrew.
> > 
> > On 2011-11-10, at 3:18 PM, Giles Bathgate wrote:
> > 
> >> Can you use:
> >> 
> >> for(j=paths)
> >> 
> >> I.e for can itterate over a vector as well as a range
> >> 
> >> Regards
> >> 
> >> Giles
> >> 
> >> On Nov 10, 2011 8:13 PM, "Andrew Plumb" <andrew at plumb.org> wrote:
> >> Hey Marius,
> >> 
> >> Is there a utility function of some kind that will return the number of elements in a vector?
> >> 
> >> I could *really* use one for iterating over elements of variable-length vectors passed into modules.
> >> 
> >> Hypothetical example:
> >> 
> >> --snip--
> >> 
> >> mypoints=[[0,0],[10,0],[10,10],[5,15],[0,10],[4,1],[6,1],[6,6],[4,6]];
> >> mypaths=[[0,1,2,3,4],[5,6,7,8]];
> >> 
> >> module polygon_outline(points,paths,width=0.2) {
> >> for(i=[0:sizeof(paths)-1] ) {
> >>   for(j=[1:sizeof(paths[i])-1] ) {
> >>     // draw line from points[paths[i][j-1]] to points[paths[i][j]] using oriented rectangles and octagons at endpoints of width 0.2
> >>   }
> >>   // draw closing line from points[paths[i][sizeof(paths[i])-1]] back to points[paths[i][0]]
> >> }
> >> }
> >> 
> >> difference() {
> >> polygon_outline(points=mypoints,paths=mypaths,width=0.5);
> >> polygon(points=mypoints,paths=mypaths);
> >> }
> >> 
> >> --end-snip--
> >> 
> >> Thanks!
> >> 
> >> Andrew.
> >> 
> >> 
> >> --
> >> 
> >> "The future is already here.  It's just not very evenly distributed" -- William Gibson
> >> 
> >> Me: http://clothbot.com/wiki/
> >> 
> >> 
> >> _______________________________________________
> >> OpenSCAD mailing list
> >> OpenSCAD at rocklinux.org
> >> http://rocklinux.net/mailman/listinfo/openscad
> >> _______________________________________________
> >> OpenSCAD mailing list
> >> OpenSCAD at rocklinux.org
> >> http://rocklinux.net/mailman/listinfo/openscad
> > 
> > --
> > 
> > "The future is already here.  It's just not very evenly distributed" -- William Gibson
> > 
> > Me: http://clothbot.com/wiki/
> > 
> > 
> 
> --
> 
> "The future is already here.  It's just not very evenly distributed" -- William Gibson
> 
> Me: http://clothbot.com/wiki/
> 
> 
> _______________________________________________
> OpenSCAD mailing list
> OpenSCAD at rocklinux.org
> http://rocklinux.net/mailman/listinfo/openscad
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rocklinux.net/pipermail/openscad/attachments/20111111/5bb0d8f0/attachment.html


More information about the OpenSCAD mailing list