Skip to content

Instantly share code, notes, and snippets.

@idkiller
Created April 15, 2019 09:52
Show Gist options
  • Save idkiller/18fadbd9aeb79bc386b4d8fabc59e1f8 to your computer and use it in GitHub Desktop.
Save idkiller/18fadbd9aeb79bc386b4d8fabc59e1f8 to your computer and use it in GitHub Desktop.
const RAD = Math.PI / 180;
function bound(x: number, y: number, w: number, h: number, deg: number) {
const rcos = Math.cos(deg * RAD);
const rsin = Math.sin(deg * RAD);
let left = 0;
let top = 0;
let right = 0;
let bottom = 0;
if (rcos >= 0 && rsin >= 0) { // top, right
left = x - h * rsin;
top = y;
right = x + w * rcos;
bottom = y + w * rsin + h * rcos;
} else if (rcos < 0 && rsin >= 0) { // bottom, right
left = x - h * rsin + w * rcos;
top = y + h * rcos;
right = x;
bottom = y + w * rsin;
} else if (rcos < 0 && rsin < 0) { // bottom, left
left = x + w * rcos;
top = y + w * rsin + h * rcos;
right = x - h * rsin;
bottom = y;
} else { // top, left
left = x;
top = y + w * rsin;
right = x + w * rcos - h * rsin;
bottom = y + h * rcos;
}
return { left, top, right, bottom };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment