凸多边形的展开填充
我有一个由 N
点组成的凸多边形 P1
。该多边形可以是任何形状或比例(只要它仍然是凸的)。
我需要使用原始多边形几何形状计算另一个多边形P2
,但“扩展”了给定数量的单位。扩展凸多边形的算法可能是什么?
I have a convex polygon P1
of N
points. This polygon could be any shape or proportion (as long as it is still convex).
I need to compute another polygon P2
using the original polygons geometry, but "expanded" by a given number of units. What might the algorithm be for expanding a convex polygon?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看看笔直的骷髅。正如这里暗示的那样,非凸多边形存在许多棘手的问题,您已经巧妙地幸免于难!
Look at straight skeletons. As has been implied here there are a number of tricky issues with non convex polygons that you have been mecifully spared!
要扩展凸多边形,请绘制一条平行于每条边并远离给定单位数的线。然后使用新线的交点作为扩展多边形的顶点。最后的 javascript/canvas 遵循以下功能细分:
第 1 步:找出哪一侧是“out”
顶点(点)的顺序很重要。在凸多边形中,它们可以按顺时针 (CW) 或逆时针 (CCW) 顺序列出。在 CW 多边形中,将其中一条边逆时针旋转 90 度以获得朝外的法线。在 CCW 多边形上,将其转向CW。
如果事先未知顶点的转动方向,请检查第二条边如何从第一条边转向。在凸多边形中,剩余的边将继续朝同一方向转动:
找到第一条边的 CW 法线。我们还不知道它是朝内还是朝外。
计算第二条边与我们计算的法线的点积。如果第二个边沿顺时针旋转,则点积将为正。否则将为负数。
数学:
代码:
第 2 步:查找与多边形边缘平行的线
现在我们知道哪一边在外面,我们可以计算平行于每个多边形边的线,精确地达到所需的距离。这是我们的策略:
对于每条边,计算其向外的法线
标准化法线,使其长度变为一个单位
将法线乘以我们希望展开的多边形与原始多边形的距离
将乘法线添加到边缘的两端。这将给我们平行线上的两个点。这两个点足以定义平行线。
代码:
第3步:计算平行线的交点
--这些将是展开的多边形的顶点。
数学:
穿过两点 P1、P2 的直线 可以描述为:
两条线可以描述为
并且它们的交点必须在两条线上:
这可以被处理为:
在 x,y 项中是:
由于点 P1、P2、P3 和P4 是已知的,因此以下值也是已知的:
这将我们的方程缩短为:
求解 t 得到:
这让我们找到
P = P1 + t * (P2 - P1) 处的交集
。代码:
第4步:处理特殊情况
有许多特殊情况值得关注。留给读者作为练习...
当两条边之间存在非常尖锐的角度时,扩展的顶点可能与原始顶点相距非常。如果超出某个阈值,您可能需要考虑修剪扩展的边缘。在极端情况下,角度为零,这表明扩展的顶点位于无穷远,导致算术中除以零。请注意。
当前两条边在同一条线上时,仅通过观察无法判断它是 CW 还是 CCW 多边形。查看更多边。
非凸多边形更有趣......这里不讨论。
完整示例代码
将其放入支持画布的浏览器中。我在 Windows 上使用 Chrome 6。三角形及其扩展版本应该具有动画效果。
示例代码免责声明:
为了清晰起见,该示例牺牲了一些效率。在您的代码中,您可能只想计算每条边的扩展并行一次,而不是像此处的两次
,
画布的 y 坐标向下增长,这会反转 CW/CCW 逻辑。不过事情仍然继续进行,因为我们只需要将外部法线转向与多边形相反的方向 - 并且两者都会翻转。
To expand a convex polygon, draw a line parallel to each edge and the given number of units away. Then use the intersection points of the new lines as the vertices of the expanded polygon. The javascript/canvas at the end follows this functional breakdown:
Step 1: Figure out which side is "out"
The order of the vertices (points) matters. In a convex polygon, they can be listed in a clockwise (CW), or a counter-clockwise (CCW) order. In a CW polygon, turn one of the edges 90 degrees CCW to obtain an outward-facing normal. On a CCW polygon, turn it CW instead.
If the turn direction of the vertices is not known in advance, examine how the second edge turns from the first. In a convex polygon, the remaining edges will keep turning in the same direction:
Find the CW normal of the first edge. We don't know yet whether it's facing inward or outward.
Compute the dot product of the second edge with the normal we computed. If the second edge turns CW, the dot product will be positive. It will be negative otherwise.
Math:
Code:
Step 2: Find lines parallel to the polygon edges
Now that we know which side is out, we can compute lines parallel to each polygon edge, at exactly the required distance. Here's our strategy:
For each edge, compute its outward-facing normal
Normalize the normal, such that its length becomes one unit
Multiply the normal by the distance we want the expanded polygon to be from the original
Add the multiplied normal to both ends of the edge. That will give us two points on the parallel line. Those two points are enough to define the parallel line.
Code:
Step 3: Compute the intersections of the parallel lines
--these will be the vertices of the expanded polygon.
Math:
A line going through two points P1, P2 can be described as:
Two lines can be described as
And their intersection has to be on both lines:
This can be massaged to look like:
Which in x,y terms is:
As the points P1, P2, P3 and P4 are known, so are the following values:
This shortens our equations to:
Solving for t gets us:
Which lets us find the intersection at
P = P1 + t * (P2 - P1)
.Code:
Step 4: Deal with special cases
There is a number of special cases that merit attention. Left as an exercise to the reader...
When there's a very sharp angle between two edges, the expanded vertex can be very far from the original one. You might want to consider clipping the expanded edge if it goes beyond some threshold. At the extreme case, the angle is zero, which suggests that the expanded vertex is at infinity, causing division by zero in the arithmetic. Watch out.
When the first two edges are on the same line, you can't tell if it's a CW or a CCW polygon by looking just at them. Look at more edges.
Non convex polygons are much more interesting... and are not tackled here.
Full sample code
Drop this in a canvas-capable browser. I used Chrome 6 on Windows. The triangle and its expanded version should animate.
sample code disclaimers:
the sample sacrifices some efficiency for the sake of clarity. In your code, you may want to compute each edge's expanded parallel just once, and not twice as in here
the canvas's y coordinate grows downward, which inverts the CW/CCW logic. Things keep on working though as we just need to turn the outward normals in a direction opposite to the polygon's -- and both get flipped.
如果多边形以原点为中心,只需将每个点乘以公共比例因子即可。
如果多边形不是以原点为中心,则首先进行平移,使中心位于原点上,缩放,然后将其平移回原来的位置。
发表评论后
您似乎希望所有点都从原点移动相同的距离。
您可以通过获取该点的归一化向量来对每个点执行此操作。将其乘以“扩展常数”,并将所得向量加回原始点。
注意:如果中心不是该解决方案的原点,您仍然需要平移-修改-平移。
If the polygon is centered on the origin simply multiply each of the points by a common scaling factor.
If the polygon is not centered on the origin then first translate so the center is on the origin, scale, and then translate it back to where it was.
After your comment
It seems you want all points to be moved the same distance away from the origin.
You can do this for each point by getting the normalised vector to this point. multiplying this by your 'expand constant' and adding the resulting vector back onto the original point.
n.b. You will still have to translate-modify-translate if the center is not also the origin for this solution.
对于原图的每个线段,找到该线段的中点 m 和(单位长度)向外法线 u。扩展多边形的相应线段将位于通过 m+n*u(您要将原始多边形扩展 n)与法向 u 的直线上。要找到扩展多边形的顶点,您需要找到连续线对的交点。
For each line segment of the original, find the midpoint m and (unit length) outward normal u of the segment. The corresponding segment of the expanded polygon will then lie on the line through m+n*u (where you want to expand the original by n) with normal u. To find the vertices of the expanded polygon you then need to find the intersection of pairs of successive lines.
设多边形的点为 A1、B1、C1...现在有从 A1 到 B1 的线,然后从 B1 到 C1...我们要计算多边形 P2 的点 A2、B2、C2。
如果平分角度,例如 A1 B1 C1,您将得到一条沿您想要的方向延伸的线。现在你可以在它上面找到一个点B2,它是等分线上距B1的适当距离。
对多边形 P1 的所有点重复此操作。
Let the points of the polygon be A1, B1, C1... Now you have lines from A1 to B1, then from B1 to C1... We want to compute points A2, B2, C2 of the polygon P2.
If you bisect angle, for example A1 B1 C1, you will have a line which goes in the direction you want. Now you can find a point B2 on it which is the appropriate distance from B1 on bisector line.
Repeat this for all points of the polygon P1.