# How is my code going over the recursion limit?

I don't know how my code is exceeding the recursion limit of 1024.

subdivide[verts_, n_] := (

listOfPoints = Join[verts, {verts[[1]]}];

subdivisons = {};

For[i = 1, i < Length[listOfPoints], i++,
firstPoint = listOfPoints[[i]];
secondPoint = listOfPoints[[i + 1]];
x = (secondPoint[[1]] - firstPoint[[1]])/n;
y = (secondPoint[[2]] - firstPoint[[2]])/n;

For[j = 0, j < n, j++,
subdivisions = Join[subdivisions, {firstPoint + j {x, y}}];

];];

subdivisions;
)


• If you scrape-n-paste that into a notebook then I think you have a missing ] somewhere to finish off your first For
– Bill
Dec 2, 2023 at 4:29
• Whoops, sorry. There was a ] there already, I didn't realize I didn't put it. Dec 2, 2023 at 5:43
• Wonderful. Next, do you expect your subdivide to return the value in subdivisions? If so then do you want that ; at the end of that? Next, you might include one more line of code at the end, maybe something like subdivide[{1,2,5},4] or whatever would be good example input to your function. Next, you might try restarting MMA and running your code again. MMA has a cache and keeps track of previous assignments. Sometimes recursion errors are the result of a variable or function still being in cache. At the moment I'm getting an odd error about missing ) that I haven't figured out.
– Bill
Dec 2, 2023 at 6:14
• subdivide is meant to return an evenly divided n-1 number of points in between the vertices provided, which are meant to be the vertices of a polygon. So, if it's given {0, 0}, {1, 0}, {1, 1}, {0, 1} with n = 2, it'll give the halfway points between the vertices and end up with 8 coordinates. Dec 2, 2023 at 6:54
• Two typos. First, subdivisons = {}; has a typo. Second, remove the semicolon after the final subdivisions; Dec 2, 2023 at 8:18

I suggest that we use Mathematica's list manipulation capabilities to eliminate the For loops. Here's a subdivide function:

subdivide[verts_List, n_] := Flatten[Most[
Transpose[
Subdivide[Sequence @@ #, n] &/@ #]] &/@
Transpose/@Partition[Append[verts, First@verts], 2, 1], 1]


Example: each side of a square divided into two parts

verts = CirclePoints[Sqrt[2], 4];
n = 2;
subdivisions = subdivide[verts, n];
Graphics[{EdgeForm[Black], FaceForm[None], Polygon[verts],
Red, PointSize[Scaled[.025]], Point[subdivisions]}, ImageSize -> 240]


Example: each side of a random, five-sided polygon divided into four parts

SeedRandom[2];
verts = RandomPolygon[5][[1]];
n = 4;
subdivisions = subdivide[verts, n];
Graphics[{EdgeForm[Black], FaceForm[None], Polygon[verts],
Red, PointSize[Scaled[.025]], Point[subdivisions]}, ImageSize -> 240]


Details of the subdivide function

The first step is to start with the vertices' coordinate pairs, {{x1, y1}, ... {xn, yn}}, then rearrange pairs into the x and y pair coordinates for the start and end points of each side. For example:

verts = {{x1, y1}, {x2, y2}, {x3, y3}};
Transpose/@Partition[Append[verts,First@verts], 2, 1]

{{{x1, x2}, {y1, y2}}, {{x2, x3}, {y2, y3}}, {{x3, x1},{y3, y1}}}


Next, use Subdivide to find the n subdivisions of each side, and Transpose back into {x, y} pairs. Because the last point of each line is the first point of the following line, use Most to remove the duplicates. Flatten gives the resulting list of points for subdivisions.