Last active
September 23, 2018 21:10
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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