Help with Locating Mouse Input within a Square
Hullo all,
It's been a long time, but I've got a quick question.
I'm trying to write a quick art project where I have a matrix of cubes, each divided diagonally to create 4 sections. I want to allow the user to toggle each one of these sections on and off by clicking. Figuring out which cube is easy enough, but I'm a little stuck on the math needed to figure out which section of said cube the user clicked on.
Here's a test case:
The cube with 4 sections looks like this:
I have the coordinates of the click. Let's assume the cube is 100x100. Therefore the center is { 50, 50 }
How on earth do I figure out from the coordinates in which section the user clicked? I'm really stumped here. Help from math geniuses? I have a feeling there's trig involved here somewhere.
It's been a long time, but I've got a quick question.
I'm trying to write a quick art project where I have a matrix of cubes, each divided diagonally to create 4 sections. I want to allow the user to toggle each one of these sections on and off by clicking. Figuring out which cube is easy enough, but I'm a little stuck on the math needed to figure out which section of said cube the user clicked on.
Here's a test case:
The cube with 4 sections looks like this:
I have the coordinates of the click. Let's assume the cube is 100x100. Therefore the center is { 50, 50 }
How on earth do I figure out from the coordinates in which section the user clicked? I'm really stumped here. Help from math geniuses? I have a feeling there's trig involved here somewhere.
Code:
bool topLeft = (x < y);
bool topRight = (100 - x < y)
if(topLeft){
if(topRight){
// top
} else {
// left
}
} else { // bottom right
if(topRight){
// right
} else {
// bottom
}
}
// or you could easily enough map that to an index if you just have one big array of wedges
int wedge_index = cube_index*4 + topLeft + topRight*2;
Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
You can actually do this without any trig. Here's how I'd do it: You'll do two tests, each with the square partitioned into two opposing right triangles (once partitioning diagonally one way, once the other way). Adjust your input coordinates so that {0, 0} is in the outer corner of one triangle, and {1, 1} is in the outer corner of the other triangle. Add x and y together, and if the result is less than 1, you're in the first triagle; if it's greater than 1, you're in the second one. Extrapolate from there to get the four regions.
Hope that made sense.
Hope that made sense.
Just to clarify: Skorche's math is using a coordinate system with 0,0 on the bottom-left of the axis. OpenGL however puts 0,0 on the top-left. So if you are working in OpenGL screen coordinates, I think his topLeft and topRight variables are instead computing bottomLeft, bottomRight.
ThemsAllTook's algorithm is equivalent to Skorche's, though he scales the coordinates to a unit square (0,) to (1,1). The idea is to test which side of the diagonal lines (x=y) and (1-x=y) the clicked point lies. Skorche's line equations are (x=y) and (100-x=y).
Those tests are for a square at the origin (0,0). For a square at position (x0, y0) just subtract those coordinates from the click location, which translates everything to the origin. Then do the test.
By "cube" it's pretty clear you meant "square", and all your squares are axis-oriented (ie. not rotated) but if either of these is not the case just let us know.
ThemsAllTook's algorithm is equivalent to Skorche's, though he scales the coordinates to a unit square (0,) to (1,1). The idea is to test which side of the diagonal lines (x=y) and (1-x=y) the clicked point lies. Skorche's line equations are (x=y) and (100-x=y).
Those tests are for a square at the origin (0,0). For a square at position (x0, y0) just subtract those coordinates from the click location, which translates everything to the origin. Then do the test.
By "cube" it's pretty clear you meant "square", and all your squares are axis-oriented (ie. not rotated) but if either of these is not the case just let us know.
Measure twice, cut once, curse three or four times.
Thanks all for your help. I was coding a quick app that would help me generate patterns for use in an art class.
Thought some of you would be interested in the final product. Check out the included sample files for an idea of what it's for (scale them down to 10% to get the idea).
http://www.aaronmcfarlane.com/tmp/pattern_maker.zip
Thought some of you would be interested in the final product. Check out the included sample files for an idea of what it's for (scale them down to 10% to get the idea).
http://www.aaronmcfarlane.com/tmp/pattern_maker.zip
Possibly Related Threads...
Thread: | Author | Replies: | Views: | Last Post | |
Second mouse button input | David | 5 | 6,111 |
Mar 28, 2003 07:24 AM Last Post: rvangaal |