0
\$\begingroup\$

I know I'm missing something very obvious here, but I have a 1D array that contains integers.I have a map that is 30 x 30 and the center coordinate is 0,0 (This is actually 3D, but I'm essentially removing the Y-axis). Because of this offset, my loop is from -15 to 14, rather than 0 to 29. Is this incorrect?

I'd like to iterate through the array and if the integer is "1", then I'd like to call AddSphere(x,y,z) with the correct x and z values. How would I get the correct values for x and z? The file is designed to be read left to right rather than left to right, right to left, left to right etc... (although if that's a better approach, I can just change the data to match that style).

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

One possible way is to split the integer into two values between 0 and 29 (this is done using integer division and modulus) then offset them by -15:

for (int n = 0; n < 30 * 30; ++n)
{
    if (Array[n] == 1)
    {
        int x = n % 30 - 15;
        int y = 0;
        int z = n / 30 - 15;

        AddSphere(x, y, z);
    }
}

Note that -15..14 is asymmetrical. If you want (0,0) to be at the exact centre of your map, you need an odd value instead of 30.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you! The asymmetrical aspect also explains why my attempts ended up with a cube overlapping on the edge, or starting slightly in from it. \$\endgroup\$
    – XSL
    Commented Dec 29, 2012 at 0:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .