基本矩形分割

时间:2011-10-14 00:42:42

标签: algorithm language-agnostic

我遇到了一些微不足道的问题,好吧,我想我需要帮助。

enter image description here

我有两个矩形,它保证了它们的4个基点(图片的上半部分)有一个共同点。它也保证它们是轴对齐的。

我知道这个共同点(也可以很容易推断),尺寸和这些矩形的坐标。


现在,我需要检索名为12的矩形的坐标,我正在寻找一种简单的方法(更低)图片的一部分)

我当前的实现依赖于许多if语句,我怀疑我太愚蠢而无法找到更好的方法。

谢谢。

更新:我当前的实施。

Point commonPoint = getCommonPoint(bigRectangle, smallRectangle);

rectangle2 = new Rectangle(smallRectangle.getAdjacentVerticalPoint(commonPoint),
                           bigRectangle.getOppositePoint(commonPoint));

rectangle1 = new Rectangle(smallRectangle.getAdjacentHorizontalPoint(commonPoint)
                           bigRectangle.getOppositePoint(commonPoint));

// Now simply adjust one of these rectangles to remove the overlap,
// it's trivial - we take the 'opposite' points for 'small' and 'big'
// rectangles and then use their absolute coordinate difference as
// a fix for either width of 'rectangle2' or height of 'rectangle1'
// (in this situation it's going to be width).

adjustRectangle(rectangle2);

这是重构的,但方法getCommonPointgetAdjacent...以及getOpposite仍然有很多if语句,我想如果可以做得更好。

3 个答案:

答案 0 :(得分:1)

根据我的理解,似乎你需要有一个if(或switch)语句来确定矩形的方向,从那里它只是一些简单的加法和减法:

如果你知道内部蓝色矩形的坐标(以及整个矩形的尺寸),那么找到其他的应该没问题。其中一个R1和R2点始终相同:等于相邻的蓝色矩形点。而其他人只是一个小数学。

似乎你无法摆脱最初的if / switch语句。如果矩形只能向上或向下,那么你可以让偏移为负或正,但它也可以是左或右......所以你可能会被卡在那里。您可以为垂直或水平状态设置 - / +偏移量,但是您必须对每个计算进行检查

答案 1 :(得分:1)

假设您输入了RARB,并且您正在使用的语言中包含Rectangle类,则可以使用4 if这样做s,Math.MinMath.MaxMath.Abs

Rectangle r1, r2; // Note - Rectangle constructor: new Rectangle(X, Y, Width, Height)
if (RA.X = RB.X) {  
    r1 = new Rectangle(Math.Min(RA.Right, RB.Right), Math.Min(RA.Y, RB.Y), Math.Abs(RA.Width - RB.Width), Math.Max(RA.Height, RB.Height));  
    if (RA.Y = RB.Y) {
        // Intersects Top Left
        r2 = new Rectangle(RA.X, Math.Min(RA.Bottom, RB.Bottom), Math.Min(RA.Width, RB.Width), Math.Abs(RA.Height - RB.Height));  
    } else {  
        // Intersects Bottom Left
        r2 = new Rectangle(RA.X, Math.Max(RA.Bottom, RB.Bottom), Math.Min(RA.Width, RB.Width), Math.Abs(RA.Height - RB.Height));
    }  
} else {  
    r1 = new Rectangle(Math.Min(RA.X, RB.X), Math.Min(RA.Y, RB.Y), Math.Abs(RA.Width - RB.Width), Math.Max(RA.Height, RB.Height));
    if (RA.Y = RB.Y) {  
        // Intersects Top Right
        r2 = new Rectangle(Math.Max(RA.X, RB.X), Math.Min(RA.Bottom, RB.Bottom), Math.Min(RA.Width, RB.Width), Math.Abs(RA.Height - RB.Height));
    } else {  
        // Intersects Bottom Right
        r2 = new Rectangle(Math.Max(RA.X, RB.X), Math.Min(RA.X, RA.Y), Math.Min(RA.Width, RB.Width), Math.Abs(RA.Height - RB.Height));
    }  
}  

这段代码是用记事本写的,所以它可能有一两个错字,但逻辑是合理的。

答案 2 :(得分:1)

矩形1的顶部和底部值与大矩形相同。矩形2的左右值与小矩形相同。我们只需要获取矩形1的左右值,以及矩形2的顶部和底部值。所以我们只有2个简单的if语句:

if (bigRectangle.Left == smallRectangle.Left) 
    left = smallRectangle.Right
    right = bigRectangle.Right
else
    left = bigRectangle.Left
    right = smallRectangle.Left
rectangle1 = new Rectangle(left, bigRectangle.Top, right - left, bigRectangle.Height)

if (bigRectangle.Top == smallRectangle.Top)
    top = smallRectangle.Bottom
    bottom = bigRectangle.Bottom
else
    top = bigRectangle.Top
    bottom = smallRectangle.Top
rectangle2 = new Rectangle(smallRectangle.Left, top, smallRectangle.Width, bottom - top)

在上面,Rectangle构造函数作为输入:left,top,width,height。