Full C Code for the 3.b case:
// extract from https://sites.google.com/site/projectsced/
long grid[10001][10001];
int main (int argc , char *argv[])
{
printf("\nrotor router program from CED builds an image");
printf("\nrule is the rotor router UDLR but with additional rule to make funny form ");
// Main algorithm run as a memory map
char *cmd;
cmd=malloc(1024);
// build the svg file from the memory map
// initialize the 2D array
printf("DEBUG: create 2D array");
/* 2D array declaration*/
long maxx = 10000;
long maxy = 10000;
long centerx = 5000;
long centery = 5000;
long agentCurrentPositionx = 0;
long agentCurrentPositiony = 0;
long agentCurrentValue = 4;
long numberOfAlgoIterations = 100000;
/*Counter variables for the loop*/
long i, j, k;
long endAgentIteration=1;
long firstcenterpass=0;
int test=0;
int itcounter=0;
int itflag=0;
printf("DEBUG: grid created");
//init to 0 the array
for(i=0; i<=maxx; i++)
{
for(j=0; j<=maxy; j++)
{
grid[i][j]=0;
//printf("DEBUG: %i",grid[i][j]);
}
}
printf("DEBUG: grid initialized");
//main loop:
for(k=0; k<=numberOfAlgoIterations; k++)
{
//agent position init:
agentCurrentPositionx=centerx;
agentCurrentPositiony=centery;
// init starting value for agent
if(agentCurrentValue < 4)
{
agentCurrentValue++;
}
else
{
agentCurrentValue=1;
}
//agent iteration loop
endAgentIteration=0;
firstcenterpass=0;
itcounter=0;
itflag=0;
while (endAgentIteration == 0)
{
//this part to faster detect probable endlessloops
itflag=0;
itcounter++;
if(itcounter>2000000 && itflag==0)
{
printf("\nDEBUG: loop 2M at %i",k);
itflag=1;
}
//we arrive to a new position
if (grid[agentCurrentPositionx][agentCurrentPositiony] == 0)
{
//empty position found
grid[agentCurrentPositionx][agentCurrentPositiony] = agentCurrentValue;
// in original rotor router this is set to UP while I set here to the agentCurrentValue
// this is an important modification that removes the moiré
endAgentIteration=1;
agentCurrentPositionx=centerx;
agentCurrentPositiony=centery;
}
if (endAgentIteration==0)
{
if(endAgentIteration==0)
{
//the agent is positive and the position is positive, lets continue the move
//then move to this direction
if(grid[agentCurrentPositionx][agentCurrentPositiony]== 1)
{
agentCurrentPositiony--;
// then rotate the rotor of the previous position
if(grid[agentCurrentPositionx][agentCurrentPositiony+1] < 4)
{
grid[agentCurrentPositionx][agentCurrentPositiony+1]=grid[agentCurrentPositionx][agentCurrentPositiony+1]+1;
}
else
{
grid[agentCurrentPositionx][agentCurrentPositiony+1]= 1;
}
}
if(grid[agentCurrentPositionx][agentCurrentPositiony]== 4)
{
agentCurrentPositionx++;
// then rotate the rotor of the previous position
if(grid[agentCurrentPositionx-1][agentCurrentPositiony] < 4)
{
grid[agentCurrentPositionx-1][agentCurrentPositiony]=grid[agentCurrentPositionx-1][agentCurrentPositiony]+1;
}
else
{
grid[agentCurrentPositionx-1][agentCurrentPositiony]= 1;
}
}
if(grid[agentCurrentPositionx][agentCurrentPositiony]== 2)
{
agentCurrentPositiony++;
// then rotate the rotor of the previous position
if(grid[agentCurrentPositionx][agentCurrentPositiony-1] < 4)
{
grid[agentCurrentPositionx][agentCurrentPositiony-1]=grid[agentCurrentPositionx][agentCurrentPositiony-1]+1;
}
else
{
grid[agentCurrentPositionx][agentCurrentPositiony-1]= 1;
}
}
if(grid[agentCurrentPositionx][agentCurrentPositiony]== 3)
{
agentCurrentPositionx--;
// then rotate the rotor of the previous position
if(grid[agentCurrentPositionx+1][agentCurrentPositiony] < 4)
{
grid[agentCurrentPositionx+1][agentCurrentPositiony]=grid[agentCurrentPositionx+1][agentCurrentPositiony]+1;
// grid[agentCurrentPositionx+1][agentCurrentPositiony]=0;
}
else
{
grid[agentCurrentPositionx+1][agentCurrentPositiony]=0;
}
}
}
}
}