Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding Array Access (Processing)
    text
    copied!<p>Hi could anybody help me understand this particular piece of code from this Langton's Ant sketch.</p> <pre><code>antLoc = new int[]{rows/2,columns/2}; </code></pre> <p>I don't exactly understand what is actually happening here, here is the rest of the code for context. (originally from here <a href="http://www.openprocessing.org/visuals/?visualID=13653" rel="nofollow">http://www.openprocessing.org/visuals/?visualID=13653</a>) </p> <pre><code>boolean[][] state; int[] antLoc; int antDirection; int squareSize = 5; int columns, rows; color bgCol = color(0,128,128); color antCol = color (255,0,0); color sqCol = color(128,128,128); void setup(){ size(800,600); background(bgCol); columns = width/squareSize; rows = height/squareSize; state = new boolean[rows][columns]; for(int j = 0; j &lt; rows; j++){ for(int i = 0; i &lt; columns; i++){ state[j][i] = false; } } antLoc = new int[]{rows/2,columns/2}; antDirection = 1; } void drawScene(){ fill(sqCol); for(int j = 0; j &lt; rows; j++){ for(int i = 0; i &lt; columns; i++){ if(state[j][i]){ rect(i*squareSize,j*squareSize,squareSize,squareSize); } } } fill(antCol); rect(antLoc[1]*squareSize,antLoc[0]*squareSize,squareSize,squareSize); } void turnLeft(){ if (antDirection &gt; 1){ antDirection--; } else{ antDirection = 4; } } void turnRight(){ if (antDirection &lt; 4){ antDirection++; } else { antDirection = 1; } } void moveForward(){ if (antDirection == 1){ antLoc[0]--; } if (antDirection == 2){ antLoc[1]++; } if (antDirection == 3){ antLoc[0]++; } if (antDirection == 4){ antLoc[1]--; } } void updateScene(){ moveForward(); if (state[antLoc[0]][antLoc[1]] == false){ state[antLoc[0]][antLoc[1]] = true; turnRight(); } else { state[antLoc[0]][antLoc[1]] = false; turnLeft(); } } void draw(){ background(bgCol); drawScene(); for(int i = 0; i &lt; 10; i++){ updateScene(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload