Detecting clicks in a non-rectangular shape

旧时模样 提交于 2019-12-12 03:04:20

问题


I am pretty new to XNA 4.0 and have a problem I can't seem to figure out. So far I have been detecting mouse clicks by creating rectangles and checking the rectangle.contains method, but what if I don't want to check inside a rectangle?

In one part of my map I have a triangle button that I want to let the user click, but I want to only count it as a click if it is actually inside the triangle. If I create a rectangle around the triangle the user will be able to click outside the texture area, if you know what I mean.

How do I go about doing this?


回答1:


You will have to use a Point in Polygon Algorithm for more complex shapes:

  • C# Point in polygon
  • Determining Whether A Point Is Inside A Complex Polygon

The check for convex polygons (your triangle) is simpler than for concave ones (see first linked article).

If you have to do lots of checks and are hitting performance limits, consider using some kind of hierarchy, a Quadtree or an LOD system. For example, you can calculate an additional bounding rectangle for very complex polygons and only do the expensive in-check with the polygon if the point lies inside the rectangle.



来源:https://stackoverflow.com/questions/14581919/detecting-clicks-in-a-non-rectangular-shape

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