# How to calculate corner positions/marks of a rotated/tilted rectangle?

I've got two elements, a 2D point and a rectangular area. The point represents the middle of that area. I also know the width and height of that area. And the area is tilted by 40° relative to the grid.

Now I'd like to calculate the absolute positions of each corner mark of that tilted area only using this data. Is that possible?

X = x*cos(θ) - y*sin(θ)
Y = x*sin(θ) + y*cos(θ)


This will give you the location of a point rotated θ degrees around the origin. Since the corners of the square are rotated around the center of the square and not the origin, a couple of steps need to be added to be able to use this formula. First you need to set the point relative to the origin. Then you can use the rotation formula. After the rotation you need to move it back relative to the center of the square.

// cx, cy - center of square coordinates
// x, y - coordinates of a corner point of the square
// theta is the angle of rotation

// translate point to origin
float tempX = x - cx;
float tempY = y - cy;

// now apply rotation
float rotatedX = tempX*cos(theta) - tempY*sin(theta);
float rotatedY = tempX*sin(theta) + tempY*cos(theta);

// translate back
x = rotatedX + cx;
y = rotatedY + cy;


Apply this to all 4 corners and you are done!

It is a common technique to rotate a point about a pivot by translating to a coordinate system where the pivot is the origin, then rotating about this origin, then translating back to world coordinates. (A very good explanation of this approach is available at Khan Academy)

However you are not storing your rectangle corners in world coordinates so we can tailor an approach to suit the data you have available.

Cx, Cy // the coordinates of your center point in world coordinates
W      // the width of your rectangle
H      // the height of your rectangle
θ      // the angle you wish to rotate

//The offset of a corner in local coordinates (i.e. relative to the pivot point)
//(which corner will depend on the coordinate reference system used in your environment)
Ox = W / 2
Oy = H / 2

//The rotated position of this corner in world coordinates
Rx = Cx + (Ox  * cos(θ)) - (Oy * sin(θ))
Ry = Cy + (Ox  * sin(θ)) + (Oy * cos(θ))


This approach can then be easily applied to the other three corners.

Based on the other answers, and to complement them, I managed to create an example with P5 here.

Here is the code, in case you want to access it directly:

function setup() {
createCanvas(400, 400);
}

var count = 0;

function draw() {
background(250);
rectMode(CENTER);
stroke(0,0,255);
fill(0,0,255);
count += 1;

var box1X = 100;
var box1Y = 100;
var box2X = 160;
var box2Y = 100;
var box1R = count;
var box2R = -60-count;
var box1W = 50;
var box1H = 50;
var box2W = 50;
var box2H = 50;

translate(box1X, box1Y);
rect(0, 0, box1W, box1H);
translate(-box1X, -box1Y);

translate(box2X, box2Y);
rect(0, 0, box2W, box2H);
translate(-box2X, -box2Y);

stroke(255,0,0);
fill(255,0,0);

var pointRotated = [];
pointRotated.push(GetPointRotated(box1X, box1Y, box1R, -box1W/2, box1H/2));  // Dot1
pointRotated.push(GetPointRotated(box1X, box1Y, box1R, box1W/2, box1H/2));   // Dot2
pointRotated.push(GetPointRotated(box1X, box1Y, box1R, -box1W/2, -box1H/2)); // Dot3
pointRotated.push(GetPointRotated(box1X, box1Y, box1R, box1W/2, -box1H/2));  // Dot4
pointRotated.push(createVector(box1X, box1Y)); // Dot5

for (var i=0;i<pointRotated.length;i++){
ellipse(pointRotated[i].x,pointRotated[i].y,3,3);
}
}

function GetPointRotated(X, Y, R, Xos, Yos){
// Xos, Yos // the coordinates of your center point of rect
// R      // the angle you wish to rotate

//The rotated position of this corner in world coordinates
var rotatedX = X + (Xos  * cos(radians(R))) - (Yos * sin(radians(R)))
var rotatedY = Y + (Xos  * sin(radians(R))) + (Yos * cos(radians(R)))

return createVector(rotatedX, rotatedY)
}
<script src="//cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.3/p5.min.js"></script>

Refactoring the code above gives a cleaned up form which also highlights the simple fact that each corner is basically center + height/2 + width/2, with signs as appropriate for each corner. This also holds if you treat height/2 and width/2 as rotated vectors.

Trusting the interpreter to inline the helpers, this should be pretty effective, should we try to benchmark this.

function addPoints(p1, p2) {
return { x: p1.x + p2.x, y: p1.y + p2.y }
}

function subPoints(p1, p2 ) {
return { x: p1.x - p2.x, y: p1.y - p2.y }
}

function multPoints(p1, p2 ) {
return { x: p1.x * p2.x, y: p1.y * p2.y }
}

function getRulerCorners() {
const sin = Math.sin(ruler.angle);
const cos = Math.cos(ruler.angle);
const height = { x: sin * ruler.height/2, y: cos * ruler.height/2 };
const heightUp = addPoints(ruler, multPoints({x: 1, y :-1}, height));
const heightDown = addPoints(ruler, multPoints({x: -1, y: 1}, height));
const width = { x: cos * ruler.width/2, y: sin * ruler.width/2 };
ruler.nw = subPoints(heightUp, width);
ruler.sw = subPoints(heightDown, width);
}


See the Wikipedia article on rotation. The essence is this:

(1) If c is the center point, then the corners are c + (L/2,W/2), +/- etc., where L and W are the length & width of the rectangle.

(2) Translate the rectangle so that center c is at the origin, by subtracting c from all four corners.

(3) Rotate the rectangle by 40 deg via the trig formulas cited.

(4) Translate back by adding c to each coordinate.

• Thanks for your answer but I'm afraid I don't get it. How am I supposed to substract the center (known) from the corners (unknown) if they are unknown? I mean, the coordinates of the corners are the very things I'm trying to find out. – Stacky Nov 2 '14 at 10:40
• I tried to clarify. – Joseph O'Rourke Nov 2 '14 at 15:33

Possibly, there is some optimizations available by dividing the problem into two:

• compute the center of the top and the bottom side i. e., center + the rotated height/2.
• compute the corners relative to these center points using the rotated width/2
• Compute the actual sine and cosine once and for all.

Code below, here the rectangle is called ruler. ruler.x, ruler,y is the rectangle center.

/** Middle point on rulers's top side. */
function getRulerTopMiddle(cos, sin) {
return {
x : ruler.x + sin * ruler.height/2,
y : ruler.y - cos * ruler.height/2
}
}

/** Middle point on rulers's bottom side. */
function getRulerBottomMiddle(cos, sin) {
return {
x : ruler.x - sin * ruler.height/2,
y : ruler.y + cos * ruler.height/2
}
}

/** Update ruler's four corner coordinates. */
function getRulerCorners() {
const sin = Math.sin(ruler.angle);
const cos = Math.cos(ruler.angle);
const topMiddle = getRulerTopMiddle(cos, sin);
const bottomMiddle = getRulerBottomMiddle(cos, sin);

ruler.nw = {
x: topMiddle.x - (cos * ruler.width/2),
y: topMiddle.y - (sin * ruler.width/2)
}
ruler.ne = {
x: topMiddle.x + (cos * ruler.width/2),
y: topMiddle.y + (sin * ruler.width/2)
}
ruler.sw = {
x: bottomMiddle.x - (cos * ruler.width/2),
y: bottomMiddle.y - (sin * ruler.width/2)
}
ruler.se = {
x: bottomMiddle.x + (cos * ruler.width/2),
y: bottomMiddle.y + (sin * ruler.width/2)
}
}


A little late, but here's a compact function I've used. It calculates the top and left points, then just flips them for the opposite corners.

rotatedRect(float x, float y, float halfWidth, float halfHeight, float angle)
{
float c = cos(angle);
float s = sin(angle);
float r1x = -halfWidth * c - halfHeight * s;
float r1y = -halfWidth * s + halfHeight * c;
float r2x =  halfWidth * c - halfHeight * s;
float r2y =  halfWidth * s + halfHeight * c;

// Returns four points in clockwise order starting from the top left.
return
(x + r1x, y + r1y),
(x + r2x, y + r2y),
(x - r1x, y - r1y),
(x - r2x, y - r2y);
}


Old post, but here is another way to do it:

public static Point[] GetRotatedCorners(Rectangle rectangleToRotate, float angle)
{

// Calculate the center of rectangle.
Point center = new Point(rectangleToRotate.Left + (rectangleToRotate.Left + rectangleToRotate.Right) / 2, rectangleToRotate.Top + (rectangleToRotate.Top + rectangleToRotate.Bottom) / 2);

Matrix m = new Matrix();
// Rotate the center.
m.RotateAt(360.0f - angle, center);

// Create an array with rectangle's corners, starting with top-left corner and going clock-wise.
Point[] corners = new Point[]
{
new Point(rectangleToRotate.Left, rectangleToRotate.Top), // Top-left corner.
new Point(rectangleToRotate.Right, rectangleToRotate.Top),    // Top-right corner.
new Point(rectangleToRotate.Right, rectangleToRotate.Bottom), // Bottom-right corner.
new Point(rectangleToRotate.Left, rectangleToRotate.Bottom),  // Botton-left corner
};

// Now apply the matrix to every corner of the rectangle.
m.TransformPoints(corners);

// Return the corners of rectangle rotated by the provided angle.
return corners;
}


Hope it helps!