Search code examples
coordinatesformulapixel

Calculate pixel from coordinates


Hello I have this image:

enter image description here

This image has 16 pixels. White is 0, pink 1... like in image. My problem is that I need to calculate number of pixel from coordinates. If I have coordinates x = 3 and y = 3, I need to get black pixel with number 15.

How can I do that?


Solution

  • If you know how many pixels per row you have, you can simply use the next formula :

    (y * numberPerRow) + x

    Example using a 16 pixels images (4x4)

    x y calcul value
    3 3 3 * 4 + 3 15
    0 0 0 * 4 + 0 0
    1 2 2 * 4 + 1 9