# Question about recursive function

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

 In reply to this post by kdtop On 11/11/2018 2:10 PM, kdtop wrote: ```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. ``` Every call to circum_pts receives a "pts" array and returns that array, with a point and a recursive call to circum_pts appended.  Nothing ever makes pts be anything other than what is initially passed in. The initial call to circum_pts passes an array consisting of one empty array.  To that it appends the first [x,y] pair and the results from a recursive call, so the result of that concat is [ [ ], [ x1, y1 ], ].  The first recursive call receives the same array-of-empty-array, and so its concat is similarly [ [ ], [ x2, y2 ], ], and so when that gets returned, the result of the initial concat is [ [ ], [ x1, y1 ], [ ], [ x2, y2 ], ].  And so on. The first step is to eliminate that "pts" parameter.  It doesn't do anything except contribute these empty arrays. ```//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(angle, rad, angle_step = 1, end_angle=360) = let(x =rad * cos(angle)) let(y =rad * sin(angle)) angle < (end_angle+1) ? concat([[x,y]], circum_pts(angle+angle_step, rad, angle_step, end_angle)) : []; //-- End function ------------ ``` Note that the empty array at the end of the loop contributes no elements to the concatenation, and that addresses the problem you saw in your second message. But really you're better off avoiding recursion entirely and using a list comprehension: ```function circum_pts2(angle, rad, angle_step, end_angle) = [ for (i = [angle:angle_step:end_angle]) [ rad * cos(i),  rad * sin(i) ] ];``` _______________________________________________ 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

 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