예제 #1
0
        public VoronoiGraph(FortunesAlgorithm <PortableColor> v, int numLloydRelaxations, Random r)
        {
            this.r     = r;
            bumps      = r.Next(5) + 1;
            startAngle = r.NextDouble() * 2 * Math.PI;
            dipAngle   = r.NextDouble() * 2 * Math.PI;
            dipWidth   = r.NextDouble() * .5 + .2;
            bounds     = v.PlotBounds;
            for (int i = 0; i < numLloydRelaxations; i++)
            {
                var points = v.GetSiteCoordinates();
                for (var j = 0; j < points.Count; j++)
                {
                    var    region = v.GetRegion(points[j]);
                    double x      = 0;
                    double y      = 0;
                    foreach (var c in region)
                    {
                        x += c.X;
                        y += c.Y;
                    }
                    x        /= region.Count;
                    y        /= region.Count;
                    points[j] = new PointDouble(x, y);
                }
                v = new FortunesAlgorithm <PortableColor>(points, null, v.PlotBounds);
            }
            BuildGraph(v);
            ImproveCorners();

            AssignCornerElevations();
            AssignOceanCoastAndLand();
            RedistributeElevations(LandCorners());
            AssignPolygonElevations();

            CalculateDownslopes();
            AssignNormals();
            //calculateWatersheds();
            CreateRivers();
            AssignCornerMoisture();
            RedistributeMoisture(LandCorners());
            AssignPolygonMoisture();
            AssignBiomes();

            if (DrawingHook.ImageFactory != null)
            {
                pixelCenterMap = DrawingHook.ImageFactory.CreateBitmap32bppArgb((int)bounds.width, (int)bounds.width);
            }
        }
예제 #2
0
        private void BuildGraph(FortunesAlgorithm <PortableColor> v)
        {
            var pointCenterMap = new Dictionary <PointDouble, Center>();
            var points         = v.GetSiteCoordinates();

            points.ForEach((p) =>
            {
                var c   = new Center();
                c.loc   = p;
                c.index = centers.Count;
                centers.Add(c);
                pointCenterMap[p] = c;
            });

            //bug fix
            centers.ForEach((c) =>
            {
                v.GetRegion(c.loc);
            });

            var libedges       = v.Edges;
            var pointCornerMap = new Dictionary <int, Corner>();

            foreach (var libedge in libedges)
            {
                LineSegment vEdge = libedge.VoronoiEdge();
                LineSegment dEdge = libedge.DelaunayLine();

                var edge = new Edge();
                edge.index = edges.Count;
                edges.Add(edge);

                edge.v0 = MakeCorner(pointCornerMap, vEdge.p0);
                edge.v1 = MakeCorner(pointCornerMap, vEdge.p1);
                edge.d0 = pointCenterMap[dEdge.p0];
                edge.d1 = pointCenterMap[dEdge.p1];

                // Centers point to edges. Corners point to edges.
                if (edge.d0 != null)
                {
                    edge.d0.borders.Add(edge);
                }
                if (edge.d1 != null)
                {
                    edge.d1.borders.Add(edge);
                }
                if (edge.v0 != null)
                {
                    edge.v0.protrudes.Add(edge);
                }
                if (edge.v1 != null)
                {
                    edge.v1.protrudes.Add(edge);
                }

                // Centers point to centers.
                if (edge.d0 != null && edge.d1 != null)
                {
                    AddToCenterList(edge.d0.neighbors, edge.d1);
                    AddToCenterList(edge.d1.neighbors, edge.d0);
                }

                // Corners point to corners
                if (edge.v0 != null && edge.v1 != null)
                {
                    AddToCornerList(edge.v0.adjacent, edge.v1);
                    AddToCornerList(edge.v1.adjacent, edge.v0);
                }

                // Centers point to corners
                if (edge.d0 != null)
                {
                    AddToCornerList(edge.d0.corners, edge.v0);
                    AddToCornerList(edge.d0.corners, edge.v1);
                }
                if (edge.d1 != null)
                {
                    AddToCornerList(edge.d1.corners, edge.v0);
                    AddToCornerList(edge.d1.corners, edge.v1);
                }

                // Corners point to centers
                if (edge.v0 != null)
                {
                    AddToCenterList(edge.v0.touches, edge.d0);
                    AddToCenterList(edge.v0.touches, edge.d1);
                }
                if (edge.v1 != null)
                {
                    AddToCenterList(edge.v1.touches, edge.d0);
                    AddToCenterList(edge.v1.touches, edge.d1);
                }
            }
        }