X Y position to array item?

I have an image which is 320x240 px

I have an array of values for each pixel in the image (76,800 points)

If the user hovers over any part of the image I need to look at the array and return the corresponding value for it.

i.e.
X Y position [0,0] of the image = the first value in the array.

If I wanted to find out the X Y position [65,33] of the image, and then look up the  value in the array, what would be the correct forumla to do this
websssCEOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Well, how is your array organized? 2-dimensional or 1-dimensional? In the later case, the normal addressing is y*PIXELS_PER_LINE + x.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Zakaria AcharkiAnalyst DeveloperCommented:
You could create a function that will init your array from the given width/height and return it as the output :

function initPixelsArray(width, height)
{
  var pixels_arr = [];

  for (var i = 0; i < width; i++) {
    pixels_arr[i] = Array(height).fill().map((x, i) => i);

    for (var j = 0; j < height; j++) {
      pixels_arr[i][j] = 'INDEX '+i+'/'+j+' VALUE';
    }
  }
  
  return pixels_arr;
}

Open in new window


Then another function that searches for X/Y in the given array like :

function getIndex(arr, X,Y){
	return arr[X][Y];
}

Open in new window


Then call them like :

var pixels_arr = initPixelsArray(100, 50); //Init array
alert( getIndex(pixels_arr, 65, 33) ); //Get the specific x/y value

Open in new window

websssCEOAuthor Commented:
thanks both
Zakaria AcharkiAnalyst DeveloperCommented:
You're welcome, glad to help brother.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.