Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>x</code> = x position<br> <code>y</code> = y position<br> <code>cols</code> = number of columns across in your grid</p> <p><code>(y * cols) + x</code> = index in array absolute value for any x, y</p> <p>you could generalize this in a function:</p> <pre><code>int get_index(int x, int y, int gridcols) { return (gridcols * y) + x; } </code></pre> <p>It should be noted that this works for ZERO BASED INDICES. This is assuming I am understanding what you're talking about at all...</p> <p>As for the second question, for any colored element you have, you should keep a value in memory (possibly stored in a structure) that keeps track of its position so you don't have to search for it at all.</p> <pre><code>struct _THING { int xpos; int ypos; }; </code></pre> <p>Using the get_index() function, you could find the index of the grid cell below it by calling like this:</p> <pre><code>index_below = get_index(thing.x, thing.y + 1, gridcols); thing.y++; // increment the thing's y now since it has moved down </code></pre> <p>simple...</p> <p>IF YOU WANT TO DO IT IN REVERSE, as in finding the x,y position by the array index, you can use the modulus operator and division.</p> <pre><code>ypos = array_index / total_cols; // division without remainder xpos = array_index % total_cols; // gives the remainder </code></pre> <p>You could generalize this in a function like this:</p> <pre><code>// x and y parameters are references, and return values using these references void get_positions_from_index(int array_index, int total_columns, int&amp; x, int&amp; y) { y = array_index / total_columns; x = array_index % total_columns; } </code></pre> <p>Whenever you're referring to an array index, it must be zero-based. However, when you are referring to the number of columns, that value will be 1-based for the calculations. x and y positions will also be zero based.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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