I am working with a native class that represents a 2D image as a 1D array. If you want to change one pixel, for example, you need to now how to derive the index from the x,y
coordinates.
So, let's say we have a 1D array array1d
like this:
array1d = [ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y ]
In the context of our program, array1d
represents a 2D grid:
a b c d e
f g h i j
k l m n o
p q r s t
u v w x y
And we want to perform operations on array1d
such as:
- Get the value at
x,y
coordinates (in this example,1,2
would givel
) - Get any sub-grid using
x,y,width,height
(1,2,2,2
would give[l, m, q, r]
) - Set the value at any
x,y
coordinate (etc.)
How do we do these?