Invert GMLscripts.com

ds_grid_translate

Shifts the contents of a grid by a given number of rows and columns. The contents are shifted so that they wrap around to the opposite side of the grid data structure.

$$\mathbf{G} = \begin{array}{|c|c|c|c|} \hline \color{#F80}A & \color{#C8F}B & \color{#C8F}C & \color{#C8F}D \\\hline \color{#C8F}E & F & G & H \\\hline \color{#C8F}I & J & K & L \\\hline \color{#C8F}M & N & O & P \\\hline \end{array} \qquad f(\mathbf{G},1,2) = \begin{array}{|c|c|c|c|} \hline L & \color{#C8F}I & J & K \\\hline P & \color{#C8F}M & N & O \\\hline \color{#C8F}D & \color{#F80}A & \color{#C8F}B & \color{#C8F}C \\\hline H & \color{#C8F}E & F & G \\\hline \end{array}$$

ds_grid_translate(grid, horiz, vert)
Shifts the contents of a grid by a given number of rows and columns.
COPY/// @func   ds_grid_translate(grid, horiz, vert)
///
/// @desc   Shifts the contents of a grid by a given number of rows
///         and columns. The contents are shifted so that they wrap
///         around to the opposite side of the grid data structure.
///
/// @param  {grid}      grid        grid data structure
/// @param  {real}      horiz       horizontal shift
/// @param  {real}      vert        vertical shift
///
/// GMLscripts.com/license

function ds_grid_translate(grid, horiz, vert)
{
    var w = ds_grid_width(grid);
    var h = ds_grid_height(grid);
    var sx = ((horiz % w) + w) % w;
    var sy = ((vert % h) + h) % h;
    var mx = w - 1;
    var my = h - 1;
    var dx = mx - sx;
    var dy = my - sy;
    var temp = ds_grid_create(w, h);
    ds_grid_set_grid_region(temp, grid, 0, 0, dx, dy, sx, sy);
    if (sx > 0) {
        ds_grid_set_grid_region(temp, grid, dx+1, 0, mx, dy, 0, sy);
    }
    if (sy > 0) {
        ds_grid_set_grid_region(temp, grid, 0, dy+1, dx, my, sx, 0);
    }
    if (sx > 0 && sy > 0) {
        ds_grid_set_grid_region(temp, grid, dx+1, dy+1, mx, my, 0, 0);
    }
    ds_grid_copy(grid, temp);
    ds_grid_destroy(temp);
}

Contributors: Leif902, xot

GitHub: View · Commits · Blame · Raw