.NET屏幕对象触摸检测

时间:2009-12-16 00:52:39

标签: c# .net winforms

如何检测屏幕(表格)上的两个图像是否在C#中相互接触?

我有一个小游戏,我必须找出两个物体(物体的图像)是否相互接触。

有没有一种简单的方法来实现它?

3 个答案:

答案 0 :(得分:3)

快速的方法是检查边界矩形:

if (a.left <= b.right && b.left <= a.right &&
    a.top <= b.bottom && b.top <= a.bottom)

如果你想对涉及透明度的东西进行像素完美检测,那就会变得更加复杂。

答案 1 :(得分:1)

最快的方法是将每个对象视为一个半径通常接近其周长的圆,以及其中心的X,Y坐标对。然后计算两个物体中心之间的距离,并将其与半径之和进行比较。如果它们的半径之和大于它们中心之间的距离,则它们会接触。

答案 2 :(得分:0)

将它们视为矩形,您可以考虑获取每个图像的每个点的点坐标。然后检查每个点是否大于另一个图像中的另一个点(点)。

类似的东西:

/// each point represent the points in the images
Point ImageAUpperLeft; 
Point ImageAUpperRight; 
Point ImageALowerLeft; 
Point ImageALowerRight; 
Point ImageBUpperLeft; 
Point ImageBUpperRight; 
Point ImageBLowerLeft; 
Point ImageBLowerRight;
Point[] PtsList = new Point[] { ImageAUpperLeft, ImageAUpperRight, ImageALowerLeft, ImageALowerRight, ImageBUpperLeft, ImageBUpperRight, ImageBLowerLeft, ImageBLowerRight }; 

/// perform checking here, like:
if (ImageAUpperLeft coincides with ImageBUpperLeft) || (... so on so forth

我想你知道继续这个的算法,但是如果它仍然不清楚你仍然可以在这里发表你的问题作为评论。很乐意帮助...:)