void Start()
    {
        // Create your sites center off vornoi cells
        //these points are used as spawn positions for the trash
        List <Vector2f> points = CreateRandomPoint();

        // Create the bounds of the voronoi diagram
        Rectf bounds = new Rectf(0, 0, AreaDimensions, AreaDimensions);

        // There is a two ways you can create the voronoi diagram: with or without the lloyd relaxation
        // Vornoi with lloyd relaxation
        Voronoi voronoi = new Voronoi(points, bounds);

        if (UseLloydRelaxation)
        {
            voronoi.LloydRelaxation(LloydFactor);
        }

        // Now retreive the edges from it, and the new sites position if you used lloyd relaxtion
        sites = voronoi.SitesIndexedByLocation;
        //edge calculation is not needed
        //edges = voronoi.Edges;

        Display();
    }
    void itterateTheVoronoi()
    {
        for (var i = 0; i < allPoints.Count; i++)
        {
            points[i] = new Vector2f(allPoints[i].X, allPoints[i].Y);
        }


        //now Create a Voronoi from it
        Voronoi voronoi = new Voronoi(points, bounds);

        //call the Llyod Relaxiation Function
        voronoi.LloydRelaxation(1);
        sites = voronoi.SitesIndexedByLocation;
        Debug.Log("created Voronoi");



        //and turn it back into Points
        int counterForEach = 0;

        foreach (KeyValuePair <Vector2f, Site> kv in sites)
        {
            points[counterForEach] = new Vector2f(kv.Key.x, kv.Key.y);
            counterForEach++;
        }
    }
Beispiel #3
0
    void GetCloser()
    {
        Debug.Log("starting next Itteration");
        //set new sites as Points
        int iC = 0;

        foreach (KeyValuePair <Vector2f, Site> kv in sites)
        {
            points[iC] = new Vector2f(kv.Key.x, kv.Key.y);
            iC++;
        }
        Debug.Log("Points from Sites");


        //move those Points towards a random darker Pixel
        for (int i = 0; i < points.Count; i++)
        {
            float           currentBrigthness = ColorToBrightness(fromImage.GetPixel(Mathf.RoundToInt(points[i].x), Mathf.RoundToInt(points[i].y)));
            float           frX          = points[i].x;
            float           frY          = points[i].y;
            List <Vector2f> darkerPixels = new List <Vector2f>();

            for (int xPix = 0; xPix < cR * 2; xPix++)
            {
                for (int yPix = 0; yPix < cR * 2; yPix++)
                {
                    if (frX - cR + xPix > 0 && frX - cR + xPix < bW && frY - cR + yPix > 0 && frY - cR + yPix < bH)

                    {
                        float toCom = ColorToBrightness(fromImage.GetPixel(Mathf.RoundToInt(frX - cR + xPix), Mathf.RoundToInt(frY - cR + yPix)));
                        if (toCom < currentBrigthness)
                        {
                            darkerPixels.Add(new Vector2f(frX - cR + xPix, frY - cR + yPix));
                        }
                    }
                }
            }

            if (darkerPixels.Count > 0)
            {
                int nPi = Mathf.FloorToInt(Random.Range(0, darkerPixels.Count));


                points[i] = new Vector2f(darkerPixels[nPi].x, darkerPixels[nPi].y);
            }
        }
        Debug.Log("moved to darker pixels");

        voronoi = new Voronoi(points, bounds);
        voronoi.LloydRelaxation(1);
        sites = voronoi.SitesIndexedByLocation;
        edges = voronoi.Edges;


        Debug.Log("new Diagram and Relaxed");

        DisplayVoronoiDiagram();
        Debug.Log("Draw Diagram");
    }
    // Created to generate a Voronoi animation with Lloyd relaxation applied
    void Update()
    {
        voronoi.LloydRelaxation(count);
        sites = voronoi.SitesIndexedByLocation;
        edges = voronoi.Edges;

        DisplayVoronoiDiagram();

        count++;
        count = count % 10;

        if (count == 1)
        {
            List <Vector2f> points = CreateRandomPoint();
            Rectf           bounds = new Rectf(0, 0, 512, 512);
            voronoi = new Voronoi(points, bounds);
        }
    }
        public ActionResult GenerateFixedMap()
        {
            var points = new List <Vector2f>();

            /*
             * points.Add(new Vector2f(100, 100));
             * points.Add(new Vector2f(200, 300));
             * points.Add(new Vector2f(40, 60));
             * points.Add(new Vector2f(50, 150));
             * points.Add(new Vector2f(256, 123));
             * points.Add(new Vector2f(545, 411));
             */
            var r = new Random();

            while (points.Count < 200)
            {
                var p = new Vector2f(r.Next(10, 900), r.Next(10, 500));
                if (!points.Contains(p))
                {
                    points.Add(p);
                }
            }

            var bounds = new Rectf(10, 10, 950, 530);

            var voronoi = new Voronoi(points, bounds);

            voronoi.LloydRelaxation(2);

            var result = new GenerateFixedMapResult();

            result.Diagram    = voronoi.VoronoiDiagram();
            result.HullPoints = voronoi.HullPointsInOrder();

            return(Json(result, JsonRequestBehavior.AllowGet));
        }
Beispiel #6
0
    // Create a graph containing all connected empty areas
    private void CreateBaseGraph(int lloydIterations)
    {
        // Create uniform random point distribution and Voronoi Diagram
        var centers = new List <Vector2f>();

        for (int i = 0; i < _voronoiSamples; i++)
        {
            var x = Random.Range(0f, MapSize);
            var y = Random.Range(0f, MapSize);
            centers.Add(new Vector2f(x, y));
        }
        VoronoiDiagram = new Voronoi(centers, new Rectf(0, 0, MapSize, MapSize));

        // Apply Lloyd Relaxation
        VoronoiDiagram.LloydRelaxation(lloydIterations);

        // Assign area segments to initial areas
        foreach (var site in VoronoiDiagram.SiteCoords())
        {
            bool isOnBorder = false;
            var  segments   = VoronoiDiagram.VoronoiBoundaryForSite(site);

            foreach (var segment in segments)
            {
                if (!(segment.p0.x <= VoronoiDiagram.PlotBounds.left) &&
                    !(segment.p0.x >= VoronoiDiagram.PlotBounds.right) &&
                    !(segment.p0.y <= VoronoiDiagram.PlotBounds.top) &&
                    !(segment.p0.y >= VoronoiDiagram.PlotBounds.bottom) &&
                    !(segment.p1.x <= VoronoiDiagram.PlotBounds.left) &&
                    !(segment.p1.x >= VoronoiDiagram.PlotBounds.right) &&
                    !(segment.p1.y <= VoronoiDiagram.PlotBounds.top) &&
                    !(segment.p1.y >= VoronoiDiagram.PlotBounds.bottom))
                {
                    continue;
                }

                isOnBorder = true;
                break;
            }

            // Assign areaSegment to site and corresponding area
            var areaSegment = new AreaSegment(isOnBorder ? AreaSegment.EAreaSegmentType.Border : AreaSegment.EAreaSegmentType.Empty);

            var nodeID = AreaSegmentGraph.AddNode(areaSegment);
            _siteAreaSegmentMap.Add(site, nodeID);
            _areaSegmentCenterMap.Add(nodeID, site.ToUnityVector2());
        }

        // Create navigation graph - for each area segment that is not a border, add reachable neighbors
        foreach (var id in _siteAreaSegmentMap)
        {
            var areaSegment = AreaSegmentGraph.GetNodeData(id.Value);
            if (areaSegment.Type == AreaSegment.EAreaSegmentType.Border)
            {
                continue;
            }

            Vector2 center = _areaSegmentCenterMap[id.Value];
            foreach (var neighbor in VoronoiDiagram.NeighborSitesForSite(new Vector2f(center.x, center.y)))
            {
                var neighborSegment = AreaSegmentGraph.GetNodeData(_siteAreaSegmentMap[neighbor]);
                if (neighborSegment.Type != AreaSegment.EAreaSegmentType.Border)
                {
                    AreaSegmentGraph.AddEdge(_siteAreaSegmentMap[neighbor], id.Value, (int)AreaSegment.EAreaSegmentEdgeType.NonNavigable);
                }
            }
        }
    }