Example #1
0
        public static List <Vector2> GeneratePoisson(Rect rect, float minDist, int newPointsCount)
        {
            //Create the grid
            float cellSize = minDist / Mathf.Sqrt(2);

            var gridWidth  = Mathf.CeilToInt(rect.width / cellSize);
            var gridHeight = Mathf.CeilToInt(rect.height / cellSize);
            var grid       = RectGrid <Vector2?> .Rectangle(gridWidth, gridHeight);

            var map = new RectMap(Vector2.one)
                      .AnchorCellBottomLeft()
                      .WithWindow(rect)
                      .Stretch(grid);

            var processList  = new RandomQueue <Vector2>();
            var samplePoints = new List <Vector2>();

            //generate the first point randomly
            //and updates
            var firstPoint = new Vector2(Random.value * rect.width, Random.value * rect.height) + new Vector2(rect.xMin, rect.yMin);

            //update containers
            processList.Push(firstPoint);
            samplePoints.Add(firstPoint);
            grid[map[firstPoint]] = firstPoint;

            //generate other points from points in queue.
            while (!processList.IsEmpty())
            {
                var point = processList.Pop();

                for (int i = 0; i < newPointsCount; i++)
                {
                    var newPoint = GenerateRandomPointAround(point, minDist);
                    //check that the point is in the image region
                    //and no points exists in the point's neighbourhood

                    if (rect.Contains(newPoint) && !IsInNeighbourhood(grid, map, newPoint, minDist))
                    {
                        if (grid.Contains(map[newPoint]))                         //TODO: why is this necessary?
                        {
                            //update containers
                            processList.Push(newPoint);
                            samplePoints.Add(newPoint);

                            grid[map[newPoint]] = newPoint;
                        }

                        /*
                         * else
                         * {
                         * Debug.Log(newPoint);
                         * Debug.Log(map[newPoint]);
                         * Debug.Break();
                         * }
                         */
                    }
                }
            }
            return(samplePoints);
        }