简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM