Linear to Matrix translation
To continue on my explanations of array usage... Once you have figured out pointers you gain a very useful tool. Unfortunately you may still want some of the characteristics of the multi-dimensional array. Currently I'm working on an image manipulation algorithm (though I apply it to sound). The math for the mutation of the image is dependant on the use of an X,Y matrix (two-dimensional array). The problem is how do you perform a manipulation on data within a matrix but store the information in a linear (one-dimensional) array.
array[10]; // an array with 10 values matrix[5][2]; // a matrix with 5 groups of two values array = {0,1,2,3,4,5,6,7,8,9}; matrix = {{0,1},{2,3},{4,5},{6,7},{8,9}};to derive (x,y) values from a linear array
x = linear_value / xmax //integer division (drop the decimals) y = linear_value % ymax xmax = number of columns ymax = number of rows % = mod operator, the remainder after division ex. (using the above array and matrix) array[5] = (x,y) x = 5/2 = 2 y = 5%2 = 1 (x,y) = (2,1)to derive linear values from an (x,y) matrix
linear_value = (xmax * x) + y