type Direction = "N" | "E" | "S" | "W" | "NW" | "NE" | "SW" | "SE"; export function toGetClosestPoint( x1: number, y1: number, x2: number, y2: number, sx: number, sy: number, ) { let direction: Direction = "N"; const sectors: Record = { NW: [x1, y2], N: [x1, y2], NE: [x2, y2], E: [x2, y2], SE: [x2, y1], S: [x1, y1], SW: [x1, y1], W: [x1, y2], }; for (let key in sectors) { const [x, y] = sectors[key]; if (sx <= x && sy >= y) { direction = "NW"; break; } if (sx >= x && sy > y && sx <= x2) { direction = "N"; break; } if (sx >= x && sy >= y) { direction = "NE"; break; } if (sx >= x && sy <= y && sy >= y1) { direction = "E"; break; } if (sx >= x && sy <= y && sx <= x2) { direction = "S"; break; } if (sx >= x && sy <= y) { direction = "SE"; break; } if (sy <= y && sx <= x) { direction = "SW"; break; } if (sy <= y2 && sy >= y1 && sx <= x) { direction = "W"; break; } } return direction; }