#Iteration problem?

30 messages · Page 1 of 1 (latest)

surreal hemlock
#

my code:

      for (int x = 0; x < pyramidVectors[level].length; x++) {
        for (int y = 0; y < pyramidVectors[level].length; y++) {
          pyramidVectors[level][x][y] = new PVector(x + (-2* PYRAMID_SIZE) + level, y + (-2* PYRAMID_SIZE) + level, level + (-1 * PYRAMID_SIZE));
          if (pyramidVectors[level][x][y].getX() == 0 && pyramidVectors[level][x][y].getY() == 0 && pyramidVectors[level][x][y].getZ() == 0) {
            pyramidVectors[level][x][y] = null;
          } 
          
        }
      }
    }```

sets `pyramidVectors[level][x][y]` to `null` whenever the loop encounters a `pyramidVectors[level][x][y]` where `level + x` = 6,

however the following code:
```public static void printVectorList() {
    for (int level = 0; level < pyramidLevels; level++) {
      for (int x = 0; x < pyramidVectors[level].length; x++) {
        for (int y = 0; y < pyramidVectors[level].length; y++) {
          
          if (pyramidVectors[level][x][y]!= null) {
            System.out.println("["+level+"]"+"["+x+"]"+"["+y+"]\n"+pyramidVectors[level][x][y]);
          }

          if (pyramidVectors[level][x][y]== null) {
            System.out.println("["+level+"]"+"["+x+"]"+"["+y+"]\n(null)");
          }
          
        }
      }
    }
  }```

is able to correctly print each vector... what gives?

Attached is the print output of the second function with the format:
```[level][x][y]
(x,y,z)```
pastel haloBOT
#

This post has been reserved for your question.

Hey @surreal hemlock! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

odd bison
#

how is this related to generics..?

surreal hemlock
#

i honestly thought generics was like a generic question

odd bison
#

either way they aren't relevant to this question yeah, but you can check them out later (or now) if you want

#

anyways

#

i don't see anything that says level + x == 6 there

#

your if checks if all the coords are 0, which would happen if x + level = 2 * PYRAMID_SIZE and y + level = 2 * PYRAMID_SIZE and level = PYRAMID_SIZE, so only when x = y = level i think
are you sure that's being reached?

surreal hemlock
#

[0,0,0] is reached

odd bison
#

is PYRAMID_SIZE 0 though?

#

what's PYRAMID_SIZE?

surreal hemlock
#

PYRAMID_SIZE = 3

odd bison
#

and what's pyramidLevels, and pyramidVectors[level].length?

surreal hemlock
#

pyramid vectors is a 3 dimensional array with pyramidVectors[0][13][13], pyramidVectors[1][11][11] up to pyramidVectors[7][1][1]

#

so the broken function attempts to iterate through the 3 dimensional array

#

but also I think you might be right in that [1][1][1] is missing from the printed list

#

i haven no clue why that specific list index would not be accessed though

#

actually [1][1][1] appears, nvm

#

the only problem is that when level + x = 2* PYRAMID_SIZE, the PVector is set to null

odd bison
#

wait isn't that what you wanted

#

im confused

#

what's the intended behavior

#

(sec brb)

surreal hemlock
#

the intended behavior for the misfunctioning for loop is that all indices are given a PVector with coordinates (x - (2 **PYRAMID_SIZE) + level, y - (2*PYRAMID_SIZE) + level, z - PYRAMID_SIZE)

#

(x,y,z) format

#

however the problem is that indices where Level + x = 6 are set as a null vector

#

for example, [0][6][0] should be (0,-6,-3) for PYRAMID_SIZE = 3

#

note that the list indices are technically [z][x][y] but the vectors are (x,y,z)