@@ -66,8 +66,9 @@ struct PoissonSampler
66
66
let normalized_width : Double = Double ( width ) / radius,
67
67
normalized_height : Double = Double ( height) / radius,
68
68
grid_width : Int = Int ( ( 2 . squareRoot ( ) * normalized_width ) . rounded ( . up) ) ,
69
- grid_height : Int = Int ( ( 2 . squareRoot ( ) * normalized_height) . rounded ( . up) )
70
- var grid = [ [ Math . DoubleV2? ] ] ( repeating: [ Math . DoubleV2? ] ( repeating: nil , count: grid_width + 4 ) , count: grid_height + 4 )
69
+ grid_height : Int = Int ( ( 2 . squareRoot ( ) * normalized_height) . rounded ( . up) ) ,
70
+ grid_stride : Int = grid_width + 4
71
+ var grid = [ Math . DoubleV2? ] ( repeating: nil , count: grid_stride * ( grid_height + 4 ) )
71
72
72
73
var queue : [ Math . DoubleV2 ]
73
74
if let seed: Math . DoubleV2 = seed
@@ -93,7 +94,7 @@ struct PoissonSampler
93
94
continue
94
95
}
95
96
96
- if PoissonSampler . attempt_insert ( candidate: candidate, into_grid: & grid)
97
+ if PoissonSampler . attempt_insert ( candidate: candidate, into_grid: & grid, grid_stride : grid_stride )
97
98
{
98
99
points. append ( ( candidate. x * radius, candidate. y * radius) )
99
100
queue. append ( candidate)
@@ -108,22 +109,28 @@ struct PoissonSampler
108
109
}
109
110
110
111
private static
111
- func attempt_insert( candidate: Math . DoubleV2 , into_grid grid: inout [ [ Math . DoubleV2 ? ] ] ) -> Bool
112
+ func attempt_insert( candidate: Math . DoubleV2 , into_grid grid: inout [ Math . DoubleV2 ? ] , grid_stride : Int ) -> Bool
112
113
{
113
- let i : Int = Int ( candidate. y * 2 . squareRoot ( ) ) + 2 ,
114
- j : Int = Int ( candidate. x * 2 . squareRoot ( ) ) + 2
114
+ let i : Int = Int ( candidate. y * 2 . squareRoot ( ) ) + 2 ,
115
+ j : Int = Int ( candidate. x * 2 . squareRoot ( ) ) + 2 ,
116
+ center : Int = i * grid_stride + j
115
117
116
- guard grid [ i ] [ j ] == nil
118
+ guard grid [ center ] == nil
117
119
else
118
120
{
119
121
return false
120
122
}
121
123
122
- let ring : [ Math . DoubleV2 ? ] = [ grid [ i - 2 ] [ j - 1 ] , grid [ i - 2 ] [ j] , grid [ i - 2 ] [ j + 1 ] ,
123
- grid [ i - 1 ] [ j - 2 ] , grid [ i - 1 ] [ j - 1 ] , grid [ i - 1 ] [ j] , grid [ i - 1 ] [ j + 1 ] , grid [ i - 1 ] [ j + 2 ] ,
124
- grid [ i ] [ j - 2 ] , grid [ i ] [ j - 1 ] , grid [ i ] [ j + 1 ] , grid [ i ] [ j + 2 ] ,
125
- grid [ i + 1 ] [ j - 2 ] , grid [ i + 1 ] [ j - 1 ] , grid [ i + 1 ] [ j] , grid [ i + 1 ] [ j + 1 ] , grid [ i + 1 ] [ j + 2 ] ,
126
- grid [ i + 2 ] [ j - 1 ] , grid [ i + 2 ] [ j] , grid [ i + 2 ] [ j + 1 ] ]
124
+ let base : ( Int , Int , Int , Int ) = ( center - 2 * grid_stride,
125
+ center - grid_stride,
126
+ center + grid_stride,
127
+ center + 2 * grid_stride)
128
+
129
+ let ring : [ Math . DoubleV2 ? ] = [ grid [ base. 0 - 1 ] , grid [ base. 0 ] , grid [ base. 0 + 1 ] ,
130
+ grid [ base. 1 - 2 ] , grid [ base. 1 - 1 ] , grid [ base. 1 ] , grid [ base. 1 + 1 ] , grid [ base. 1 + 2 ] ,
131
+ grid [ center - 2 ] , grid [ center - 1 ] , grid [ center + 1 ] , grid [ center + 2 ] ,
132
+ grid [ base. 2 - 2 ] , grid [ base. 2 - 1 ] , grid [ base. 2 ] , grid [ base. 2 + 1 ] , grid [ base. 2 + 2 ] ,
133
+ grid [ base. 3 - 1 ] , grid [ base. 3 ] , grid [ base. 3 + 1 ] ]
127
134
for cell : Math . DoubleV2 ? in ring
128
135
{
129
136
guard let occupant: Math . DoubleV2 = cell
@@ -141,7 +148,7 @@ struct PoissonSampler
141
148
}
142
149
}
143
150
144
- grid [ i ] [ j ] = candidate
151
+ grid [ center ] = candidate
145
152
return true
146
153
}
147
154
}
0 commit comments