Example #1
0
    public Vector2[][] GenerateVoronoi(Vector2[] points, Vector2 center)
    {
        eq.ClearAll();
        Cell[] allCells;

        float boundaryLow   = -1;
        float boundaryHigh  = 1;
        float boundaryLeft  = -1;
        float boundaryRight = 1;

        // Read input points.
        allCells = new Cell[points.Length];

        for (int i = 0; i < points.Length; i++)
        {
            float x = points[i].x;
            float y = points[i].y;
            allCells[i] = new Cell(0, i, x, y);
            eq.Enqueue(allCells[i]);
        }

        Vector2   centerV2 = new Vector2(center.x, center.y);
        BeachLine BL       = new BeachLine(allCells, centerV2);

        float lastY = float.MaxValue;

        while (!eq.IsEmpty())
        {
            Site eventP = eq.Dequeue();

            if (eventP.type == 0)
            {
                BL.Insert(eventP);
            }
            else if (eventP.type == 1)
            {
                BL.HandleVVEvent((VoronoiVertexPoint)eventP);
            }

            BL.Print();
        }

        Vector2[][] OutputPoints = new Vector2[points.Length][];

        for (uint i = 0; i < points.Length; ++i)
        {
            Cell cell = allCells[i];
            cell.HandleEdges(boundaryHigh, boundaryLow, boundaryLeft, boundaryRight);
            cell.Sort();

            OutputPoints[i] = new Vector2[cell.relatedVoronoiVertex.Count];
            for (int j = 0; j < cell.relatedVoronoiVertex.Count; ++j)
            {
                OutputPoints[i][j] = new Vector2(cell.relatedVoronoiVertex[j].x, cell.relatedVoronoiVertex[j].y + cell.relatedVoronoiVertex[j].radius);
            }
        }

        return(OutputPoints);
    }