Example #1
0
        private List <Delaunay.DVertex> GenVertices(int vertexCount, float innerRadius, float outterRadius, int slpiceSegment, FPseudoRandom rnd)
        {
            float cx = .5f * this.col;
            float cy = .5f * this.row;
            float hw = .5f * (this.col - 1);
            float hh = .5f * (this.row - 1);
            List <Delaunay.DVertex> vertices = new List <Delaunay.DVertex>();

            for (int i = 0; i < vertexCount; i++)
            {
                //横轴半径
                int r1 = ( int )(hw * (rnd.NextFloat() * (outterRadius - innerRadius) + innerRadius));
                //纵轴半径
                int r2 = ( int )(hh * (rnd.NextFloat() * (outterRadius - innerRadius) + innerRadius));
                //随机角度
                float sita = rnd.NextFloat() * MathUtils.PI * 2f;
                //椭圆极坐标参数方程
                int x = ( int )(cx + r1 * MathUtils.Cos(sita));
                int y = ( int )(cy + r2 * MathUtils.Sin(sita));

                vertices.Add(new Delaunay.DVertex(x, y));
            }
            //取半径的平均单位比例
            float avgRadius = (outterRadius + innerRadius) * 0.5f;
            //椭圆周长近似公式
            float p = MathUtils.Sqrt(0.5f * avgRadius * avgRadius * (hw * hw + hh * hh));
            //按给定的分段数计算阈值
            float threshold = 2f * MathUtils.PI * p / slpiceSegment;

            //去掉距离较近的顶点
            TripVertex(vertices, threshold);
            return(vertices);
        }
Example #2
0
        private Graph2D CreateFullDigraph(FPseudoRandom random)
        {
            Graph2D graph = new Graph2D(this.row, this.col);

            for (int i = 1; i < this.row - 1; i++)
            {
                for (int j = 1; j < this.col - 1; j++)
                {
                    int       cur  = i * this.col + j;
                    GraphNode node = graph[cur];
                    if (j < this.col - 2)
                    {
                        node.AddEdge(cur, cur + 1, random.NextFloat());
                    }
                    if (j > 1)
                    {
                        node.AddEdge(cur, cur - 1, random.NextFloat());
                    }
                }
            }
            for (int i = 1; i < this.col - 1; i++)
            {
                for (int j = 1; j < this.row - 1; j++)
                {
                    int       cur  = j * this.col + i;
                    GraphNode node = graph[cur];
                    if (j < this.row - 2)
                    {
                        node.AddEdge(cur, cur + this.col, random.NextFloat());
                    }
                    if (j > 1)
                    {
                        node.AddEdge(cur, cur - this.col, random.NextFloat());
                    }
                }
            }
            return(graph);
        }