GML Condensing a Grid? [SOLVED]

C

Carbiner

Guest
Hello!

I'm having some trouble condensing a two-dimensional Ds_grid into a one-dimensional Ds_grid. The problem is that the 2D grid is of varying size, and the 1D grid is of fixed length.

I know I can't just read the 2D grid, so I want to take the mean of a region of the 2D grid and set my 'next' 1D cell equal to that mean. I do know that the 2D grid will always be square, and its size should always fit nicely into the 1D grid. I just can't seem to make that work, though.

Here's what I've come up with so far:
Code:
var region_size = xcells mod (input_grid_size div cell_size);
// Input Grid Size (32) is the Number of Xcells and Ycells. So there are a total of 1024 input grid cells.
// Xcells is the number of horizontal cells in the heat map. There are an equal number of ycells because the rooms are square.
// Cell Size is the number of pixels that each cell of the heat map represents in it's horizontal or verticle direction. So right now, each heat map cell represents 64 pixels
var i,ii,avg_heat;
var counter = 0;
for(i=0;i<xcells;i+=1+region_size)
    {
    for(ii=0;ii<ycells;ii+=1+region_size)
        {
        avg_heat = ds_grid_get_mean(heat_map,i,ii,i+region_size,ii+region_size);
        input_grid[# 0, counter] = avg_heat;
        counter++;
        }
    }
show_debug_message("Input Nodes: " + string(sqr(input_grid_size))-1); // -1 because it starts at 0 and counter starts at 0
show_debug_message("Inputed Nodes: " + string(counter)); // These should be equal
Here's a picture for how it's supposed to work in my mind, as an example:
Grid Conversion.png

Edited for clarity
 
Last edited by a moderator:
B

bojack29

Guest
Just do it manually

Code:
g1[# 0, 0] = (g2[# 0, 0] + g2[# 0, 1] + g2# 1, 0] + g2[#1, 1]) / 4;
 
C

Carbiner

Guest
Just do it manually
That's not strictly possible.

I'm sorry I wasn't clear on this, but the picture I showed is an example, my actual input grid is 1024 units long, and the grid that I'm averaging is of variable size in both the x and y directions, although it will always be a square and should always be evenly divisible.

Edited for clarity
 
B

bojack29

Guest
Oh. Then simply loop the instruction:

Code:
var n = ds_grid_width(g2) - 1;//The grid is a perfect square
for(var i = 0;i <= ds_grid_size(g1) -1;i ++){
     for(var j = 0;j<= n;j += 2){
          for(var k = 0;k <= n;k += 2){
               g1[# i, 0] = (g2[# j, k] + g2[# j + 1, k] + g2[# j, k + 1] + g2[# j + 1, k + 1]) / 4;
          }
     }
}
 
C

Carbiner

Guest
Alright. I took your code, modified it a bit and got a workable solution.

Here it is, in case anyone is wondering:
Code:
// Updating the input grid
var region_size = ((sqr(xcells) div sqr(input_grid_size))) div 2;
var i,ii,iii,avg_heat;
var p1,p2,p3,p4;
var n = (ds_grid_width(heat_map) div region_size)-region_size;
var length = ds_grid_height(input_grid);
for(i=0;i<length;i++)
    {
     for(ii=0;ii<=n;ii++)
        {
          for(iii=0;iii<=n;iii++)
            {
               p1 = heat_map[# ii*region_size, iii*region_size]
               p2 = heat_map[# ii*region_size + region_size, iii*region_size]
               p3 = heat_map[# ii*region_size, iii*region_size + region_size]
               p4 = heat_map[# ii*region_size + region_size, iii*region_size + region_size];
               avg_heat = (p1+p2+p3+p4) / 4;
               input_grid[# 0, i] = avg_heat;
            }
        }
    }
Thanks for the help.
 
Top