gpt4 book ai didi

algorithm - 检测矩形(按钮)是否在 x 或 y 轴上重叠

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:19:02 25 4
gpt4 key购买 nike

我目前正尝试在 android 中开发一个原型(prototype),用户可以在屏幕上拖动 4 个单独的按钮。

我遇到的问题是碰撞检测。例如,如果其中一个按钮与另一个按钮并排,则只允许沿 Y 轴移动。同样,如果其中一个按钮接触到另一个按钮的顶部或底部,则只允许沿 X 轴移动。

有点像

for (TouchButton t:myButtons)
{
if(!(t.equals(b)))
{
if((b.getY() >= t.getY() && (b.getY() <= (t.getY() + t.getMeasuredHeight()))))
{
if((b.getX() >= t.getX() && (b.getX() <= (t.getX() + t.getMeasuredWidth()))))
{
//dont move
}
}
}

应该能够检测按钮是否在两个轴上接触?但是我如何确定是否可以向上滑动或滑过对象?

Example

最佳答案

这是严格的版本:

boolean areOverlapping (TouchButton a, TouchButton b)
{
return (b.getY() >= a.getY()
&& (b.getY() <= (a.getY() + a.getMeasuredHeight()))
&& b.getX() >= a.getX()
&& (b.getX() <= (a.getX() + a.getMeasuredWidth()))));
}

在此版本中,允许按轴触摸:

boolean areOverlappingButTouchIsAllowed (TouchButton a, TouchButton b)
{
return (b.getY() > a.getY()
&& (b.getY() < (a.getY() + a.getMeasuredHeight()))
&& b.getX() > a.getX()
&& (b.getX() < (a.getX() + a.getMeasuredWidth()))));
}

您还可以检查他们是否触摸:

boolean areTouching (TouchButton a, TouchButton b)
{
return ((b.getY() == (a.getY() + a.getMeasuredHeight()))
|| (a.getY() == (b.getY() + b.getMeasuredHeight()))
|| (b.getX() == (a.getX() + a.getMeasuredWidth())))
|| (a.getX() == (b.getX() + b.getMeasuredWidth()))));
}

然后

for (TouchButton t : myButtons)
{
if (!t.equals(b))
{
if (areOverlappingButTouchIsAllowed(b,t))
{
// overlapping beside borders
}
else if (areTouching(b,t))
{
// touching; overlapping borders only
}
}
}

关于algorithm - 检测矩形(按钮)是否在 x 或 y 轴上重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18754722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com