views:

75

answers:

2

For my application, i need to divide diagonally the screen of my iphone in 4 parts and detect which of the part was touched. I am very confused because my areas are triangles and not squares and I can't find a solution to detect which of the triangle was touched...

I get the touched Point with method touchesBegan, and there I'm stuck... :( How to define triangle shapes and test if it was touched? with View? layer?

It could be really cool if someone could help me.

+2  A: 

Given width, height of the rectangle and x, y of a point in the rectangle you can do this...

int s = y * width / height;
int code = (x > s) + 2*(x > width - s);

code will be a number from 0 to 3 representing which part has been selected.

6502
Nice. Very clean.
Mike Daniels
A: 

This website show some algorithms to check if a point is inside a triangle or not.

http://www.blackpawn.com/texts/pointinpoly/default.html

Depending on how fast your application should run, you might want to precompute and store a matrix will all possible x and y coordinates. This matrix would look something like this:

point_inside[X][Y][triangle]

Where X and Y are the coordinates in your screen and "triangle" is pointer to which triangle should be in that point.

Cesar Canassa
It is basically inconceivable that the memory cost of a lookup table would be justified by any marginal speedup that might be achieved over 6502's method, at least in the case of something like iPhone touch events.
walkytalky