這是同樣的事情作爲測試矩形是否相交,我覺得你的代碼是,如果你不認爲這些船隻作爲一個點,長度和方向,但作爲一個矩形的簡單。
所以它轉換
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