akinuri
akinuri

Reputation: 12027

How to get pixel position

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

Upvotes: 1

Views: 3460

Answers (1)

Blindman67
Blindman67

Reputation: 54039

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.

Upvotes: 1

Related Questions