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 }; }