Note that there are some explanatory texts on larger screens.

plurals
  1. POPython/numpy: Splitting an index into coordinates
    primarykey
    data
    text
    <h2>Context</h2> <p>I'm working in Python and numpy, using an array to represent a 3D terrain. The array is of shape ((0.0, 0.0, 0.0),terrainLength) where terrainLength - for this question's purposes - is usually 400. I'm addressing by (x,z) and y is irrelevant for now. It seems that the best way to do this in numpy is as above: a one-dimensional array of length-three arrays (ie vertices). This means that if I want to pull out the vertex at (x=5,y=?,z=10), I have to reference terrain_array[x*terrainLength+z] ie terrain_array[2010].</p> <p>The "arbitrary base" mentioned in the title is terrainLength: I figure that numpy's coordinate system, if I'm switching between 1D numpy and 2D flat grid reference, is just like having a base-400 system where z is the rightmost column and x is the next left column.</p> <h2>Question</h2> <p>I'm writing a pair of functions to convert between the intuitive (x,?,z) coordinate system and numpy's one-dimensional coordinate system. As mentioned above, if I want to pull out the vertex at (x=5,y=?,z=10), I have to reference terrain_array[x*terrainLength+z] ie terrain_array[2010]. It seems like my functions will benefit from use of / and %, but I'm wondering if this is best practice or not, assuming positive integer coordinates:</p> <pre><code>def toGridCoords( matrix_index ): global terrainLength return floor(matrix_index/terrainLength), matrix_index%terrainLength def toArrayCoord( x, z ): global terrainLength return x*terrainLength+z </code></pre> <p>Thanks!</p> <p>PS - to pre-empt "why not have an array of shape ((0.0),3) which only contains the y-coord and assumes the x- and z-coords from position", I'm still very much writing the terrain system and that's caused all kinds of havoc already (made it difficult to introduce variable terrain resolution, for example...).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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