[英]How to calculate rotation angle of a rectangle with 4 points (x1,y1,.,x4,y4) in clock-wise direction, to make it straight or 0 degree
给定一个多边形(倾斜矩形),它代表一个单词区域,其中 4 个点按顺时针方向排列,如何识别旋转角度以使其在视角中为 0 度以使文本歪斜?
I guess the rectangle looks like this?!我猜矩形看起来像这样?!
The red angle, let's call it alpha
, is your rotation angle.红色角度,我们称之为alpha
,是您的旋转角度。 Let's say the top left point is named A
and top right is called B
.假设左上角名为A
,右上角名为B
。 The line from A
to B
is the hypotenuse of the red triangle which is as long as the width of the rectangle.从A
到B
的线是红色三角形的斜边,与矩形的宽度一样长。 The opposite cathetus is the right side of the red triangle which is as long as the difference between the y
coordinates of A
and B
.对面的导管是红色三角形的右侧,与A
和B
的y
坐标之间的差一样长。
The sine function is defined as the opposite cathetus divided by the hyptenuse.正弦函数被定义为对面导管除以斜边。
sin(alpha) = (y_A-y_B)/width
-> alpha = sin^-1((y_A-y_B)/width)
sin(alpha) = (y_A-y_B)/width
-> alpha = sin^-1((y_A-y_B)/width)
# I guess the coordinates are stored in a list (x1,y1,...,x4,y4) called coords
y_A = coords[1]
y_B = coords[3]
op_cath = y_A - y_B
import math
alpha = math.asin(op_cath/width)
To get the new coordinates (rotation).获取新坐标(旋转)。
# new coordinates of A
x_A = x_A*math.cos(alpha)-y_A*math.sin(alpha)
y_A = x_A*math.sin(alpha)+y_A*math.cos(alpha)
# repeat for each point
( https://math.stackexchange.com/questions/2581058/rotating-rectangle-by-its-center ) ( https://math.stackexchange.com/questions/2581058/rotating-rectangle-by-its-center )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.