避免重叠的方法:矩形位置偏移

3
我有以下情况,其中矩形重叠。 enter image description here 我有交集矩形(蓝色)的坐标。 如果我有交集矩形的坐标,如何偏移两个矩形的起始坐标(左上角X-Y),以防止重叠。
rectangle3 = Rectangle.Intersect(rectangle1, rectangle2);

更新:

根据 @MBo 的更新,我已经添加了以下代码。

Blue = Rectangle.Intersect(First, Second);
if (Blue != Rectangle.Empty)
{
if (First.Right == Blue.Right)
{

imgpoint.X += (Blue.Right - Blue.Left);
}

if (First.Bottom == Blue.Bottom)
{
imgpoint.Y += (Blue.Bottom - Blue.Top);
}

if (First.Left == Blue.Left)
{
imgpoint.X-= (Blue.Right - Blue.Left);
}
if (First.Top == Blue.Top)
{
imgpoint.Y -= (Blue.Bottom - Blue.Top);
}
}

更新2:

根据@MBo的逻辑,我实现了上面的代码。在大多数情况下,这个方法是有效的。但是,在某些图像中,即使检测到交叉并添加了偏移量,仍会出现重叠。请参见下面的示例图像。

我正在尝试解决的实际问题与以下两个问题相关: 从自定义PictureBox控件缩放不同大小的图像的点(X,Y)的转换

执行2个位图的重叠检测

请给予建议。

更新:enter image description here

更新:

根据您的更新,我已将Delphi代码翻译成了C#。

Blue = Rectangle.Intersect(First, Second);
if (Blue != Rectangle.Empty)
{
int dcy,dcx;
int dy,dx;
int fl=First.Left,fw=First.Width,fh=First.Height,ft=First.Top,fb=First.Bottom,fr=First.Right;
int sl=Second.Left,sr=Second.Right,st=Second.Top,sb=Second.Bottom,sw=Second.Width,sh=Second.Height;
dcy = (sb + st) - (fb + ft);  //doubled center y-difference
dcx = (sl + sr) - (fl + fr);
if ((int)(dcx) >= (fw + sw) || ((int)(dcy) >= (fh + sh)))
{//no intersection

}
else
{

dx = fw + sw - (int)dcx;  //doubled  needed x-shift absolute value
dy = fh + sh - (int)dcy;

if (dx > dy)
{
if (dcy < 0)
{
dy = - dy/2;
}
else
{
dy = dy/ 2;  //needed y-shift accounting for direction
}
dx = 0;
}
else 
{
if (dcy < 0)
{
dx =  - dx/ 2;
}
else
{
dx =  dx/2;
dy = 0;
}
}
imgpoint.X+=dx;
imgpoint.Y+=dy;
}
} 

使用这段代码时,第二个图像在某些情况下会移动得太远。请告诉我这段代码是否正确。

enter image description here


你在两个地方使用了(int)(dcx)dcy,需要使用绝对值(类似于math.abs)的地方。 - MBo
@MBo 谢谢。我会检查并回复。 - techno
@MBo 非常感谢 :) 在使用 Math.Abs 后,你的第二次更新非常好用。真的非常感谢你的努力。 - techno
2个回答

2

编辑:检查了所有可能的情况,得到了以下简单的代码(Delphi代码,可用)。

//fl, fw, fr, ft, fh, fb: First.Left, Width, Right, Top, Height, Bottom
//s* - similar parameters for the second rectangle   


dcy := (sb + st) - (fb + ft);  //doubled center y-difference
dcx := (sl + sr) - (fl + fr);
if (Abs(dcx) >= fw + sw) or ((Abs(dcy) >= fh + sh)) then  //no intersection
  Exit;

dx := fw + sw - Abs(dcx);  //doubled  needed x-shift absolute value
dy := fh + sh - Abs(dcy);

 if dx > dy then begin
   if dcy < 0 then
      dy := - dy div 2
   else
      dy := dy div 2;  //needed y-shift accounting for direction
   dx := 0;
 end else begin
   if dcy < 0 then
      dx :=  - dx div 2
   else
      dx :=  dx div 2;
   dy := 0;
 end;

//Result: dx, dy pair to shift the second rectangle

完整代码


旧答案,仅供参考:

您需要获取信息-第一个矩形的哪个坐标与相应坐标的交集矩形重合。对于简单的交集情况:

case
   First.Right = Blue.Right:   
      shift Second.Left by (Blue.Right - Blue.Left)
   First.Bottom = Blue.Bottom:
      shift Second.Top by (Blue.Bottom - Blue.Top)
   First.Left = Blue.Left:
      shift Second.Left by  -(Blue.Right - Blue.Left)
   First.Top = Blue.Top:
      shift Second.Top by -(Blue.Bottom - Blue.Top)

编辑:
如果存在交集且以上情况均不满足,则发生完全包含和十字形交叉。因此,请确定避免重叠的最短路径:

 dcy = (Second.Bottom + Second.Top) - (First.Bottom + First.Top)   
 if dcy >=0 then
      shift Second.Top by (First.Bottom - Second.Top)   //down
 else
      shift Second.Top by -(Second.Bottom - First.Top)   //up

 dcx = (Second.Left + Second.Right) - (First.Left + First.Right)   
 if dcx >=0 then
      shift Second.Left by (First.Right - Second.Left)   //right
 else
      shift Second.Left by -(Second.Right - First.Left)   //left

谢谢..但如果Blue有两个相交的边怎么办呢? - techno
我已经在我的代码中实现了你的逻辑,它运行得非常好。请检查我的更新并查看是否有什么问题。 - techno
看起来没问题。(如果您不需要完全包含和类似交叉的交集情况) - MBo
请看我的添加。 - MBo
我已经看到了你的加法。存在一种情况,可以找到交集,并且满足所有4个if条件。我已将您的逻辑实现为“if”条件语句,而不是“switch case”。因此,由于命中了所有4个“if”块,您的新逻辑何时适用?请查看更新。请给予建议。 - techno
显示剩余5条评论

0

你需要检查矩形r2是否在r2.X和r2.X + r2.Height之间或者在r2.Y和r2.Y + r2.Y + r2.Width之间,然后进行修改。我写了一段示例代码

private int GetNewX(Rectangle r1, Rectangle r2)
{
   if (r2.X < r1.X)
   {
       return r1.X - r2.Width;
   }
   else
   {
       return r1.X + r1.Width;
   }
}
private int GetNewY(Rectangle r1, Rectangle r2)
{
   if (r2.Y < r1.Y)
   {
       return r1.Y - r2.Height;
   }
   else
   {
       return r1.Y + r1.Height;
   }
}

Pen pen = new Pen(Color.Black);
Rectangle r1 = new Rectangle(60, 10, 200, 200);
Rectangle r2 = new Rectangle(40, 25, 200, 160);


//If overlapped change X,Y of the r2 rectangle
Rectangle overlapRect = Rectangle.Intersect(r1, r2);
if (overlapRect.Width > 0 || overlapRect.Height > 0)
{
    bool betweenX = overlapRect.X >= r1.X && overlapRect.X <= (r1.X + r1.Height);
    bool betweenY = overlapRect.Y >= r1.Y && overlapRect.Y <= (r1.Y + r1.Width);

    if (betweenX)
    {
        r2.X = GetNewX(r1,r2);                    
    }
    else if (betweenY)
    {
        r2.Y = GetNewY(r1, r2);
    }
    else
    {
        if (overlapRect.Width <= overlapRect.Height)
        {
            r2.X = GetNewX(r1, r2);
        }
        else
        {
            r2.Y = GetNewY(r1, r2);
        }
    }                
}

Graphics g = this.CreateGraphics();
g.DrawRectangle(pen, r1);
g.DrawRectangle(pen, r2);

欢迎,@techno。如果您有任何问题,请回来。请注意,我在一个表单上使用了我的代码。 - user2585076
你的意思是你已经在你的表单上测试了这段代码吗? - techno
好的,谢谢。我会在我的用例中测试并回复您。我实际上想通过这个问题解决的是这个问题 http://stackoverflow.com/questions/44018201/performing-overlap-detection-of-2-bitmaps - techno
@techno 如果你有两个位图坐标和它们的宽度和高度,你可以使用相同的技术。如果你无法解决,请写信给我。如果我能帮忙,我会的。 - user2585076
@techno 我为你创建了一个示例表单,可以接受2个不重叠的图像。请参考答案http://stackoverflow.com/questions/44018201/performing-overlap-detection-of-2-bitmaps。 - user2585076
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接