This is in C.
I have two 2D arrays, ArrayA and ArrayB, that sample the same space. B samples a different attribute than ArrayA less frequently than ArrayA, so it is smaller than A.
Just to try to define some variables: ArrayA: SizeAX by SizeAY, indexed by indexA for a position posAX, posAY ArrayB: SizeBX by SizeAY, indexed by indexB for a position posBX, posBY
ArrayA and ArrayB are pointers to the start of the array, where the row of X's is stored first, then Y is incremented, and the next row of X's is stored (Y=1)
So I need to set indexB from a given indexA, such that it is a nearest neighbor sample, to associate with indexA's value.
Here's where I am (correct any errors please! Note that I am starting at index 0): If ArrayA is 9x9 and ArrayB is 3x3: (posX,posY) posA 0,0; indexA = 0 posB 0,0; indexB = 0
posA 8,0; indexA = 8 (end of first row) posB 2,0; indexB = 2
posA 0,1; indexA = 9 posB 0,0; indexB = 0 (still closer to the bottom point)
posA 0,3; indexA = 27 posB 0,1; indexB = 3
posA 8,8; indexA = 80 (last point) posB 2,2; indexB = 8
so far I have: indexA = posAX + (posAY * SizeAX)
what I've tried (and of course failed): indexB = (int) (indexA * (SizeBX * SizeBY / (SizeAX * SizeAY)) + 0.5) // Only appears to work for the first row and last value.. but this clearly doesn't work - but I am curious as to how exactly it maps the two together, but I'll look into that after I fix it..
I didn't have access to posAY or posAX, just indexA, but I should be able to break it down using mod and remainder, right? or is there a more efficient faster way? A
I also tried this:
indexB = (posAY * SizeBY / SizeAY) * SizeBY + (posAX * SizeBX / SizeAX)
I think the problem is that I need to round the X and Y indexes separate then use SizeBX and SizeBY afterwards?
An extra caveat is that ArrayA and ArrayB come from larger data set that both sample a larger space. Since the rectangle is arbitrary, either ArrayA or ArrayB could have the point closest to the bounds of the rectangle, leading to other issues as to which way the nearest neighbor is really grabbing. I am not sure about how to address this, either.