偏移矩形位置以防止重叠

时间:2017-05-17 08:14:00

标签: c# bitmap geometry gdi+ system.drawing

我有以下情况,其中矩形重叠。 enter image description here

我有交叉矩形的坐标(蓝色)。 我如何偏移2个矩形的起始坐标(左上角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);
}
}

UPDATE2:

根据@MBo的逻辑,我实现了上面的代码。这在大多数情况下都适用。但是在某些图像中,即使检测到内部区域并且添加了偏移,我也会重叠。请参阅下面的示例图像。

我试图解决的实际问题与这两个问题有关 Translating a Point(X,Y) for Images of Different Sizes from a Custom PictureBox Control

Performing Overlap Detection of 2 Bitmaps

请指教。

更新 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

2 个答案:

答案 0 :(得分:1)

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

    {
    "_id": "591c08ccb673741787b123c2",
    "bezeichnung": "test",
    "__v": 0,
    "risiken": [{
        "bezeichnung": "Naturgefahren",
        "kategorien": [{
            "_id": "5915ab9d19d0d027b870d259",
            "beschreibung": "-",
            "name": "Hochwasser"
        }, {
            "_id": "5915abd019d0d027b870d25a",
            "beschreibung": "-",
            "name": "Muren"
        }, {
            "_id": "5915abd719d0d027b870d25b",
            "beschreibung": "-",
            "name": "Oberflächenwasser"
        }]
    }]
}

Full code

临时参考的旧答案:

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

//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)

答案 1 :(得分:0)

你需要检查r2(rectangle2)是否在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);