1

I feel really dumb asking this, but bear with me. I know about the formula to get the pixel position in a linear array:

pos = (y * width + x) * 4

which works fine. jsFiddle. But before the image/table is linearized, same formula doesn't work. You need to use (let's discard the RGBA for simplicity)

pos = (y-1) * width + x

enter image description here

Why is that? I'm probably missing something really simple.


Update: I knew it was something simple. Silly me.

enter image description here

1
  • 4
    Your cartesian coordinates are off by 1. Both column and row counts start at 0, not 1. Commented Nov 1, 2015 at 15:08

1 Answer 1

1

In javascript pixel coordinates start at 0, the same as any coordinate system. The pixel is referenced by its top left corner, thus the first pixel is at (0,0) , then the next going right (1,0) (2,0) and so on. The pixel below at (0,1) we give coordinates relative to the origin (0,0).

We give sizes as counts. When using width and height they are pixel counts and you start at one when you count. So the 100th pixel is on row 99. Just the same as this is the 21th century but we are in the year 2015.

So no need to subtract 1 from the pixel coordinates.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.