c++ 快速矩形求交

jhkqcmku  于 2023-06-25  发布在  其他
关注(0)|答案(7)|浏览(193)

有什么快速的方法来测试两个矩形是否相交?
在互联网上搜索了这个一行字(哇!),但我不明白如何用JavaScript编写,它似乎是用C++的一种古老形式编写的。

  1. struct
  2. {
  3. LONG left;
  4. LONG top;
  5. LONG right;
  6. LONG bottom;
  7. } RECT;
  8. bool IntersectRect(const RECT * r1, const RECT * r2)
  9. {
  10. return ! ( r2->left > r1->right
  11. || r2->right < r1->left
  12. || r2->top > r1->bottom
  13. || r2->bottom < r1->top
  14. );
  15. }
5lhxktic

5lhxktic1#

这就是如何将代码转换为JavaScript。请注意,在您的代码中以及本文的代码中有一个错别字,正如注解所建议的那样。具体来说,r2->right left应该是r2->right < r1->leftr2->bottom top应该是r2->bottom < r1->top,函数才能正常工作。

  1. function intersectRect(r1, r2) {
  2. return !(r2.left > r1.right ||
  3. r2.right < r1.left ||
  4. r2.top > r1.bottom ||
  5. r2.bottom < r1.top);
  6. }
  7. // Test case:
  8. var rectA = {
  9. left: 10,
  10. top: 10,
  11. right: 30,
  12. bottom: 30
  13. };
  14. var rectB = {
  15. left: 20,
  16. top: 20,
  17. right: 50,
  18. bottom: 50
  19. };
  20. var rectC = {
  21. left: 70,
  22. top: 70,
  23. right: 90,
  24. bottom: 90
  25. };
  26. console.log(intersectRect(rectA, rectB)); // returns true
  27. console.log(intersectRect(rectA, rectC)); // returns false
展开查看全部
dnph8jn4

dnph8jn42#

  1. function intersect(a, b) {
  2. return (a.left <= b.right &&
  3. b.left <= a.right &&
  4. a.top <= b.bottom &&
  5. b.top <= a.bottom)
  6. }

这假设top通常小于bottom(即y坐标向下增加)。

8mmmxcuj

8mmmxcuj3#

这就是. NETFramework实现Rectangle.Intersect的方式

  1. public bool IntersectsWith(Rectangle rect)
  2. {
  3. if (rect.X < this.X + this.Width && this.X < rect.X + rect.Width && rect.Y < this.Y + this.Height)
  4. return this.Y < rect.Y + rect.Height;
  5. else
  6. return false;
  7. }

静态版本:

  1. public static Rectangle Intersect(Rectangle a, Rectangle b)
  2. {
  3. int x = Math.Max(a.X, b.X);
  4. int num1 = Math.Min(a.X + a.Width, b.X + b.Width);
  5. int y = Math.Max(a.Y, b.Y);
  6. int num2 = Math.Min(a.Y + a.Height, b.Y + b.Height);
  7. if (num1 >= x && num2 >= y)
  8. return new Rectangle(x, y, num1 - x, num2 - y);
  9. else
  10. return Rectangle.Empty;
  11. }
展开查看全部
rvpgvaaj

rvpgvaaj4#

另一个更简单的方法。(假设y轴向下增加)。

  1. function intersect(a, b) {
  2. return Math.max(a.left, b.left) < Math.min(a.right, b.right) &&
  3. Math.max(a.top, b.top) < Math.min(a.bottom, b.bottom);
  4. }

上述条件中的4个数字(max和min)也给予了交点。

dgiusagp

dgiusagp5#

它有一个Rect类型可以使用。它已经是JavaScript了。
https://dxr.mozilla.org/mozilla-beta/source/toolkit/modules/Geometry.jsm

91zkwejq

91zkwejq6#

我使用了混合的方法,在一个大矩形中检测一个较小的矩形。这是一种节点方法,并且使用宽度/高度,但是可以很容易地适应。

  1. isIntersectingRect: function (r1, r2) {
  2. var quickCheck = (r1.x <= r2.x + r2.w &&
  3. r2.x <= r1.x + r1.w &&
  4. r1.y <= r2.y + r2.h &&
  5. r2.y <= r1.y + r1.h)
  6. if (quickCheck) return true;
  7. var x_overlap = Math.max(0, Math.min(r1.x + r1.w, r2.x + r2.w) - Math.max(r1.x, r2.x));
  8. var y_overlap = Math.max(0, Math.min(r1.y + r1.h, r2.y + r2.h) - Math.max(r1.y, r2.y));
  9. var overlapArea = x_overlap * y_overlap;
  10. return overlapArea == 0;
  11. }
vsikbqxv

vsikbqxv7#

当前的.NET方式很简单

  1. public bool IsEmpty => _width < 0.0;
  2. public bool IntersectsWith(Rect rect)
  3. {
  4. if (IsEmpty || rect.IsEmpty)
  5. {
  6. return false;
  7. }
  8. if (rect.Left <= Right && rect.Right >= Left && rect.Top <= Bottom)
  9. {
  10. return rect.Bottom >= Top;
  11. }
  12. return false;
  13. }
展开查看全部

相关问题