本文介绍了如何在C ++中生成随机顶点以形成凸多边形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为一个简单的凸多边形生成一组顶点,以便使用动态编程对该多边形进行最小权重三角剖分,我考虑过取一个半径为r的圆,然后取20个顶点逆时针移动,然后我将形成20个顶点凸多边形,但是我该怎么做

I need to generate a set of vertices for a simple convex polygon to do a minimum weight triangluation for that polygon using dynamic programming , I thought about taking a circle of radius r and then take 20 vertices moving counter clock wise and then i will form a 20 vertex convex polygon but i how can i do that

我怎么知道位于半径为r的圆上的顶点?

How would i know the vertex that lies on a circle of radius r ?

还有没有比这更简单的方法来生成凸多边形的顶点

and is there another easier way of generating vertices for convex polygon other than that way

任何帮助我们都非常感谢

Any help greatly appreciated

推荐答案

btw。 +1表示该圆的好方法...

btw. +1 for nice approach with that circle ...


  1. 不关心顶点的数量

{
double x0=50.0,y0=50.0,r=50.0;  // circle params
double a,da,x,y;
// [view]                       // my view engine stuff can skip this
glview2D::_lin l;
view.pic_clear();
l.col=0x00FFFFFF;
// [/view]
for (a=0.0;a<2.0*M_PI;)         // full circle
    {
    x=x0+(r*cos(a));
    y=y0+(r*sin(a));
    a+=(20.0+(40.0*Random()))*M_PI/180.0;              // random angle step < 20,60 > degrees

    // here add your x,y point to polygon

    // [view]                   // my view engine stuff can skip this
    l.p0=l.p1;                  // just add line (lust x,y and actual x,y)
    l.p1.p[0]=x;
    l.p1.p[1]=y;
    view.lin.add(l);
    // [/view]
    }
// [view]                   // my view engine stuff can skip this
view.lin[0].p0=l.p1;        // just join first and last point in first line (was point0,point0)
// [view]
}


  • 如果已知顶点数量= N

  • if number of vertexes is known = N

    将随机步长平均设置为小于 2PI / N ,例如:

    Set random step to be on average little less then 2PI / N for example:

    da=a0+(a1*Random());
    




    • a0 = 0.75 *(2 * M_PI / N) ...最小da

    • a1 = 0.40 *(2 * M_PI / N) ... a0 +(0.5 * a1) avg = 0.95 ...小于 2PI / N

      • a0=0.75*(2*M_PI/N) ... minimal da
      • a1=0.40*(2*M_PI/N) ... a0+(0.5*a1) is avg = 0.95 ... is less then 2PI/N
      • 如果顶点数达到 N 。如果之后,顶点计数不是 N ,则从头开始重新计算所有内容,因为使用随机数不能认为总是以这种方式命中 N 个顶点!!!

        inside for add break if vertex count reach N. If after for the vertex count is not N then recompute all from beginning because with random numbers you cannot take it that you always hit N vertexes this way !!!

        上面源代码的示例输出

        PS。

        您也可以使用如果圆形不够好,则为椭圆形

        You can also use ellipse if the circle shape is not good enough

        x=x0+(rx*cos(a));
        y=y0+(ry*sin(a));
        




        • rx!= ry

          • rx != ry
          • 这篇关于如何在C ++中生成随机顶点以形成凸多边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 09-04 23:03