这是同样的事情作为测试矩形是否相交,我觉得你的代码是,如果你不认为这些船只作为一个点,长度和方向,但作为一个矩形的简单。
所以它转换
int x // x position of first part of ship
int y // y position of first part of ship
char dir // direction of the ship, either 'N','S','E' or 'W'
int length // length of the ship
这个(允许负CX & CY得到N,S,E,W)
int x // x position of first part of ship
int y // y position of first part of ship
int cx // length of the ship in X
int cy // length of the ship in Y
或本
int left // x position of Eastern part of the ship
int top // y position of Northernmost part of ship
int right // x position of Westernmost part of the ship
int bottom // y position of Southernmost part of ship
bool orientation; // so we can tell East from West or North from South.
然后,简单的功能可以确定两艘船是否相交。
bool DoShipsIntersect(Ship * a, Ship * b)
{
if ((a->right < b->left) || (b->right < a->left))
return false;
if ((a->bottom < b->top) || (b->bottom < a->top))
return false;
return true;
}
只要你没有成千上万的船只,每艘船与其他船只的蛮力比较应该是相当快的。
您只需要2个方向(例如南部和东部),以防方向本身无关紧要。 – 2010-03-20 07:01:38
从文件中读入方向,所以它可能不一定包含船舶的顶部或最左侧部分 – Gary 2010-03-20 07:06:01
这看起来像一艘战列舰游戏,是吗? – erelender 2010-03-20 07:27:34