Question about recursive function Classic List Threaded 7 messages Open this post in threaded view
|

Question about recursive function

 Hello all.  First time poster, but long-time OpenSCAD user (2+ yrs). I have the following code.   //Obtain vector of points on circumference of circle of radius = 50, start angle=10, end angle=45, step of 2 degrees circum_pts1 = circum_pts([[]], 10, 50, 2, 45);   echo(circum_pts1); function circum_pts(pts, angle, rad, angle_step = 1, end_angle=360) =   let(x =rad * cos(angle))   let(y =rad * sin(angle))     angle < (end_angle+1) ?     concat(pts, [[x,y]], circum_pts(pts, angle+angle_step, rad, angle_step, end_angle))   :     pts; //-- End function ------------ This outputs the following value for result ECHO: [[], [49.2404, 8.68241], [], [48.9074, 10.3956], [], [48.5148, 12.0961], [], [48.0631, 13.7819], [], [47.5528, 15.4508], [], [46.9846, 17.101], [], [46.3592, 18.7303], [], [45.6773, 20.3368], [], [44.9397, 21.9186], [], [44.1474, 23.4736], [], [43.3013, 25], [], [42.4024, 26.496], [], [41.4519, 27.9596], [], [40.4508, 29.3893], [], [39.4005, 30.7831], [], [38.3022, 32.1394], [], [37.1572, 33.4565], [], [35.967, 34.7329], []] Notice that every other value is a null value.  I can program around this, but would like to figure out how to avoid.   Any ideas? Thanks Kevin Toppenberg -- Sent from: http://forum.openscad.org/_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

Re: Question about recursive function

Open this post in threaded view
|

Re: Question about recursive function

Open this post in threaded view
|

Re: Question about recursive function

 In reply to this post by kdtop I wouldn't code such a function as recursive function. It is much faster and straight forward to code it as simple loop: circum_pts1 = circum_pts(0, 50, 2, 90);   echo(circum_pts1); polygon(circum_pts1);  // visual control function circum_pts(angle, rad, angle_step = 1, end_angle=360) = [for(i=[angle:angle_step:end_angle]) rad*[cos(i), sin(i)]]; -- Sent from: http://forum.openscad.org/_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

Re: Question about recursive function

 I like your solution of having this all in 1 line.  I didn't realize that one could use the following pattern: * [vector dependent on loop variable]thanks!KevinOn Mon, Nov 12, 2018 at 7:03 AM Parkinbot <[hidden email]> wrote:I wouldn't code such a function as recursive function. It is much faster and straight forward to code it as simple loop: circum_pts1 = circum_pts(0, 50, 2, 90);  echo(circum_pts1); polygon(circum_pts1);  // visual control function circum_pts(angle, rad, angle_step = 1, end_angle=360) = [for(i=[angle:angle_step:end_angle]) rad*[cos(i), sin(i)]]; -- 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