Reputation: 3653
This is really sort of a general programming question. I have a window split into 9 even squares. When the user clicks one of those squares, I'd like to know which one. I can get the location of the click via x and y variables.
My current approach is xRegion = screenWidth / x
and yRegion = screenHeight / y
which will give me, for instance, (1,1) for the point (320,240) in a 640x480 window. But that only works for x
and y
values greater than one third or so of the screen. I know this is probably really simple, but I can't seem to wrap my brain around it.
Upvotes: 0
Views: 26
Reputation: 5156
xRegion = (x*3) / screenWidth;
yRegion = (y*3) / screenHeight;
+-----+-----+-----+
| 0,0 | 1,0 | 2,0 |
+-----+-----+-----+
| 0,1 | 1,1 | 2,1 |
+-----+-----+-----+
| 0,2 | 1,2 | 2,2 |
+-----+-----+-----+
If you are ausing a language like js or php, you must floor/trunc the result to get an integer.
Add 1 to the results if you want the first region to be (1,1)
For results 1 to 9 do this: cell = yRegion*3 + xRegion + 1;
1 2 3
4 5 6
7 8 9
Upvotes: 2