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

            float cellSize = exclusionRadius;

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

            var grid = new CountingGenerationGrid(
                cellSize,
                new IntVector2(Mathf.CeilToInt(width / cellSize), Mathf.CeilToInt(height / cellSize))
                );

            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.Increment(firstPoint);


            int tryCount = 0;

            while (!processList.Empty)
            {
                var point = processList.RandomPop();
                for (int i = 0; i < generationCount; i++)
                {
                    Vector2 newPoint = GenerateRandomPointAround(point, exclusionRadius, random);

                    if (offsetGenerationArea.Contains(newPoint))
                    {
                        var maxPointsInGrid =
                            intensityProvider.Sample(
                                RectangleUtils.CalculateSubelementUv(offsetGenerationArea, newPoint)) *
                            maxPerGridCount;
                        if (grid.Retrive(newPoint) < maxPointsInGrid)
                        {
                            processList.Add(newPoint);
                            acceptedPoints.Add(newPoint);
                            grid.Increment(newPoint);
                        }
                    }
                    tryCount++;
                }
                if (tryCount > maxTries)
                {
                    break;
                }
            }
            return(acceptedPoints.Select(c => c + generationArea.DownLeftPoint).ToList());
        }
Example #2
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());
        }
Example #3
0
        public List <Vector2> Generate(MyRectangle generationArea, float generationCount,
                                       MyRange exclusionRadiusRange,
                                       IIntensitySamplingProvider intensityProvider, int maxTries)

        {
            var random = new UnsafeRandomProvider(); //todo!
            var offsetGenerationArea = new MyRectangle(0, 0, generationArea.Width, generationArea.Height);

            float cellSize = exclusionRadiusRange.Max / Mathf.Sqrt(2);

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

            var grid = new GenerationMultipointGrid(
                cellSize,
                new IntVector2(Mathf.CeilToInt(width / cellSize), Mathf.CeilToInt(height / cellSize))
                );
            //if (usedCount++ % 2 == 0)
            //{
            //    return new List<Vector2>();
            //}

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


            PointWithExclusionRadius firstPoint = null;

            while (firstPoint == null)
            {
                maxTries--;
                if (maxTries < 0)
                {
                    return(acceptedPoints);
                }

                var randX       = random.Next(0, width);
                var randY       = random.Next(0, height);
                var randomPoint = new Vector2(randY, randX);

                var exclusionRadius = CalculateExclusionRadius(randomPoint, offsetGenerationArea, exclusionRadiusRange,
                                                               intensityProvider);
                if (!exclusionRadius.HasValue)
                {
                    continue;
                }
                firstPoint = new PointWithExclusionRadius()
                {
                    ExclusionRadius = exclusionRadius.Value,
                    Point           = randomPoint
                };
            }
            ;
            processList.Add(firstPoint);
            acceptedPoints.Add(firstPoint.Point);

            grid.Set(firstPoint);

            while (!processList.Empty)
            {
                var point = processList.RandomPop();
                for (int i = 0; i < generationCount; i++)
                {
                    Vector2 newPoint = GenerateRandomPointAround(point.Point, point.ExclusionRadius, random);
                    if (offsetGenerationArea.Contains(newPoint))
                    {
                        var calculatedExclusionRadius = CalculateExclusionRadius(newPoint, offsetGenerationArea,
                                                                                 exclusionRadiusRange, intensityProvider);
                        if (!calculatedExclusionRadius.HasValue)
                        {
                            continue;
                        }
                        var newPointWithExclusionRadius = new PointWithExclusionRadius()
                        {
                            Point           = newPoint,
                            ExclusionRadius = calculatedExclusionRadius.Value
                        };

                        if (!grid.Collides(newPointWithExclusionRadius))
                        {
                            processList.Add(newPointWithExclusionRadius);
                            acceptedPoints.Add(newPoint);
                            grid.Set(newPointWithExclusionRadius);
                        }
                    }
                    maxTries--;
                }
                if (maxTries < 0)
                {
                    break;
                }
            }

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