[英]How to add n points between vertices when creating a polygon
Let's say I'm using the following code to create the vertices in a hexagon polygon: 假设我正在使用以下代码在六边形多边形中创建顶点:
hexagonPoints = new Array<Vector2>();
for (int a = 0; a < 6; a++)
{
float x = r * (float)Math.cos(a * 60 * Math.PI / 180f);
float y = r * (float)Math.sin(a * 60 * Math.PI / 180f);
hexagonPoints.add(new Vector2(x, y));
}
How can I add additional points along each side of the polygon, so that there are n additional points between each connecting set of vertices? 如何在多边形的每一边添加附加点,以使每个连接的顶点集之间都没有n个附加点? All vertices must be equal distance (including the vertices that formed the initial shape). 所有顶点必须等距(包括形成初始形状的顶点)。 Eg before: 例如:
. .
. .
After (where n = 1): 之后(其中n = 1):
. . .
. .
. . .
edit: here's my current code based on Volker's suggestion: 编辑:这是我当前基于Volker建议的代码:
float r = 3.0f;
int n = 1 + 2; // number of additional vertices between the main shape vertices
for (int a = 0; a < 6; a++)
{
float xe = r * (float)Math.cos(a * 60 * Math.PI / 180f);
float ye = r * (float)Math.sin(a * 60 * Math.PI / 180f);
if (a > 0)
{
for (int i = 1; i < n; ++i)
{
float xs = ((n - i) * hexagonPoints.get(a - 1).x + i * xe) / n;
float ys = ((n - i) * hexagonPoints.get(a - 1).y + i * ye) / n;
hexagonPoints.add(new Vector2(xs, ys));
}
}
hexagonPoints.add(new Vector2(xe, ye));
}
This plots the additional vertices, but they are not in the correct positions. 这将绘制其他顶点,但它们的位置不正确。
edit: seems this wasn't working because I wasn't taking into account the first vertex position. 编辑:似乎这是行不通的,因为我没有考虑到第一个顶点的位置。
Calculate the endpoints of each side as you are doing it already. 计算已经完成的每一端的端点。 Then introduce with an inner loop the additional splitting points. 然后通过内部循环引入其他拆分点。
for (int i=1; i<n: ++i)
{
float xs = ((n-i)*xb + i*xe)/n;
float ys = ((n-i)*yb + i*ye)/n;
hexagonPoints.add(new Vector(xs, ys));
}
hexagonPoints.add(new Vector(xe, ye));
where xb, yb is the beginning of a hexagon side and xe, ye the end. 其中xb,yb是六角形边的开始,而xe,是末端。
Here's a working solution, based on Volker's suggestion: 根据沃尔克的建议,这是一个可行的解决方案:
int size = 6;
int npoints = 2;
int nsegs = npoints + 1;
float xb = r;
float yb = 0;
hexagonPoints.add(new Vector2(xb, yb));
for (int a = 1; a <= size; a++)
{
float xe = r * (float) Math.cos(a * 60 * Math.PI / 180f);
float ye = r * (float) Math.sin(a * 60 * Math.PI / 180f);
for (int i = 1; i < nsegs; ++i)
{
float xs = ((nsegs - i) * xb + i * xe) / nsegs;
float ys = ((nsegs - i) * yb + i * ye) / nsegs;
hexagonPoints.add(new Vector2(xs, ys));
}
if (a < size) hexagonPoints.add(new Vector2(xe, ye));
xb = xe;
yb = ye;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.