diff --git a/lib/toGetClosestPoint/toGetClosestPoint.ts b/lib/toGetClosestPoint/toGetClosestPoint.ts index 4ff41ef..e625efb 100644 --- a/lib/toGetClosestPoint/toGetClosestPoint.ts +++ b/lib/toGetClosestPoint/toGetClosestPoint.ts @@ -7,60 +7,18 @@ export function toGetClosestPoint( sx: number, sy: number, ) { - let direction: Direction = "N"; + let direction: Direction = "" as Direction; - 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], - }; + if (sx >= x1 && sx <= x2 && sy >= y2) direction = "N"; + if (sx >= x2 && sy <= y2 && sy >= y1) direction = "E"; + if (sx >= x1 && sx <= x2 && sy <= y1) direction = "S"; + if (sx <= x1 && sy <= y2 && sy >= y1) direction = "W"; - for (let key in sectors) { - const [x, y] = sectors[key]; + if (sx <= x1 && sy >= y2) direction = "NW"; + if (sx >= x2 && sy >= y2) direction = "NE"; - 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; - } - } + if (sx <= x1 && sy <= y1) direction = "SW"; + if (sx >= x2 && sy <= y1) direction = "SE"; return direction; }