Пример #1
0
        private void ImproveRandomPoints(List <Vector2> points)
        {
            for (var i = 0; i < NumLloydIterations; i++)
            {
                var voronoi = new Voronoi2.Voronoi(points, new Rectangle(0, 0, _size, _size));
                for (var index = 0; index < points.Count; index++)
                {
                    var p = points[index];

                    var region = voronoi.Region(p);
                    if (region.Count == 0)
                    {
                        continue;
                    }
                    p.X = 0.0f;
                    p.Y = 0.0f;
                    foreach (var q in region)
                    {
                        p.X += q.X;
                        p.Y += q.Y;
                    }
                    p.Y          /= region.Count;
                    p.X          /= region.Count;
                    points[index] = p;
                    region.Clear();
                }
            }
        }
Пример #2
0
        private void BuildGraph(IEnumerable <Vector2> points, Voronoi2.Voronoi voronoi)
        {
            var libEdges     = voronoi.Edges;
            var centerLookup = new Dictionary <Vector2, Center>();

            foreach (var point in points)
            {
                var p = new Center {
                    Index     = _centers.Count,
                    Point     = point,
                    Neighbors = new List <Center>(),
                    Borders   = new List <Edge>(),
                    Corners   = new List <Corner>()
                };
                _centers.Add(p);
                centerLookup[point] = p;
            }
            foreach (var p in _centers)
            {
                voronoi.Region(p.Point);
            }
            _cornerMap = new Dictionary <int, List <Corner> >();

            foreach (var libEdge in libEdges)
            {
                var dedge = libEdge.DelaunayLine();
                var vedge = libEdge.VoronoiEdge();

                var edge = new Edge {
                    Index    = _edges.Count,
                    River    = 0,
                    Midpoint = vedge.P0.HasValue && vedge.P1.HasValue ? Vector2.Lerp(vedge.P0.Value, vedge.P1.Value, 0.5f) : (Vector2?)null
                };
                _edges.Add(edge);


                edge.V0 = MakeCorner(vedge.P0);
                edge.V1 = MakeCorner(vedge.P1);
                edge.D0 = dedge.P0.HasValue ? centerLookup[dedge.P0.Value] : null;
                edge.D1 = dedge.P1.HasValue ? centerLookup[dedge.P1.Value] : null;

                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);
                }

                if (edge.D0 != null && edge.D1 != null)
                {
                    AddToCenterList(edge.D0.Neighbors, edge.D1);
                    AddToCenterList(edge.D1.Neighbors, edge.D0);
                }
                if (edge.V0 != null && edge.V1 != null)
                {
                    AddToCornersList(edge.V0.Adjacent, edge.V1);
                    AddToCornersList(edge.V1.Adjacent, edge.V0);
                }
                if (edge.D0 != null)
                {
                    AddToCornersList(edge.D0.Corners, edge.V0);
                    AddToCornersList(edge.D0.Corners, edge.V1);
                }
                if (edge.D1 != null)
                {
                    AddToCornersList(edge.D1.Corners, edge.V0);
                    AddToCornersList(edge.D1.Corners, edge.V1);
                }

                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);
                }
            }
        }
Пример #3
0
Файл: Map.cs Проект: remy22/dx11
        private void ImproveRandomPoints(List<Vector2> points)
        {
            for (var i = 0; i < NumLloydIterations; i++) {
                var voronoi = new Voronoi2.Voronoi(points, new Rectangle(0, 0, _size, _size));
                for (var index = 0; index < points.Count; index++) {
                    var p = points[index];

                    var region = voronoi.Region(p);
                    if (region.Count == 0) continue;
                    p.X = 0.0f;
                    p.Y = 0.0f;
                    foreach (var q in region) {
                        p.X += q.X;
                        p.Y += q.Y;

                    }
                    p.Y /= region.Count;
                    p.X /= region.Count;
                    points[index] = p;
                    region.Clear();
                }
            }
        }