C# 给定矩形的4个点,忽略这4个点,是否有任何边相交?

C# 给定矩形的4个点,忽略这4个点,是否有任何边相交?,c#,geometry,emgucv,C#,Geometry,Emgucv,我正在摆弄一些EMGU的物体识别样本: Rectangle rect = modelImage.ROI; PointF p1 = new PointF(rect.Left, rect.Bottom); PointF p2 = new PointF(rect.Right, rect.Bottom); PointF p3 = new PointF(rect.Right, rect.Top); PointF p4 = new PointF(rect.Left, rect.Top); //check

我正在摆弄一些EMGU的物体识别样本:

Rectangle rect = modelImage.ROI;

PointF p1 = new PointF(rect.Left, rect.Bottom);
PointF p2 = new PointF(rect.Right, rect.Bottom);
PointF p3 = new PointF(rect.Right, rect.Top);
PointF p4 = new PointF(rect.Left, rect.Top);

//check if any opposite lines intersect
//if so, then don't add to final results
//we should never have 2 opposite sides intersecting
LineSegment2DF l1 = new LineSegment2DF(p1,p2);
LineSegment2DF l2 = new LineSegment2DF(p2, p3);
LineSegment2DF l3 = new LineSegment2DF(p3, p4);
LineSegment2DF l4 = new LineSegment2DF(p4, p1)

if (!(intersects(l1, l3) || intersects(l2, l4))) 
{
    //draw line
}
但是,我得到了一些可疑的结果,例如(灰色):

及(红色):

我也得到了一些其他的坏结果,但我注意到了这些的趋势。这些矩形(或技术上的梯形…?)有一些相互交叉或重叠的线条。如果是这样的话,我想忽略绘制这些结果。在得到4分的情况下,有没有办法确定这一点

更新:应user@Chris的请求,我退房。我试图复制伪代码。然而,我可能误解了这一点。它没有给出预期的结果。它似乎总是返回
true
。这可能是因为我把伪代码翻译错了

public static bool intersects(LineSegment2DF l1, LineSegment2DF l2)
{
    float x1 = l1.P1.X;
    float x2 = l1.P2.X;
    float x3 = l2.P1.X;
    float x4 = l2.P2.X;
    float y1 = l1.P1.Y;
    float y2 = l1.P2.Y;
    float y3 = l2.P1.Y;
    float y4 = l2.P2.Y;

    float intervalAMin = Math.Min(x1, x2);
    float intervalAMax = Math.Max(x1, x2);
    float intervalBMin = Math.Min(x3, x4);
    float intervalBMax = Math.Max(x3, x4);

    //if (Math.Max(l1.P1.X, l1.P2.X) < Math.Min(l2.P1.X, l2.P2.X)) return false;
    if(intervalAMax < intervalBMin) return false;

    float a1 = (y1-y2)/(x1-x2); // Pay attention to not dividing by zero
    float a2 = (y3-y4)/(x3-x4); // Pay attention to not dividing by zero
    if (a1 == a2) return false; // Parallel segments

    float b1 = y1-a1*x1;// = y2-a1*x2;
    float b2 = y3-a2*x3;// = y4-a2*x4;

    float xa = (b2 - b1) / (a1 - a2);// Once again, pay attention to not dividing by zero
    float ya = a1 * xa + b1;
    //float ya = a2 * xa + b2;

    if ((xa < Math.Max(Math.Min(x1, x2), Math.Min(x3, x4))) || (xa > Math.Min(Math.Max(x1, x2), Math.Max(x3, x4)))) return false; // intersection is out of bound
    return true;
}
公共静态布尔相交(LineSegment2DF l1、LineSegment2DF l2)
{
浮点x1=l1.P1.X;
浮点x2=l1.P2.X;
浮点x3=l2.P1.X;
浮点x4=l2.P2.X;
浮动y1=l1.P1.Y;
浮动y2=l1.P2.Y;
浮动y3=l2.P1.Y;
浮动y4=l2.P2.Y;
浮动区间数=数学最小值(x1,x2);
浮点间隔最大值=数学最大值(x1,x2);
浮动间隔bmin=数学最小值(x3,x4);
浮点间隔b最大=数学最大值(x3,x4);
//if(Math.Max(l1.P1.X,l1.P2.X)Math.Min(Math.Max(x1,x2,Math.Max(x3,x4)))返回false;//交点越界
返回true;
}
我发现了一个非常好的方法。我将其简化为:

public static bool Intersects2DF(LineSegment2DF thisLineSegment, LineSegment2DF otherLineSegment)
{
    float firstLineSlopeX, firstLineSlopeY, secondLineSlopeX, secondLineSlopeY;

    firstLineSlopeX = thisLineSegment.P2.X - thisLineSegment.P1.X;
    firstLineSlopeY = thisLineSegment.P2.Y - thisLineSegment.P1.Y;

    secondLineSlopeX = otherLineSegment.P2.X - otherLineSegment.P1.X;
    secondLineSlopeY = otherLineSegment.P2.Y - otherLineSegment.P1.Y;

    float s, t;
    s = (-firstLineSlopeY * (thisLineSegment.P1.X - otherLineSegment.P1.X) + firstLineSlopeX * (thisLineSegment.P1.Y - otherLineSegment.P1.Y)) / (-secondLineSlopeX * firstLineSlopeY + firstLineSlopeX * secondLineSlopeY);
    t = (secondLineSlopeX * (thisLineSegment.P1.Y - otherLineSegment.P1.Y) - secondLineSlopeY * (thisLineSegment.P1.X - otherLineSegment.P1.X)) / (-secondLineSlopeX * firstLineSlopeY + firstLineSlopeX * secondLineSlopeY);

    if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
    {
        // Collision detected
        return true;
    }
    return false; // No collision
}
public static bool Intersects2DF(LineSegment2DF thisLineSegment,LineSegment2DF otherLineSegment)
{
浮动firstLineSlopeX、firstLineSlopeY、secondLineSlopeX、secondLineSlopeY;
firstLineSlopeX=thisLineSegment.P2.X-thisLineSegment.P1.X;
firstLineSlopeY=thisLineSegment.P2.Y-thisLineSegment.P1.Y;
secondLineSlopeX=otherLineSegment.P2.X-otherLineSegment.P1.X;
secondLineSlopeY=otherLineSegment.P2.Y-otherLineSegment.P1.Y;
浮动s,t;
s=(-firstLineSlopeY*(thisLineSegment.P1.X-otherLineSegment.P1.X)+firstLineSlopeX*(thisLineSegment.P1.Y-otherLineSegment.P1.Y))/(-secondLineSlopeX*firstLineSlopeY+firstLineSlopeX*secondLineSlopeY);
t=(secondLineSlopeX*(thisLineSegment.P1.Y-otherLineSegment.P1.Y)-secondLineSlopeY*(thisLineSegment.P1.X-otherLineSegment.P1.X))/(-secondLineSlopeX*firstLineSlopeY+firstLineSlopeX*secondLineSlopeY);

如果(s>=0&&s=0&&t)可能会有帮助(你可以用它来测试相对的边,看看它们是否相交)。@Chris我试图按照检查过的答案,但对我无效(我发现这是因为我认为这一定是某个地方的一个已解决问题,所以只是在谷歌上搜索。我实际上没有遵循解决方案,目前没有时间通过它来找到问题。不过,如果我有空闲时间,我可能会稍后再讨论。祝你好运!@Chris我找到了一个可行的解决方案。哇