【发布时间】:2011-10-14 00:42:41
【问题描述】:
我被一些琐碎的问题困住了,好吧,我想我需要帮助。
我有两个矩形,保证它们的 4 个基点有一个共同点(图片的上半部分)。还保证它们是轴对齐的。
我知道这些矩形的这个共同点(也很容易推导出来)、尺寸和坐标。
现在,我需要检索名为1 和2 的矩形的坐标,我正在寻找一种简单的方法来做到这一点(图片的下半部分)。
我当前的实现依赖于许多 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);
这是重构的,但方法getCommonPoint 和getAdjacent... 和getOpposite 有很多if 语句,我想如果这可以做得更好。
【问题讨论】:
-
呃,你用的是什么语言,肯定不能用C#,java 和 c++?另外,你试过什么?
-
投票结束——问题太宽泛,编程语言标签太多。谁知道他指的是什么语言,或者语言是否重要。
-
@ChristopherCurrens 我编写了单独的 if 语句来确定我的情况是否属于 4 种可能的情况,然后使用单独的代码部分来确定矩形的坐标。但是,我怀疑,这可以很聪明地完成,甚至无需确定 4 种可能状态中的 1 种。
-
对这个问题投反对票或投赞成票是愚蠢的。
-
您可以发布您现在拥有的代码吗?
标签: algorithm language-agnostic