假设我有两个长方形如下:
如何得到描述内部矩形向上移动到非共享边所需的位移的向量?
发布于 2011-09-29 10:13:10
如果我对你的理解是正确的,那么你应该遵循以下步骤:
vector = outerRecCorner - innerRecCorner到
发布于 2011-09-29 10:39:10
与其说是编程题,不如说是数学:)
假设您有两个矩形:A(内部)和B(外部)。它们有四个角落:
Point [] GetCorners (Rectangle rect)
{
Point [] corners = new Point[4];
Point corners[0] = new Point(rect.X, rect.Y);
Point corners[1] = new Point(rect.X + rect.Width, rect.Y);
Point corners[2] = new Point(rect.X, rect.Y + rect.Height);
Point corners[3] = new Point(rect.X + rect.Width + rect.Width, rect.Y);
return corners;
}首先找到第一个共享的角落:
Point [] cornersListA = GetCorners(A);
Point [] cornersListB = GetCorners(B);
int sharedCornerIndex = 0;
for (int i=0; i<4; i++)
{
if(cornersListA[i].X==cornersListB[i].X && cornersListA[i].Y==cornersListB[i].Y)
{
sharedCornerIndex = i;
break;
}
}那就去找它的角土:
int oppositeCornerIndex = 0;
if(sharedCornerIndex ==0) oppositeCornerIndex = 3;
if(sharedCornerIndex ==3) oppositeCornerIndex = 0;
if(sharedCornerIndex ==1) oppositeCornerIndex = 2;
if(sharedCornerIndex ==2) oppositeCornerIndex = 1;最后,获取向量(我没有检查代码的这一部分,但它应该能工作):
Vector v = new Vector();
v.X = cornersListB[oppositeCornerIndex].X - cornersListA[oppositeCornerIndex].X;
v.Y = cornersListB[oppositeCornerIndex].Y - cornersListA[oppositeCornerIndex].Y;https://stackoverflow.com/questions/7595651
复制相似问题