Java 创建多边形时如何在顶点之间添加n个点

Java 创建多边形时如何在顶点之间添加n个点,java,math,polygon,Java,Math,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,

假设我使用以下代码创建六边形多边形中的顶点:

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));
}
之后(其中n=1):

编辑:以下是我根据沃尔克的建议编写的当前代码:

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));
}
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;
}
float r=3.0f;
int n=1+2;//主形状顶点之间的附加顶点数
对于(int a=0;a<6;a++)
{
浮点xe=r*(浮点)数学cos(a*60*Math.PI/180f);
float ye=r*(float)Math.sin(a*60*Math.PI/180f);
如果(a>0)
{
对于(int i=1;i
这将打印其他顶点,但它们不在正确的位置


编辑:这似乎不起作用,因为我没有考虑第一个顶点的位置。

计算每边的端点,就像你已经在做的那样。然后用一个内循环引入额外的分裂点

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));

for(int i=1;i计算每边的端点,就像你已经在做的那样。然后用一个内循环引入额外的分割点

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));

for(int i=1;i根据沃尔克的建议,这里有一个有效的解决方案:

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));
}
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;
}
int size=6;
int npoints=2;
int nsegs=npoints+1;
浮动xb=r;
浮动yb=0;
添加(新矢量2(xb,yb));

对于(int a=1;a,根据沃尔克的建议,这里有一个有效的解决方案:

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));
}
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;
}
int size=6;
int npoints=2;
int nsegs=npoints+1;
浮动xb=r;
浮动yb=0;
添加(新矢量2(xb,yb));

对于(int a=1;一个不错的。@lepton注意,Volker代码中的
n
应该等于顶点之间所需的分段数,即1+额外点的数目(想想栅栏和栅栏柱)谢谢各位。我按照Volker的建议添加了内环(参见编辑的OP),但是额外的顶点没有被绘制在正确的位置,它们被绘制在六边形内部,而不是在主要顶点之间。你知道我哪里出错了吗?当你把xe,ye添加到六边形点时,你必须切换到下一行的开头,即x=xe;y=ye;很好。@lepton注意到沃尔克的
n
代码应该等于顶点之间所需的线段数量,即1+额外点的数量(想想栅栏和栅栏柱)。我按照Volker的建议添加了内环(参见编辑的OP),但是额外的顶点没有被绘制在正确的位置,它们被绘制在六边形内部,而不是主要顶点之间。你知道我哪里出错了吗?当你将xe,ye添加到六边形点时,你必须切换到下一行的开头,即x=xe;y=ye;谢谢,现在它工作了。看起来我的没有工作king,因为我没有正确设置第一个起点。虽然要绘制所有六条边,您需要在代码中将a<6更改为a<7。不客气。a<6对于六边形是正确的,因为我们正在添加第一个点(其中a==0)在循环开始之前。设置a<7将生成重复的点。很抱歉,您是正确的。我已将循环终止测试编辑为
a谢谢,现在可以使用了。我的测试似乎不起作用,因为我没有正确设置第一个起始点。不过,您需要在代码中将a<6更改为a<7,以便所有六个面都可以使用不客气。对于六边形,a<6是正确的,因为我们在循环开始之前添加第一个点(其中a==0)。设置a<7将生成重复点。很抱歉,您是正确的。我已将循环终止测试编辑为
a