Exemple #1
0
        public List <Vector2> Generate(MyRectangle generationArea, float generationCount,
                                       float exclusionRadius)
        {
            var random = new UnsafeRandomProvider(); //todo!
            var offsetGenerationArea = new MyRectangle(0, 0, generationArea.Width, generationArea.Height);

            float cellSize = exclusionRadius / Mathf.Sqrt(2);

            var width  = generationArea.Width;
            var height = generationArea.Height;

            var grid = new SingleElementGenerationGrid(
                cellSize,
                Mathf.CeilToInt(width / cellSize), Mathf.CeilToInt(height / cellSize),
                exclusionRadius
                );

            var processList    = new GenerationRandomQueue <Vector2>();
            var acceptedPoints = new List <Vector2>(1000);

            var firstPoint = new Vector2(random.Next(0, width), random.Next(0, height));

            processList.Add(firstPoint);
            acceptedPoints.Add(firstPoint);

            grid.Set(firstPoint);

            while (!processList.Empty)
            {
                var point = processList.RandomPop();
                for (int i = 0; i < generationCount; i++)
                {
                    Vector2 newPoint = GenerateRandomPointAround(point, exclusionRadius, random);
                    if (offsetGenerationArea.Contains(newPoint) && !grid.Collides(newPoint))
                    {
                        processList.Add(newPoint);
                        acceptedPoints.Add(newPoint);
                        grid.Set(newPoint);
                    }
                }
            }
            return(acceptedPoints.Select(c => c + generationArea.DownLeftPoint).ToList());
        }
Exemple #2
0
        public List <Vector2> Generate(MyRectangle generationArea, int generationCount, float maxTries,
                                       float exclusionRadius, bool collisionsAreChecked, IIntensitySamplingProvider intensitySamplingProvider)
        {
            var random = new UnsafeRandomProvider(); //todo!
            var offsetGenerationArea = new MyRectangle(0, 0, generationArea.Width, generationArea.Height);

            float cellSize = exclusionRadius / Mathf.Sqrt(2);

            var width  = generationArea.Width;
            var height = generationArea.Height;

            var grid = new SingleElementGenerationGrid(
                cellSize,
                Mathf.CeilToInt(width / cellSize), Mathf.CeilToInt(height / cellSize),
                exclusionRadius
                );

            var acceptedPoints = new List <Vector2>(generationCount);

            int generatedPointsCount = 0;
            int triesCount           = 0;

            while (generatedPointsCount < generationCount && triesCount < maxTries)
            {
                var randomPoint = new Vector2(random.Next(0, width), random.Next(0, height));

                var randomPointValue = random.NextValue;
                var intensity        =
                    intensitySamplingProvider.Sample(
                        RectangleUtils.CalculateSubelementUv(offsetGenerationArea, randomPoint));
                if (intensity >= randomPointValue)
                {
                    if (!collisionsAreChecked || !grid.Collides(randomPoint))
                    {
                        if (collisionsAreChecked)
                        {
                            grid.Set(randomPoint);
                        }

                        acceptedPoints.Add(randomPoint);
                        generatedPointsCount++;
                    }
                }

                triesCount++;
            }
            return(acceptedPoints.Select(c => c + generationArea.DownLeftPoint).ToList());
        }