Reputation: 335
Can someone give me an algorithm that finds the position of all four corners of a rectangle if I know its center point(in global coordinate space), width and height, and its rotation around that center point?
clarification edit: The width and height I am referring to is the length of the sides of the rectangle.
Upvotes: 10
Views: 17485
Reputation: 357
Code Python with matrices :
import numpy as np
import math
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle, Circle
#center of rectangle
X = 2698.77
Y = 1283.01
center = np.array([[X],[Y]])
angle_deg = 83.5694 #angle rectangle
angle = math.radians(angle_deg)
# rectangle's dimension
width = 2022.23
height = 1978.78
R_lt = np.array([[np.cos(angle),-np.sin(angle)],[-np.sin(angle),-np.cos(angle)]])
A = np.dot(R_lt,np.array([[width/2], [height/2]])) + center
R_rt = np.array([[np.cos(angle),np.sin(angle)],[-np.sin(angle),np.cos(angle)]])
B = np.dot(R_rt,np.array([[width/2], [height/2]])) + center
R_rb = np.array([[-np.cos(angle),np.sin(angle)],[np.sin(angle),np.cos(angle)]])
C = np.dot(R_rb,np.array([[width/2], [height/2]])) + center
R_lb = np.array([[-np.cos(angle),-np.sin(angle)],[np.sin(angle),-np.cos(angle)]])
D = np.dot(R_lb,np.array([[width/2], [height/2]])) + center
corners = [A,B,C,D]
Upvotes: 0
Reputation: 355
The coordinates of each vertices:
Center point = (center.x, center.y)
Angle = angle
Height = height
Width = width
TOP RIGHT VERTEX:
Top_Right.x = center.x + ((width / 2) * cos(angle)) - ((height / 2) * sin(angle))
Top_Right.y = center.y + ((width / 2) * sin(angle)) + ((height / 2) * cos(angle))
TOP LEFT VERTEX:
Top_Left.x = center.x - ((width / 2) * cos(angle)) - ((height / 2) * sin(angle))
Top_Left.y = center.y - ((width / 2) * sin(angle)) + ((height / 2) * cos(angle))
BOTTOM LEFT VERTEX:
Bot_Left.x = center.x - ((width / 2) * cos(angle)) + ((height / 2) * sin(angle))
Bot_Left.y = center.y - ((width / 2) * sin(angle)) - ((height / 2) * cos(angle))
BOTTOM RIGHT VERTEX:
Bot_Right.x = center.x + ((width / 2) * cos(angle)) + ((height / 2) * sin(angle))
Bot_Right.y = center.y + ((width / 2) * sin(angle)) - ((height / 2) * cos(angle))
This algorithm is a compressed version of these 3 steps:
Step 1: Center your rectangle around the origin
Step 2: Apply the rotation matrix to each vertex
Step 3: Move the rotated rectangle to the correct position, by adding the center point to each coordinate
This is explained in more depth here https://math.stackexchange.com/questions/126967/rotating-a-rectangle-via-a-rotation-matrix
Upvotes: 9
Reputation: 122
If you need all of the corners, it just might be faster to create two perpendicular vectors from the center of the rectangle to both of its sides, and then to add/subtract these vectors to/from the center of the rectangle to form the points.
This might be faster, since you don't need to repeatedly call sin() and cos() functions (you do so only once for each).
Assuming we have a Vector library (for cleaner code - only helps with vector arithmetic), here is the code in Python:
def get_corners_from_rectangle(center: Vector, angle: float, dimensions: Vector):
# create the (normalized) perpendicular vectors
v1 = Vector(cos(angle), sin(angle))
v2 = Vector(-v1[1], v1[0]) # rotate by 90
# scale them appropriately by the dimensions
v1 *= dimensions[0] / 2
v2 *= dimensions[1] / 2
# return the corners by moving the center of the rectangle by the vectors
return [
center + v1 + v2,
center - v1 + v2,
center - v1 - v2,
center + v1 - v2,
]
Upvotes: 6
Reputation: 80287
Top right corner has coordinates w/2, h/2 relative to the center. After rotation its absolute coordinates are
x = cx + w/2 * Cos(Phi) - h/2 * Sin(Phi)
y = cy + w/2 * Sin(Phi) + h/2 * Cos(Phi)
Upvotes: 14