JSFiddle - React, Tailwind, and code Playground

by Evgeniy Lukovsky

TypeScript

const boundingBox = ({
  x,
  y,
  width,
  height,
  rotate,
}: RotatedRectangle): Rectangle => {
  const cX = x + width / 2
  const cY = y + height / 2
  const cosT = Math.cos(rotate)
  const sinT = Math.sin(rotate)
  const halfWidth = (width * cosT - height * sinT) / 2
  const halfHeight = (width * sinT + height * cosT) / 2
  return {
    x: cX - halfWidth,
    y: cY - halfHeight,
    width: halfWidth * 2,
    height: halfHeight * 2,
  }
}

const testBb= {
  x: 50,
  y: 50,
  width:20,
  height:1,
  rotate: Math.PI/4
}

console.log(boundingBox(testBb))