private void CreateRivers()
        {
            for (int i = 0; i < bounds.width / 2; i++)
            {
                Corner c = corners[r.Next(corners.Count)];
                if (c.ocean || c.elevation < 0.3 || c.elevation > 0.9)
                {
                    continue;
                }

                if (r.Next(5) != 0)
                {
                    continue;
                }

                while (!c.coast)
                {
                    if (c == c.downslope)
                    {
                        break;
                    }

                    DEdge dE = LookupEdgeFromCorner(c, c.downslope);
                    if (!dE.v0.water || !dE.v1.water)
                    {
                        dE.river++;
                        c.river++;
                        c.downslope.river++;
                    }
                    c = c.downslope;
                }
            }
        }
        // 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;
                }
            }
        }
        private void BuildGraph(Voronoi v)
        {
            Dictionary <Point, Center> pCMap = new Dictionary <Point, Center>();
            List <Point> points = v.SiteCoords();

            // For each point create a center
            foreach (Point p in points)
            {
                Center c = new Center();
                c.loc   = p;
                c.index = centers.Count;
                centers.Add(c);
                pCMap[p] = c;
            }

            // For each center, assign a region
            foreach (Center c in centers)
            {
                v.Region(c.loc);
            }

            List <Edge> libEdges            = v.Edges();
            Dictionary <int, Corner> pCRMap = new Dictionary <int, Corner>();

            // For each edge in the voronoi edges
            foreach (Edge e in libEdges)
            {
                LineSegment vEdge = e.VoronoiEdge();
                LineSegment dEdge = e.DelaunayLine();
                DEdge       dE    = new DEdge();
                dE.index = edges.Count;
                edges.Add(dE);

                // Setup midpoints for viable voronoi edges for noisy edge generation
                if (vEdge.p0 != null && vEdge.p1 != null)
                {
                    dE.midPoint = InterpolatePoint(vEdge.p0, vEdge.p1, 0.5);
                }

                // Create corners for the voronoi vertices of the edge
                dE.v0 = MakeCorner(pCRMap, vEdge.p0);
                dE.v1 = MakeCorner(pCRMap, vEdge.p1);
                dE.d0 = pCMap[dEdge.p0];
                dE.d1 = pCMap[dEdge.p1];

                // Add borders for delaunay edges
                if (dE.d0 != null)
                {
                    dE.d0.borders.Add(dE);
                }
                if (dE.d1 != null)
                {
                    dE.d1.borders.Add(dE);
                }
                if (dE.v0 != null)
                {
                    dE.v0.protrudes.Add(dE);
                }
                if (dE.v1 != null)
                {
                    dE.v1.protrudes.Add(dE);
                }

                if (dE.d0 != null && dE.d1 != null)
                {
                    AddToCenterList(dE.d0.neighbors, dE.d1);
                    AddToCenterList(dE.d1.neighbors, dE.d0);
                }
                if (dE.v0 != null && dE.v1 != null)
                {
                    AddToCornerList(dE.v0.adjacent, dE.v1);
                    AddToCornerList(dE.v1.adjacent, dE.v0);
                }

                if (dE.d0 != null)
                {
                    AddToCornerList(dE.d0.corners, dE.v0);
                    AddToCornerList(dE.d0.corners, dE.v1);
                }
                if (dE.d1 != null)
                {
                    AddToCornerList(dE.d1.corners, dE.v0);
                    AddToCornerList(dE.d1.corners, dE.v1);
                }

                if (dE.v0 != null)
                {
                    AddToCenterList(dE.v0.touches, dE.d0);
                    AddToCenterList(dE.v0.touches, dE.d1);
                }
                if (dE.v1 != null)
                {
                    AddToCenterList(dE.v1.touches, dE.d0);
                    AddToCenterList(dE.v1.touches, dE.d1);
                }
            }
        }
        public System.Drawing.Color ColorWithSlope(System.Drawing.Color c, Center p, Center q, DEdge e)
        {
            Corner r = e.v0;
            Corner s = e.v1;

            if (r == null || s == null)
            {
                return(c);
            }
            else if (p.water)
            {
                return(c);
            }

            if (q != null && p.water == q.water)
            {
                c = InterpolateColor(c, GetColor(q.biome), 0.4);
            }

            System.Drawing.Color low  = InterpolateColor(c, System.Drawing.Color.FromArgb(51, 51, 51), 0.7);
            System.Drawing.Color high = InterpolateColor(c, System.Drawing.Color.FromArgb(255, 255, 255), 0.3);

            double light = CalculateLighting(p, r, s);

            if (light < 0.5)
            {
                return(InterpolateColor(low, c, light * 2));
            }
            else
            {
                return(InterpolateColor(c, high, light * 2 - 1));
            }
        }