Romain
Romain

Reputation: 4080

How to detect touch in triangle area

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.

Upvotes: 2

Views: 953

Answers (2)

Cesar Canassa
Cesar Canassa

Reputation: 20173

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.

Upvotes: 1

6502
6502

Reputation: 114481

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.

Upvotes: 3

Related Questions