コード例 #1
0
        // Draws a voronoi polygon
        private void DrawPolygon(System.Drawing.Graphics g, Center c, System.Drawing.Color color, bool noisyEdges, bool smoothBlending, bool lighting, bool drawBiomes)
        {
            Corner eC1 = null;
            Corner eC2 = null;

            c.area = 0;

            foreach (Center n in c.neighbors)
            {
                DEdge e = EdgeWithCenters(c, n);

                //Experimental Color (Biome Blending)
                if (smoothBlending && drawBiomes)
                {
                    color = BlendBiome(color, c, n);
                }

                //Experimental Lighting (Light Vectors)
                if (lighting && drawBiomes)
                {
                    color = ColorWithSlope(color, c, n, e);
                }


                // If noisy edges is off, draw triangles to create voronoi polygons (center to vertices of each edge)
                if (!noisyEdges)
                {
                    if (e.v0 == null)
                    {
                        continue;
                    }

                    Corner corner1A = e.v0.border ? e.v0 : e.v1;
                    if (corner1A.border)
                    {
                        if (eC1 == null)
                        {
                            eC1 = corner1A;
                        }
                        else
                        {
                            eC2 = corner1A;
                        }
                    }

                    DrawTriangle(g, e.v0, e.v1, c, color);

                    c.area = Math.Abs(c.loc.x * (e.v0.loc.y - e.v1.loc.y)
                                      + e.v0.loc.x * (e.v1.loc.y - c.loc.y)
                                      + e.v1.loc.x * (c.loc.y - e.v0.loc.y)) / 2;
                }

                //Experimental Polygon Drawing using noisy (natural) edges
                if (noisyEdges)
                {
                    // For each of the noisy edge paths fill the polygon created betweeen them and the center
                    if (path0[e.index] != null)
                    {
                        List <Point> tPath = path0[e.index];

                        System.Drawing.Point[] tA = new System.Drawing.Point[tPath.Count + 2];
                        tA[0] = new System.Drawing.Point((int)c.loc.x, (int)c.loc.y);
                        for (int i = 0; i < tPath.Count; i++)
                        {
                            tA[i + 1] = new System.Drawing.Point((int)tPath[i].x, (int)tPath[i].y);
                        }
                        tA[tA.Count() - 1] = new System.Drawing.Point((int)c.loc.x, (int)c.loc.y);

                        g.FillPolygon(new System.Drawing.SolidBrush(color), tA);
                    }

                    if (path1[e.index] != null)
                    {
                        List <Point> tPath = path1[e.index];

                        System.Drawing.Point[] tA = new System.Drawing.Point[tPath.Count + 2];
                        tA[0] = new System.Drawing.Point((int)c.loc.x, (int)c.loc.y);
                        for (int i = 0; i < tPath.Count; i++)
                        {
                            tA[i + 1] = new System.Drawing.Point((int)tPath[i].x, (int)tPath[i].y);
                        }
                        tA[tA.Count() - 1] = new System.Drawing.Point((int)c.loc.x, (int)c.loc.y);

                        g.FillPolygon(new System.Drawing.SolidBrush(color), tA);
                    }
                }
            }

            if (eC2 != null)
            {
                if (CloseEnough(eC1.loc.x, eC2.loc.x, 1))
                {
                    DrawTriangle(g, eC1, eC2, c, color);
                }
                else
                {
                    System.Drawing.Point[] points = new System.Drawing.Point[4];
                    points[0] = new System.Drawing.Point((int)c.loc.x, (int)c.loc.y);
                    points[1] = new System.Drawing.Point((int)eC1.loc.x, (int)eC1.loc.y);
                    int tX = (int)((CloseEnough(eC1.loc.x, bounds.x, 1) || CloseEnough(eC2.loc.x, bounds.x, 0.5)) ? bounds.x : bounds.right);
                    int tY = (int)((CloseEnough(eC1.loc.y, bounds.y, 1) || CloseEnough(eC2.loc.y, bounds.y, 0.5)) ? bounds.y : bounds.bottom);
                    points[2] = new System.Drawing.Point(tX, tY);
                    points[3] = new System.Drawing.Point((int)eC2.loc.x, (int)eC2.loc.y);
                    g.FillPolygon(new System.Drawing.SolidBrush(color), points);
                    c.area += 0;
                }
            }
        }