이것은 직사각형이 교차하는지 테스트하는 것과 같은 것으로,이 배송선을 점, 길이 및 방향으로 생각하지 않고 직사각형으로 생각하면 코드가 더 간단해질 것이라고 생각합니다.
그래서 그 다음이이
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
(N, S, E를 얻을 제외 CX에게 & CY를 허용 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 방향 (예 : 남쪽 및 동쪽 방향) 만 있으면됩니다. –
파일에서 방향이 읽혀 지므로 우주선의 맨 위 또는 맨 왼쪽 부분이 반드시 포함되지 않을 수도 있습니다 – Gary
이것은 전함 게임처럼 보입니까? – erelender