Discussion:
[OpenSCAD] Question about recursive function
kdtop
2018-11-11 22:10:38 UTC
Permalink
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/
Kevin Toppenberg
2018-11-11 22:25:40 UTC
Permalink
Let's see, working on this some more....

The following code gets rid of the every-other bad value. So this is
almost there. It just has a final null value that I wish I could get rid
of.

Thanks
Kevin

//Obtain vector of points on circumference of circle of radius = 50, start
angle=0, end angle=90, step of 10 degrees
circum_pts1 = circum_pts(0, 50, 10, 90);
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) ?
concat([[x,y]], circum_pts(angle+angle_step, rad, angle_step,
end_angle))
:
[[]];
//-- End function ------------

ECHO: [[50, 0], [49.2404, 8.68241], [46.9846, 17.101], [43.3013, 25],
[38.3022, 32.1394], [32.1394, 38.3022], [25, 43.3013], [17.101, 46.9846],
[8.68241, 49.2404], [0, 50], []]
Post by kdtop
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
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Parkinbot
2018-11-12 12:02:50 UTC
Permalink
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/
Kevin Toppenberg
2018-11-12 12:50:51 UTC
Permalink
I like your solution of having this all in 1 line. I didn't realize that
one could use the following pattern:

<for loop producing scalar> * [vector dependent on loop variable]

thanks!

Kevin
Post by Parkinbot
I wouldn't code such a function as recursive function. It is much faster and
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
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
runsun
2018-11-15 02:44:27 UTC
Permalink
Post by Kevin Toppenberg
I like your solution of having this all in 1 line. I didn't realize that
<for loop producing scalar>
* [vector dependent on loop variable]
1. a*[b,c] = [ab,ac];
2. a*[b,c,d] = [ab,ac,ad];
3. [a,b]*[c,d] = ac + bd;
4. [a,b,c]*[d,e,f] = ad + be + cf;
5. [a,b,c]*[c,d] = undef;
6. [a,b]*[d,e,f] = undef;
a. (P+Q)/2 = the mid point between P,Q;
(P+Q+R)/3 = the centroid point (the gravity) of PQR;
b. n*[P,Q] = a line parallel to line PQ;
n*[P,Q,R] = a plane parallel to plane PQR;
--- on line PQ if i+j=1; // 1.2P -0.2Q
--- within PQ line segment if i+j=1 AND i,j>0 ; // 0.2P + 0.8Q
--- on plane PQR if i+j+k = 1; // 0.2P + 1.1Q - 0.3R
--- within PQR triangle if i+j+k=1 AND i,j,k>0 ; // 0.2P + 0.5Q + 0.3R
-----

$ Runsun Pan, PhD $ libs: scadx , doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), editor of choice: CudaText ( OpenSCAD lexer );&nbsp;$ Tips ;&nbsp;$ Snippets

--
Sent from: http://forum.openscad.org/
Kevin Toppenberg
2018-11-15 03:53:17 UTC
Permalink
Very cool. Thanks!

Kevin
Post by runsun
Post by Kevin Toppenberg
I like your solution of having this all in 1 line. I didn't realize that
<for loop producing scalar>
* [vector dependent on loop variable]
1. a*[b,c] = [ab,ac];
2. a*[b,c,d] = [ab,ac,ad];
3. [a,b]*[c,d] = ac + bd;
4. [a,b,c]*[d,e,f] = ad + be + cf;
5. [a,b,c]*[c,d] = undef;
6. [a,b]*[d,e,f] = undef;
a. (P+Q)/2 = the mid point between P,Q;
(P+Q+R)/3 = the centroid point (the gravity) of PQR;
b. n*[P,Q] = a line parallel to line PQ;
n*[P,Q,R] = a plane parallel to plane PQR;
--- on line PQ if i+j=1; // 1.2P -0.2Q
--- within PQ line segment if i+j=1 AND i,j>0 ; // 0.2P + 0.8Q
--- on plane PQR if i+j+k = 1; // 0.2P + 1.1Q - 0.3R
--- within PQR triangle if i+j+k=1 AND i,j,k>0 ; // 0.2P + 0.5Q +
0.3R
-----
$ Runsun Pan, PhD $ libs: scadx , doctest , faces ( git ), offline doc (
git ), runscad.py ( 2 , git ), editor of choice: CudaText ( OpenSCAD lexer
);&nbsp;$ Tips ;&nbsp;$ Snippets
--
Sent from: http://forum.openscad.org/
_______________________________________________
OpenSCAD mailing list
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Loading...