计算边界矩形在多边形的角 [英] Calculating the Bounding Rectangle at an Angle of a Polygon
问题描述
我有必要确定在任意角度多边形的边框。这张图片说明了什么,我需要做的:
I have the need to determine the bounding rectangle for a polygon at an arbitrary angle. This picture illustrates what I need to do:
粉红色的矩形就是我需要确定在不同的角度进行简单的2D多边形。
The pink rectangle is what I need to determine at various angles for simple 2d polygons.
任何解决方案都多少AP preciated!
Any solutions are much appreciated!
编辑:
谢谢你的答案,我得到了它的工作,一旦我得到了中心点正确的。你们是真棒!
Thanks for the answers, I got it working once I got the center points correct. You guys are awesome!
推荐答案
要获得边框具有一定的角度,由角度旋转多边形倒过来。然后,您可以使用最小/最大x / y坐标,以得到一个简单的边框和旋转,通过角度,让您的最终结果。
To get a bounding box with a certain angle, rotate the polygon the other way round by that angle. Then you can use the min/max x/y coordinates to get a simple bounding box and rotate that by the angle to get your final result.
从您的评论看来你有问题,得到了多边形的中心点。一个多边形的中心应是每一个点的坐标和的平均值。因此,对于点P1,...,PN,计算:
From your comment it seems you have problems with getting the center point of the polygon. The center of a polygon should be the average of the coordinate sums of each point. So for points P1,...,PN, calculate:
xsum = p1.x + ... + pn.x;
ysum = p1.y + ... + pn.y;
xcenter = xsum / n;
ycenter = ysum / n;
为了使这一完整的,我还加了一些公式所涉及的旋转。要旋转点(X,Y)围绕一个中心点(CX,CY),请执行以下操作:
To make this complete, I also add some formulas for the rotation involved. To rotate a point (x,y) around a center point (cx, cy), do the following:
// Translate center to (0,0)
xt = x - cx;
yt = y - cy;
// Rotate by angle alpha (make sure to convert alpha to radians if needed)
xr = xt * cos(alpha) - yt * sin(alpha);
yr = xt * sin(alpha) + yt * cos(alpha);
// Translate back to (cx, cy)
result.x = xr + cx;
result.y = yr + cx;
这篇关于计算边界矩形在多边形的角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!