【问题标题】:Basic rectangle splitting基本矩形分割
【发布时间】:2011-10-14 00:42:41
【问题描述】:

我被一些琐碎的问题困住了,好吧,我想我需要帮助。

我有两个矩形,保证它们的 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 语句,我想如果这可以做得更好。

【问题讨论】:

  • 呃,你用的是什么语言,肯定不能用C#,java c++?另外,你试过什么?
  • 投票结束——问题太宽泛,编程语言标签太多。谁知道他指的是什么语言,或者语言是否重要。
  • @ChristopherCurrens 我编写了单独的 if 语句来确定我的情况是否属于 4 种可能的情况,然后使用单独的代码部分来确定矩形的坐标。但是,我怀疑,这可以很聪明地完成,甚至无需确定 4 种可能状态中的 1 种。
  • 对这个问题投反对票或投赞成票是愚蠢的。
  • 您可以发布您现在拥有的代码吗?

标签: algorithm language-agnostic


【解决方案1】:

矩形 1 的顶部和底部值与大矩形相同。矩形 2 的左右值与小矩形相同。我们只需要获取矩形 1 的左右值,以及矩形 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。

【讨论】:

    【解决方案2】:

    据我了解,您似乎需要一个 if(或 switch)语句来确定矩形的方向,然后从那里进行一些简单的加减:

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

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

    【讨论】:

      【解决方案3】:

      假设您有 RARB 作为您的输入,并且您使用的任何语言都有一个 Rectangle 类,这是一种使用 4 个 ifs、Math.Min、@987654326 的方法@ 和Math.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));
          }  
      }  
      

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

      【讨论】:

        猜你喜欢
        • 2018-06-26
        • 2015-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-09
        • 2011-08-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多