Skip to content

Instantly share code, notes, and snippets.

@jiayihu
Last active September 23, 2018 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiayihu/791fc2163ed83502188ef9cf372bdb15 to your computer and use it in GitHub Desktop.
Save jiayihu/791fc2163ed83502188ef9cf372bdb15 to your computer and use it in GitHub Desktop.
Find the nearest point in the perimeter of a rectangle to a given point. JavaScript/TypeScript porting
/**
* TS porting of https://stackoverflow.com/questions/20453545/how-to-find-the-nearest-point-in-the-perimeter-of-a-rectangle-to-a-given-point
*/
function clamp(x: number, lower: number, upper: number): number {
return Math.max(lower, Math.min(upper, x));
}
export function getNearestPointInPerimeter(
l: number,
t: number,
w: number,
h: number,
x: number,
y: number
) {
const r = l + w;
const b = t + h;
x = clamp(x, l, r);
y = clamp(y, t, b);
const dl = Math.abs(x - l);
const dr = Math.abs(x - r);
const dt = Math.abs(y - t);
const db = Math.abs(y - b);
const m = Math.min(dl, dr, dt, db);
if (m == dt) return { x: x, y: t };
if (m == db) return { x: x, y: b };
if (m == dl) return { x: l, y: y };
return { x: r, y: y };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment