0
$\begingroup$

I'm working on the implementation of a cantor set on a 2-dimensional plane. It looks like this. Honestly, There is the obvious algorithm for the cantor set, but it includes a recursive method call. Unfortunately, My environment(Houdini vex) does not support recursive function calls, so I have to design the algorithm without using recursion.

  1. Is it possible to represent a cantor set without recursion?
  2. How can I represent the recursive thing in the algorithm without recursion?

Cantor set implementation on a 2-dimensional grid

int size=ch("size"); //size=27
int can[]={0};
int offset=0;
int col=size;
append(can,size);
int index=0;
int step=1;
int temp[]={};
int firstentry=0;
function void line(int x1; int x2)
{
 //this function gets x1(startpoint), x2(endpoint) and color entries in
 int step= x2-x1;
 if (step==0)
 {
 
 
 //color the single sqaure 
 setprimgroup(0,"cantor",x1,1,"set");
 }
 else{
 //setprimgroup do the role of coloring the squre.
 
 for(int i=x1; i<=step+x1; i++)
 {
 setprimgroup(0,"cantor",i,1,"set");
 }
 }
 
}
while(size>=1)
{
 //first step 0~26
 if(index<size)
 {
 //first line is set.
 setprimgroup(0,"cantor",index,1,"set");
 index+=1;
 
 //first entry indicates the first entry of the row.
 
 }
 
///
 else{
 //split by 1/3
 //first entry indicates the first entry of the row.
 firstentry+=col;
 size=size/3;
 step=step*2;
 //done only for subdivision ex 1,2,4...
 for(int i=0; i<step; i++)
 {
 
 offset = size*2;
 int start=index;
 int end= start+size-1;
 
 line(start,end);
 
 
 printf("start %g end %g \n",start,end);
 
 index+=offset;
 //
 }
 
 index=firstentry+col;
 
 //for recursive step, multiply by 2.
 
 
 }
 
}

this language resembles C, I don't want you to do coding for me. Please present just an idea for correcting the gap calculation.

asked Dec 18, 2022 at 8:37
$\endgroup$

1 Answer 1

2
$\begingroup$

Yes. It can be expressed without recursion, as long as you have loops and data structures. Any recursive algorithm can be converted to a non-recursive iterative algorithm, by introducing an explicit stack. See, e.g., Iteration can replace Recursion?, Iterative and/or tail-recursive implementations of merge sort?, Do we need recursion in programming language to solve any problem?.

answered Dec 18, 2022 at 8:51
$\endgroup$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.