【问题标题】:How to know if a line intersects a rectangle如何知道一条线是否与矩形相交
【发布时间】:2018-04-06 14:04:33
【问题描述】:

我已经检查过这个问题,但答案对我来说非常大:

How to know if a line intersects a plane in C#? - Basic 2D geometry

是否有任何 .NET 方法可以知道由两点定义的线是否与矩形相交?

public bool Intersects(Point a, Point b, Rectangle r)
{
   // return true if the line intersects the rectangle
   // false otherwise
}

提前致谢。

【问题讨论】:

标签: c# .net winforms drawing drawing2d


【解决方案1】:
    public static bool LineIntersectsRect(Point p1, Point p2, Rectangle r)
    {
        return LineIntersectsLine(p1, p2, new Point(r.X, r.Y), new Point(r.X + r.Width, r.Y)) ||
               LineIntersectsLine(p1, p2, new Point(r.X + r.Width, r.Y), new Point(r.X + r.Width, r.Y + r.Height)) ||
               LineIntersectsLine(p1, p2, new Point(r.X + r.Width, r.Y + r.Height), new Point(r.X, r.Y + r.Height)) ||
               LineIntersectsLine(p1, p2, new Point(r.X, r.Y + r.Height), new Point(r.X, r.Y)) ||
               (r.Contains(p1) && r.Contains(p2));
    }

    private static bool LineIntersectsLine(Point l1p1, Point l1p2, Point l2p1, Point l2p2)
    {
        float q = (l1p1.Y - l2p1.Y) * (l2p2.X - l2p1.X) - (l1p1.X - l2p1.X) * (l2p2.Y - l2p1.Y);
        float d = (l1p2.X - l1p1.X) * (l2p2.Y - l2p1.Y) - (l1p2.Y - l1p1.Y) * (l2p2.X - l2p1.X);

        if( d == 0 )
        {
            return false;
        }

        float r = q / d;

        q = (l1p1.Y - l2p1.Y) * (l1p2.X - l1p1.X) - (l1p1.X - l2p1.X) * (l1p2.Y - l1p1.Y);
        float s = q / d;

        if( r < 0 || r > 1 || s < 0 || s > 1 )
        {
            return false;
        }

        return true;
    }

【讨论】:

  • @Habjan:谢谢,简单而具体。最佳答案。是否经过测试?
  • @Daniel Peñalba:我做了几个测试 :-)
  • @HABJAN:感谢您的更新。我认为条件是 (r.Contains(p1) || r.Contains(p2))。如果任何点在矩形内,则线相交。
  • 有人能解释一下实现吗,不是很清楚...谢谢
  • @Marko:我同意,变量命名尤其让人不清楚。但是一个简单的算法(假设你的矩形有水平线和垂直线)是:获取垂直线的 x 值,检查每个 x 处的线的 y 值。然后获取水平线的 y 值。如果两个线的 y 值都大于顶部矩形 y,或者都小于底部矩形 y,那么你的线不会相交。否则,它会。
【解决方案2】:

很遗憾,错误的答案被投了赞成票。计算实际的交点非常昂贵,您只需要比较。要查找的关键字是“Line Clipping”(http://en.wikipedia.org/wiki/Line_clipping)。当您想要快速拒绝时,维基百科推荐 Cohen-Sutherland 算法 (http://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland),这可能是最常见的情况。维基百科页面上有一个 C++ 实现。如果您对实际剪裁线不感兴趣,则可以跳过大部分内容。 @Johann 的答案看起来和那个算法很像,但是我没有仔细看。

【讨论】:

  • 很好的答案。是的,Johann 的代码看起来好多了。谢谢你的解释。
【解决方案3】:

蛮力算法...

首先检查矩形是在线端点的左侧还是右侧:

  • 建立线端点的最左边和最右边的X值:XMIN和XMAX
  • 如果 Rect.Left > XMAX,则没有交集。
  • 如果 Rect.Right

然后,如果以上不足以排除相交,请检查矩形是在线端点上方还是下方:

  • 确定线端点的最高和最低 Y 值:YMAX 和 YMIN
  • 如果 Rect.Bottom > YMAX,则没有交集。
  • 如果 Rect.Top

然后,如果以上还不足以排除相交,您需要检查直线方程y = m * x + b,看看矩形是否在直线上方:

  • 在 Rect.Left 和 Rect.Right 处建立直线的 Y 值:LINEYRECTLEFT 和 LINEYRECTRIGHT
  • 如果 Rect.Bottom > LINEYRECTRIGHT && Rect.Bottom > LINEYRECTLEFT,则没有交集。

然后,如果以上还不足以排除相交,则需要检查矩形是否在该线下方:

  • 如果 Rect.Top

那么,如果你到了这里:

  • 十字路口。

注意我确信有一个更优雅的代数解决方案,但是用笔和纸以几何方式执行这些步骤很容易遵循。

一些未经测试和未编译的代码:

public struct Line
{
    public int XMin { get { ... } }
    public int XMax { get { ... } }

    public int YMin { get { ... } }
    public int YMax { get { ... } }

    public Line(Point a, Point b) { ... }

    public float CalculateYForX(int x) { ... }
}

public bool Intersects(Point a, Point b, Rectangle r)
{
    var line = new Line(a, b);

    if (r.Left > line.XMax || r.Right < line.XMin)
    {
        return false;
    }

    if (r.Top < line.YMin || r.Bottom > line.YMax)
    {
        return false;
    }

    var yAtRectLeft = line.CalculateYForX(r.Left);
    var yAtRectRight = line.CalculateYForX(r.Right);

    if (r.Bottom > yAtRectLeft && r.Bottom > yAtRectRight)
    {
        return false;
    }

    if (r.Top < yAtRectLeft && r.Top < yAtRectRight)
    {
        return false;
    }

    return true;
}

【讨论】:

  • 正如 JPE 在他的回答中提到的那样,您的代码比赞成的答案要好得多。
  • 这个答案似乎是针对一条线段,而不是一条(无限)线。
  • @naught101 - 这是要求的。
  • 问题没有提到线段。两点也可以定义一条实际的线。但是,是的,这个问题有点模棱两可。
  • 这个答案的一个问题是,尽管对于 X,我们几乎可以肯定地期望 X 会向右增加,但对于 Y,这不太确定。在某些情况下,Y 会向下增加,在其他情况下,Y 会像本答案中假设的那样向上增加。
【解决方案4】:

这段代码性能更好:

    public static bool SegmentIntersectRectangle(
        double rectangleMinX,
        double rectangleMinY,
        double rectangleMaxX,
        double rectangleMaxY,
        double p1X,
        double p1Y,
        double p2X,
        double p2Y)
    {
        // Find min and max X for the segment
        double minX = p1X;
        double maxX = p2X;

        if (p1X > p2X)
        {
            minX = p2X;
            maxX = p1X;
        }

        // Find the intersection of the segment's and rectangle's x-projections
        if (maxX > rectangleMaxX)
        {
            maxX = rectangleMaxX;
        }

        if (minX < rectangleMinX)
        {
            minX = rectangleMinX;
        }

        if (minX > maxX) // If their projections do not intersect return false
        {
            return false;
        }

        // Find corresponding min and max Y for min and max X we found before
        double minY = p1Y;
        double maxY = p2Y;

        double dx = p2X - p1X;

        if (Math.Abs(dx) > 0.0000001)
        {
            double a = (p2Y - p1Y)/dx;
            double b = p1Y - a*p1X;
            minY = a*minX + b;
            maxY = a*maxX + b;
        }

        if (minY > maxY)
        {
            double tmp = maxY;
            maxY = minY;
            minY = tmp;
        }

        // Find the intersection of the segment's and rectangle's y-projections
        if (maxY > rectangleMaxY)
        {
            maxY = rectangleMaxY;
        }

        if (minY < rectangleMinY)
        {
            minY = rectangleMinY;
        }

        if (minY > maxY) // If Y-projections do not intersect return false
        {
            return false;
        }

        return true;
    }

您也可以在 JS 演示中查看它是如何工作的:http://jsfiddle.net/77eej/2/

如果你有两个 Points 和 Rect 你可以这样调用这个函数:

    public static bool LineIntersectsRect(Point p1, Point p2, Rect r)
    {
        return SegmentIntersectRectangle(r.X, r.Y, r.X + r.Width, r.Y + r.Height, p1.X, p1.Y, p2.X, p2.Y);
    }

【讨论】:

    【解决方案5】:

    我采用了 HABJAN 的解决方案,它运行良好,并将其转换为 Objective-C。 Objective-C代码如下:

    bool LineIntersectsLine(CGPoint l1p1, CGPoint l1p2, CGPoint l2p1, CGPoint l2p2)
    {
        CGFloat q = (l1p1.y - l2p1.y) * (l2p2.x - l2p1.x) - (l1p1.x - l2p1.x) * (l2p2.y - l2p1.y);
        CGFloat d = (l1p2.x - l1p1.x) * (l2p2.y - l2p1.y) - (l1p2.y - l1p1.y) * (l2p2.x - l2p1.x);
    
        if( d == 0 )
        {
            return false;
        }
    
        float r = q / d;
    
        q = (l1p1.y - l2p1.y) * (l1p2.x - l1p1.x) - (l1p1.x - l2p1.x) * (l1p2.y - l1p1.y);
        float s = q / d;
    
        if( r < 0 || r > 1 || s < 0 || s > 1 )
        {
            return false;
        }
    
        return true;
    }
    
    bool LineIntersectsRect(CGPoint p1, CGPoint p2, CGRect r)
    {
        return LineIntersectsLine(p1, p2, CGPointMake(r.origin.x, r.origin.y), CGPointMake(r.origin.x + r.size.width, r.origin.y)) ||
        LineIntersectsLine(p1, p2, CGPointMake(r.origin.x + r.size.width, r.origin.y), CGPointMake(r.origin.x + r.size.width, r.origin.y + r.size.height)) ||
        LineIntersectsLine(p1, p2, CGPointMake(r.origin.x + r.size.width, r.origin.y + r.size.height), CGPointMake(r.origin.x, r.origin.y + r.size.height)) ||
        LineIntersectsLine(p1, p2, CGPointMake(r.origin.x, r.origin.y + r.size.height), CGPointMake(r.origin.x, r.origin.y)) ||
        (CGRectContainsPoint(r, p1) && CGRectContainsPoint(r, p2));
    }
    

    非常感谢HABJAN。我会注意到,起初我编写了自己的例程来检查渐变中的每个点,并且我尽我所能来最大限度地提高性能,但这马上要快得多。

    【讨论】:

    • 感谢 Objective-C 各位!
    【解决方案6】:

    没有简单的预定义 .NET 方法可以调用来完成此操作。然而,使用 Win32 API,有一个非常简单的方法来做到这一点(在实现意义上很容易,性能不是强项):LineDDA

    BOOL LineDDA(int nXStart,int nYStart,int nXEnd,int nYEnd,LINEDDAPROC lpLineFunc,LPARAM lpData)
    

    此函数为要绘制的线的每个像素调用回调函数。在此函数中,您可以检查像素是否在矩形内 - 如果找到,则它相交。

    正如我所说,这不是最快的解决方案,但很容易实现。要在 C# 中使用它,您当然需要从 gdi32.dll ddlimport。

    [DllImport("gdi32.dll")] public static extern int LineDDA(int n1,int n2,int n3,int n4,int lpLineDDAProc,int lParam);
    

    【讨论】:

      【解决方案7】:

      最简单的计算几何技术是遍历多边形的各个部分,看看它是否与其中任何一个相交,因为它也必须与多边形相交。

      这种方法(以及大部分 CG)的唯一警告是我们必须小心边缘情况。如果线在某个点穿过矩形怎么办 - 我们是否将其视为交叉点?实施时要小心。

      编辑:线相交线段计算的典型工具是LeftOf(Ray, Point) 测试,如果点位于光线左侧,则返回。给定一条线l(我们将其用作射线)和一个包含点a 和b 的线段,如果一个点在左边而一个点不在,则该线与该线段相交:

      (LeftOf(l,a) && !LeftOf(l,b)) || (LeftOf(l,b) && !LeftOf(l,a))
      

      同样,当点在线时,您需要注意边缘情况,但这取决于您希望如何实际定义交点。

      【讨论】:

      • 如果你与多边形的任何段相交,那么你一定与多边形相交吗?穿过矩形一侧但未到达另一侧的线仍与矩形相交...
      • 但它是一个行。它不可能穿过一段然后停在一个矩形的中间。您正在考虑一个细分市场。我想我可能误解了这个问题,但是......
      • 我认为您正在描述一种算法,该算法通过计算多边形的边缘与从相关点到已知外部点的线的交点来确定一个点是否包含在多边形内多边形。
      • 我将一条线定义为两点的直线连接。您是否以经典几何方式定义一条线(即无限期?)。
      • 如果它是一条没有端点的线,那么交叉点的数量永远不会是奇数。
      【解决方案8】:

      对于 Unity(反转 y!)。这可以解决此处其他方法存在的除零问题:

      using System;
      using UnityEngine;
      
      namespace Util {
          public static class Math2D {
      
              public static bool Intersects(Vector2 a, Vector2 b, Rect r) {
                  var minX = Math.Min(a.x, b.x);
                  var maxX = Math.Max(a.x, b.x);
                  var minY = Math.Min(a.y, b.y);
                  var maxY = Math.Max(a.y, b.y);
      
                  if (r.xMin > maxX || r.xMax < minX) {
                      return false;
                  }
      
                  if (r.yMin > maxY || r.yMax < minY) {
                      return false;
                  }
      
                  if (r.xMin < minX && maxX < r.xMax) {
                      return true;
                  }
      
                  if (r.yMin < minY && maxY < r.yMax) {
                      return true;
                  }
      
                  Func<float, float> yForX = x => a.y - (x - a.x) * ((a.y - b.y) / (b.x - a.x));
      
                  var yAtRectLeft = yForX(r.xMin);
                  var yAtRectRight = yForX(r.xMax);
      
                  if (r.yMax < yAtRectLeft && r.yMax < yAtRectRight) {
                      return false;
                  }
      
                  if (r.yMin > yAtRectLeft && r.yMin > yAtRectRight) {
                      return false;
                  }
      
                  return true;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2010-09-07
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 2022-10-04
        • 1970-01-01
        • 2011-04-14
        • 1970-01-01
        相关资源
        最近更新 更多