如何使 rect.intersects 仅检查外部边界

发布于 2024-12-26 15:03:48 字数 250 浏览 5 评论 0原文

我正在尝试确定画布上单击鼠标创建的矩形 clickRectangle 是否与 DrawingRectangle 相交。

但是,我不希望 intersects 方法返回 true,如果我单击latedRectangle 中的任何位置(即矩形的内部),我只希望它在单击drawnRectangle 的外部边界时返回 true。

我怎样才能做到这一点?

PS:对于clickRectangle和drawnRectangle请参阅下面我的评论。

I am trying to determine if clickRectangle, a rectangle created by the click of a mouse, on a canvas intersects with a drawnRectangle or not.

However, I don't want the intersects method to return true if I click anywhere in the drawnRectangle (i.e. the interior of the rectangle), I just want it to return true if an outer boundary of drawnRectangle was clicked.

How can I do that?

P.S: For clickRectangle and drawnRectangle see my comment below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桜花祭 2025-01-02 15:03:48

如果我正确理解这个问题,你想要

public static boolean pointNearEdge(Point click, Rectangle drawnRectangle, int howNear){
   Rectangle clickRect = new Rectangle(click.x-howNear, click.y-howNear, howNear*2, howNear*2);
   if (drawnRectangle.contains(clickRect))  // totally inside -> false
      return false;

   // test if there is a partial intersection - i.e. we are near the edge
   return drawnRectangle.intersects(clickRect);
}

If I understand the question correctly, you want

public static boolean pointNearEdge(Point click, Rectangle drawnRectangle, int howNear){
   Rectangle clickRect = new Rectangle(click.x-howNear, click.y-howNear, howNear*2, howNear*2);
   if (drawnRectangle.contains(clickRect))  // totally inside -> false
      return false;

   // test if there is a partial intersection - i.e. we are near the edge
   return drawnRectangle.intersects(clickRect);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文