Rotate a Rectangle

for some reason the math for this is eluding me :frowning:

Given a Rectangle (x:0,y:0, width:W, height:H) and an angle to rotate it
what is the minimum resulting rectangle required to to encapsulate the result?
where the CENTER remains constant (ie. I want to rotate a rectangle around its center point, with no clipping)
NOTE : rectangle… which may or may not be square

This will rotate a rectangle based at 0,0:

Sub RotateWidthHeight(pWidth As Integer, pHeight As Integer, pRotation As Double, ByRef pRotatedWidth As Integer, ByRef pRotatedHeight As Integer)
  Dim theRadians As Double
  Dim theCosine, theSine As Double
  Dim x1, y1, x2, y2, x3, y3 As Double
  Dim minx, miny, maxx, maxy As Double
  Dim rotatedWidth, rotatedHeight As Integer
  
  If (pRotation = 0) Or (pRotation = 180) Then
    rotatedWidth = pWidth
    rotatedHeight = pHeight
  Elseif (pRotation = 90) Or (pRotation = 270) Then
    rotatedWidth = pHeight
    rotatedHeight = pWidth
  Else
    theRadians = (2 * 3.14159265358979323846264338327950288 * pRotation) / 360
    
    theCosine = Cos(theRadians)
    theSine = Sin(theRadians)
    
    x1 = (-pHeight * theSine)
    y1 = (pHeight * theCosine)
    x2 = (pWidth * theCosine - pHeight * theSine)
    y2 = (pHeight * theCosine + pWidth * theSine)
    x3 = (pWidth * theCosine)
    y3 = (pWidth * theSine)
    
    minx = Min(0, Min(x1, Min(x2,x3)))
    miny = Min(0, Min(y1, Min(y2,y3)))
    
    maxx = Max(0, Max(x1, Max(x2,x3)))
    maxy = Max(0, Max(y1, Max(y2,y3)))
    
    rotatedWidth = (maxx - minx + 1)
    rotatedHeight = (maxy - miny + 1)
  End If
  
  pRotatedWidth = rotatedWidth
  pRotatedHeight = rotatedHeight
End Sub

The centre point should be: (rotatedWidth - width) and (rotatedHeight - height)

If I understood well (probably I didn’t :() you need a square centred in the same centre of the original rectangle, which side is the diagonal of the rectangle side = sqrt(W^2+H^2)