Calculating Dimensions of a Rotated Rectangle in PHP

When you rotate a rectangle, you might want to find the bounding box that the rotated rectangle is contained in.

The code below will give you the width and height of the bounding box.

/**
 * Gets the bounding box dimensions of the rectangle rotated by the angle.
 * @param intAngle The angle in degrees about which the rectangle is being rotated, clockwise.
 * @param intWidth The width of the rectangle.
 * @param intHeight The height of the rectangle.
 * @return
 */
function getBoundingBox( $intAngle, $intWidth, $intHeight ) {
	$fltRadians = deg2rad( $intAngle );
	$intWidthRotated = $intHeight * abs( sin( $fltRadians) ) + $intWidth * abs( cos( $fltRadians ) );
	$intHeightRotated = $intHeight * abs( cos( $fltRadians ) ) + $intWidth * abs( sin( $fltRadians ) );
	
	return array( $intWidthRotated, $intHeightRotated );
}

Leave a Reply

Your email address will not be published. Required fields are marked *