You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.1 KiB
TypeScript
67 lines
1.1 KiB
TypeScript
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<Direction, number[]> = {
|
|
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;
|
|
}
|