So I gave Nifty a copy of Matt's code and told him to go away and study it. Nifty was not happy.

bool EXTRA_TRIG_FUNCTION=false;

float rxy;

float ratrack=pi2-pi2/folds; //pi2 is 2*pi

float rotadjust=-pi/folds + FoldAdjust; // I called folds splits in my original code...

// I called them splits because I didn't know

float pintrack=pi/folds; //what they were called.....

float pi2splits=pi2/folds;

float i=0.0; //why a float?? I was doing something with it...

float omega=atan(z.y,z.x);

if (omega<0.0) {omega+=pi2;} //so angle goes from 0 to 2pi instead of having negative part

float rotate=0;

//I only wanted to do the kaleidoscope rotations on specific iterations

// doing multiple polyfolds... you can do it every iteration or whatever- for cool effects

// only do it on specific iterations- I was making kaleidoscopes out of 2d polyfolding

if (RotateIter==n) {rotate=Rotate;} //only do the rotation on a specific iteration

while (omega>pi2folds && i<floor(folds+1.0)) {

i+=1.0;

if (EvenFolds) {rotate*=-1;} //if you have an even number of folds, you can do 1/2 as

//many rotations- it's sort of cool

if (!EXTRA_TRIG_FUNCTION) { //that is a NOT "!"

rotadjust+=ratrack;

pintrack+=pi2folds;

omega-=pi2folds;

} else { //with extra trig function, you don't need the above... just do the above instead

omega-=pi2folds;

}

}

if (EXTRA_TRIG_FUNCTION) { // don't do this unless you absolutely love using transcendental functions

z=length(z)*vec2(cos(omega),sin(omega));

}

// if you don't have an even number of folds, you have to make an even number of rotations like the following:

if (omega>pi2folds*.5 && !EvenFolds) {rotate*=-1;}

// pintrack is used to shift the fold center outwords- images follow

vec2 z2=vec2(cos(pintrack),sin(pintrack));

// splitrad is the distance from the center that the folds are shifted outwards

z.xy-=z2*splitrad;

rxy=length (z.xy);

// rotadjust keeps every duplicate aligned correctly.

omega=atan(z.y,z.x)+rotadjust+rotate+FoldAdjust;

z.xy=rxy * vec2(cos(omega),sin(omega));

return z;