Assets/Scripts/RandomSampler/PoissonDiscSampling.cs
/// <summary>
/// Check if candidate is a valid pt
/// </summary>
/// <param name="candidate"></param>
/// <param name="sampleRegionSize"></param>
/// <param name="cellSize"></param>
/// <param name="radius"></param>
/// <param name="points"></param>
/// <param name="grid"></param>
/// <param name="circularRegion"></param>
/// <returns></returns>
Check if candidate is a valid pt
Check if candidate is a valid pt
static bool IsValid(
        Vector2 candidate,
        Vector2 sampleRegionSize,
        float cellSize,
        float radius,
        List<Vector2> points,
        int[,] grid,
        bool circularRegion
    )
    {
        if (circularRegion &&
            !isInsideCircle(sampleRegionSize, sampleRegionSize.x / 2, candidate))
            return false;
        if (
            candidate.x >= 0
            && candidate.x < sampleRegionSize.x
            && candidate.y >= 0 
            && candidate.y < sampleRegionSize.y
        )
        {
            int cellX = (int) (candidate.x / cellSize);
            int cellY = (int) (candidate.y / cellSize);
            int searchStartX = Mathf.Max(0, cellX - 2);
            int searchEndX = Mathf.Min(cellX + 2, grid.GetLength(0) - 1);
            int searchStartY = Mathf.Max(0, cellY - 2);
            int searchEndY = Mathf.Min(cellY + 2, grid.GetLength(1) - 1);
            for (int x = searchStartX; x <= searchEndX; x++)
            {
                for (int y = searchStartY; y <= searchEndY; y++)
                {
                    int pointIndex = grid[x, y] - 1;
                    if (pointIndex != -1)
                    {
                        float sqrDst = (candidate - points[pointIndex]).sqrMagnitude;
                        if (sqrDst < radius * radius)
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
        return false;
    }
static bool IsValid ( Vector2 candidate , Vector2 sampleRegionSize , float cellSize , float radius , List < Vector2 > points , int [ , ] grid , bool circularRegion ) { if ( circularRegion && ! isInsideCircle ( sampleRegionSize , sampleRegionSize . x / 2 , candidate ) ) return false ; if ( candidate . x >= 0 && candidate . x < sampleRegionSize . x && candidate . y >= 0 && candidate . y < sampleRegionSize . y ) { int cellX = ( int ) ( candidate . x / cellSize ) ; int cellY = ( int ) ( candidate . y / cellSize ) ; int searchStartX = Mathf . Max ( 0 , cellX - 2 ) ; int searchEndX = Mathf . Min ( cellX + 2 , grid . GetLength ( 0 ) - 1 ) ; int searchStartY = Mathf . Max ( 0 , cellY - 2 ) ; int searchEndY = Mathf . Min ( cellY + 2 , grid . GetLength ( 1 ) - 1 ) ; for ( int x = searchStartX ; x <= searchEndX ; x ++ ) { for ( int y = searchStartY ; y <= searchEndY ; y ++ ) { int pointIndex = grid [ x , y ] - 1 ; if ( pointIndex != - 1 ) { float sqrDst = ( candidate - points [ pointIndex ] ) . sqrMagnitude ; if ( sqrDst < radius * radius ) { return false ; } } } } return true ; } return false ; }
Check if candidate is a valid pt
Check if candidate is a valid pt
None
candidate
Vector2
None
sampleRegionSize
Vector2
None
cellSize
float
None
radius
float
None
points
List<Vector2>
None
grid
int[,]
None
circularRegion
bool
None