TNO
-
Total Posts
:
2091
- Scores: 34
-
Reward points
:
0
- Joined: 12/18/2004
- Location: Earth
-
Status: offline
|
RE: 2 Dimensional array
Monday, May 04, 2009 7:46 AM
( permalink)
Hi ehvbs, let me explain it this way: Imagine you have an image that is 10 x 10 in size. Each pixel in that image is defined by the color values R,G,B, A for Red Green Blue and Alpha. If I was to represent that picture in an array it could be a 3 dimensional array like this:
screen = [
[
[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a]
],
[
[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a]
],
[
[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a]
],
[
[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a]
],
[
[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a]
],
[
[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a]
],
[
[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a]
],
[
[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a]
],
[
[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a]
],
[
[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a]
]
] This would make it easy and intuitive for me to find and update the individual pixel colors: screen[x][y][c] = red screen[x][y][c + 1] = green screen[x][y][c + 2] = blue screen[x][y][c + 3] = alpha But what if I want to do some image processing or filtering on my image? What if my image is 5000x5000 in size? That's a lot of pixels to touch. And alot of lookups on the array to find that x/y position. So one of the things you can do to speed up access to the array is to use fewer look ups by changing your data representation, in this case flattening it down to 1 dimension: screen = [ r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a, r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a, r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a, r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a, r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a, r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a, r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a, r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a, r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a, r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a ] So your lookup can look like screen[..some expression resulting in an index number...] In which that expression could be calculated before hand in whole or in part. So the main hurdle in my challenge is, what is that magic expression that will allow you to find any color in the array? In other words, can you come up with the formula for P? Sub SetPixel(x,y,c) Dim P : P = ??? screen(P) = c("Red") screen(P +1) = c("Green") screen(P + 2) = c("Blue") screen(P + 3) = c("Alpha") End Sub In regards to the Tuples, I was hoping TomRiddle was going to take a shot at it. Maybe with something involving a regular expression approach to see if it can prove to be shorter. My original solution to that challenge isn't in VBS, so I need to translate it over.
To iterate is human, to recurse divine. -- L. Peter Deutsch
|