相关疑难解决方法(0)

确定两个矩形是否相互重叠?

我正在尝试编写一个C++程序,它从用户那里获取以下输入来构造矩形(2到5之间):高度,宽度,x-pos,y-pos.所有这些矩形将平行于x轴和y轴存在,即它们的所有边都将具有0或无穷大的斜率.

我试图实现这个问题中提到的但我没有太多运气.

我目前的实现如下:

// Gets all the vertices for Rectangle 1 and stores them in an array -> arrRect1
// point 1 x: arrRect1[0], point 1 y: arrRect1[1] and so on...
// Gets all the vertices for Rectangle 2 and stores them in an array -> arrRect2

// rotated edge of point a, rect 1
int rot_x, rot_y;
rot_x = -arrRect1[3];
rot_y = arrRect1[2];
// point on rotated edge
int pnt_x, pnt_y;
pnt_x = arrRect1[2]; 
pnt_y = arrRect1[3]; …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm geometry rectangles overlap

320
推荐指数
7
解决办法
24万
查看次数

两个坐标的相对基数方向

以下枚举定义如下:

public enum Direction
{
    North,
    South,
    East,
    West,
    Northeast,
    Northwest,
    Southeast,
    Southwest,
    Undefined
}
Run Code Online (Sandbox Code Playgroud)

给定二维空间中的两组坐标,我想确定从第2点到第1点的相对基数方向.

例子:

  • P1(1,1)和P2(0,1)返回Direction.North,因为P2是P1的北部
  • P1(1,1)和P2(5,4)返回Direction.Southeast
  • P1(1,1)和P2(1,1)返回Direction.Undefined

我目前的方法涉及一系列条件,即

if (P1.X == P2.X)
{
    // either North, South or Undefined
    if (P1.Y < P2.Y)
        return Direction.South;
    else if (P1.Y > P2.Y)
        return Direction.North,
    else
        return Direction.Undefined;
}
else if (P1.Y == P2.Y)
{
    ...
}
else 
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

我正在寻求更短,更优雅的解决方案.

c# algorithm

6
推荐指数
1
解决办法
1842
查看次数

标签 统计

algorithm ×2

c# ×1

c++ ×1

geometry ×1

overlap ×1

rectangles ×1