给定中心点和旋转找到旋转矩形的角
Find corners of a rotated rectangle given its center point and rotation
如果我知道矩形的中心点(在全局坐标 space 中)、宽度和高度以及围绕该中心点的旋转,谁能给我一个算法来找到矩形所有四个角的位置?
澄清编辑:
我指的宽度和高度是矩形边的长度。
右上角相对于中心的坐标为w/2、h/2。旋转后其绝对坐标为
x = cx + w/2 * Cos(Phi) - h/2 * Sin(Phi)
y = cy + w/2 * Sin(Phi) + h/2 * Cos(Phi)
如果您需要所有角,从矩形的中心到其两侧创建两个垂直向量,然后 add/subtract 这些向量 to/from 可能会更快矩形的中心形成点。
这可能会更快,因为您不需要重复调用 sin() 和 cos() 函数(每个函数只需调用一次)。
假设我们有一个 Vector 库(为了更简洁的代码 - 仅有助于向量运算),这里是 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,
]
各顶点坐标:
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))
此算法是以下 3 个步骤的压缩版本:
第 1 步:将矩形围绕原点居中
第 2 步:将旋转矩阵应用于每个顶点
第 3 步:通过将中心点添加到每个坐标,将旋转后的矩形移动到正确的位置
这里有更深入的解释https://math.stackexchange.com/questions/126967/rotating-a-rectangle-via-a-rotation-matrix
使用矩阵编码 Python :
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]
如果我知道矩形的中心点(在全局坐标 space 中)、宽度和高度以及围绕该中心点的旋转,谁能给我一个算法来找到矩形所有四个角的位置?
澄清编辑: 我指的宽度和高度是矩形边的长度。
右上角相对于中心的坐标为w/2、h/2。旋转后其绝对坐标为
x = cx + w/2 * Cos(Phi) - h/2 * Sin(Phi)
y = cy + w/2 * Sin(Phi) + h/2 * Cos(Phi)
如果您需要所有角,从矩形的中心到其两侧创建两个垂直向量,然后 add/subtract 这些向量 to/from 可能会更快矩形的中心形成点。
这可能会更快,因为您不需要重复调用 sin() 和 cos() 函数(每个函数只需调用一次)。
假设我们有一个 Vector 库(为了更简洁的代码 - 仅有助于向量运算),这里是 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,
]
各顶点坐标:
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))
此算法是以下 3 个步骤的压缩版本:
第 1 步:将矩形围绕原点居中
第 2 步:将旋转矩阵应用于每个顶点
第 3 步:通过将中心点添加到每个坐标,将旋转后的矩形移动到正确的位置
这里有更深入的解释https://math.stackexchange.com/questions/126967/rotating-a-rectangle-via-a-rotation-matrix
使用矩阵编码 Python :
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]