How to detect touch in triangle area

人走茶凉 提交于 2020-01-15 03:11:30

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3001487/how-to-detect-touch-in-triangle-area

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!