| 1 | package Random::PoissonDisc; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use List::Util qw(sum); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Math::Random::MT::Auto qw(rand gaussian); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use vars qw($VERSION %grid_neighbours); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # %grid_neighbours caches the vectors pointing to | ||||||||||||||||||||||||||||
| 10 | # neighbours | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Random::PoissonDisc - distribute points aesthetically in R^n | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | my $points = Random::PoissonDisc->points( | ||||||||||||||||||||||||||||
| 19 | dimensions => [100,100], | ||||||||||||||||||||||||||||
| 20 | r => $r, | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | print join( ",", @$_),"\n" | ||||||||||||||||||||||||||||
| 23 | for @$points; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | This module allows relatively fast | ||||||||||||||||||||||||||||
| 26 | (linear in the number of points generated) generation of random points in | ||||||||||||||||||||||||||||
| 27 | I<n>-dimensional space with a distance of | ||||||||||||||||||||||||||||
| 28 | at least C<r> between each other. This distribution | ||||||||||||||||||||||||||||
| 29 | results in aesthetic so called "blue noise". | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | The algorithm was adapted from a sketch | ||||||||||||||||||||||||||||
| 32 | by Robert Bridson | ||||||||||||||||||||||||||||
| 33 | in L<https://www.cs.ubc.ca/~rbridson/docs/bridson-siggraph07-poissondisk.pdf>. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head1 DATA REPRESENTATION | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | All vectors (or points) are represented | ||||||||||||||||||||||||||||
| 38 | as anonymous arrays of numbers. All have the same | ||||||||||||||||||||||||||||
| 39 | dimension as the cardinality of the C<dimensions> | ||||||||||||||||||||||||||||
| 40 | array passed in the C<< ->points >> method. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head2 USER INTERFACE | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head3 C<< Random::PoissonDisc->points( %options ) >> | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Returns a reference to an array of points. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | Acceptable options are: | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =over 4 | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =item * | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | C< r > - minimum distance between points. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | Default is 10 units. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =item * | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | C< dimensions > - number of dimensions and respective value range as an arrayref. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | Default is | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | [ 100, 100 ] | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | meaning all points will be in R^2 , with each coordinate in the | ||||||||||||||||||||||||||||
| 67 | range [0, 100). | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =item * | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | C< candidates > - Number of candidates to inspect before deciding that no | ||||||||||||||||||||||||||||
| 72 | new neighbours can be placed around a point. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | Default is 30. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | This number may or may not need to be tweaked if you go further up in | ||||||||||||||||||||||||||||
| 77 | dimensionality beyond 3 dimensions. The more candidates you inspect | ||||||||||||||||||||||||||||
| 78 | the longer the algorithm will run for generating a number of points. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | In the algorithm description, this constant is named I<k>. | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =item * | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | C<< avoid_edge >> - The distance from the edge of the plot. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | Default is C<0> | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | If greater than zero, this will not plot points within that distance from the edge. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =item * | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | C<< center >> - Start adding points at the center of the plot. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Default is C<0> | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | If this is set to the default, the initial point will be added at a | ||||||||||||||||||||||||||||
| 97 | random position in the plot. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =back | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =cut | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub points { | ||||||||||||||||||||||||||
|
Subroutine
points: not called CC=16 SCAR=56.1
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 0 | my ($class,%options) = @_; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | $options{center} ||= 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$options{"center"} ||= 0
MC/DC0%0/1
$options{"center"} ||= 0
$options{"center"}
Truth table0%0/2
A
A= $options{"center"}
| |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | $options{avoid_edge} ||= 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$options{"avoid_edge"} ||= 0
MC/DC0%0/1
$options{"avoid_edge"} ||= 0
$options{"avoid_edge"}
Truth table0%0/2
A
A= $options{"avoid_edge"}
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | $options{candidates} ||= 30; | ||||||||||||||||||||||||||
|
Condition0%0/2
$options{"candidates"} ||= 30
MC/DC0%0/1
$options{"candidates"} ||= 30
$options{"candidates"}
Truth table0%0/2
A
A= $options{"candidates"}
| |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | $options{dimensions} ||= [100,100]; # do we only create integral points? | ||||||||||||||||||||||||||
|
Condition0%0/2
$options{"dimensions"} ||= [100, 100]
MC/DC0%0/1
$options{"dimensions"} ||= [100, 100]
$options{"dimensions"}
Truth table0%0/2
A
A= $options{"dimensions"}
| |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | $options{r} ||= 10; | ||||||||||||||||||||||||||
|
Condition0%0/2
$options{"r"} ||= 10
MC/DC0%0/1
$options{"r"} ||= 10
$options{"r"}
Truth table0%0/2
A
A= $options{"r"}
| |||||||||||||||||||||||||||||
| 111 | #$options{max} ||= 10; # we want to fill the space instead?! | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | $options{ grid } ||= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$options{"grid"} ||= {}
MC/DC0%0/1
$options{"grid"} ||= {}
$options{"grid"}
Truth table0%0/2
A
A= $options{"grid"}
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | my $grid_size = $options{ r } / sqrt( 0+@{$options{dimensions}}); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | my @result; | |||||||||||||||||||||||||||
| 117 | my @work; | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # Create a first point in our cube - either at random or in the center: | ||||||||||||||||||||||||||||
| 120 | my $p = $options{ center } | ||||||||||||||||||||||||||||
| 121 | 0 | ? [map { $_ / 2 } @{ $options{ dimensions }}] | |||||||||||||||||||||||||||
| 122 | 0 | ▶ | : [map { rnd(0,$_) } @{ $options{ dimensions }}]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | push @result, $p; | |||||||||||||||||||||||||||
| 124 | 0 | push @work, $p; | |||||||||||||||||||||||||||
| 125 | 0 | my $c = grid_coords($grid_size, $p); | |||||||||||||||||||||||||||
| 126 | 0 | $options{ grid }->{ $c } = $p; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | while (@work) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | my $origin = splice @work, int rnd(0,$#work), 1; | |||||||||||||||||||||||||||
| 130 | 0 | CANDIDATE: for my $candidate ( 1..$options{ candidates } ) { | |||||||||||||||||||||||||||
| 131 | # Create a random distance between r and 2r | ||||||||||||||||||||||||||||
| 132 | # that is, in the annulus with radius (r,2r) | ||||||||||||||||||||||||||||
| 133 | # surrounding our current point | ||||||||||||||||||||||||||||
| 134 | 0 | my $dist = rnd( $options{r}, $options{r}*2 ); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # Choose a random angle in which to point | ||||||||||||||||||||||||||||
| 137 | # this vector | ||||||||||||||||||||||||||||
| 138 | 0 | my $angle = random_unit_vector(0+@{$options{ dimensions}}); | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # Generate a new point by adding the $angle*$dist to $origin | ||||||||||||||||||||||||||||
| 141 | 0 | my $p = [map { $origin->[$_] + $angle->[$_]* $dist } 0..$#$angle]; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # Check whether our point lies within the dimensions | ||||||||||||||||||||||||||||
| 144 | 0 | for (0..$#$p) { | |||||||||||||||||||||||||||
| 145 | next CANDIDATE | ||||||||||||||||||||||||||||
| 146 | if $p->[$_] >= $options{ dimensions }->[ $_ ] - $options{ avoid_edge } | ||||||||||||||||||||||||||||
| 147 | or $p->[$_] < $options{ avoid_edge } | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$p->[$_] >= $options{"dimensions"}[$_] - $options{"avoid_edge"} or $p->[$_] < $options{"avoid_edge"}
MC/DC0%0/2
$p->[$_] >= $options{"dimensions"}[$_] - $options{"avoid_edge"} or $p->[$_] < $options{"avoid_edge"}
$p->[$_] >= $options{"dimensions"}[$_] - $options{"avoid_edge"}
$p->[$_] < $options{"avoid_edge"}
Truth table0%0/3
A or B
A= $p->[$_] >= $options{"dimensions"}[$_] - $options{"avoid_edge"}B= $p->[$_] < $options{"avoid_edge"}
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # check discs by using the grid | ||||||||||||||||||||||||||||
| 151 | # Here we should check the "neighbours" in the grid too | ||||||||||||||||||||||||||||
| 152 | 0 | my $c = grid_coords($grid_size, $p); | |||||||||||||||||||||||||||
| 153 | 0 | ▶ | if (! $options{ grid }->{ $c }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | my @n = neighbour_points($grid_size, $p, $options{ grid }); | |||||||||||||||||||||||||||
| 155 | 0 | for my $neighbour (@n) { | |||||||||||||||||||||||||||
| 156 | 0 | ▶ | if( vdist($neighbour, $p) < $options{ r }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | next CANDIDATE; | |||||||||||||||||||||||||||
| 158 | }; | ||||||||||||||||||||||||||||
| 159 | }; | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # not already in grid, no close neighbours, add it | ||||||||||||||||||||||||||||
| 162 | 0 | push @result, $p; | |||||||||||||||||||||||||||
| 163 | 0 | push @work, $p; | |||||||||||||||||||||||||||
| 164 | 0 | $options{ grid }->{ $c } = $p; | |||||||||||||||||||||||||||
| 165 | #warn "$candidate Taking"; | ||||||||||||||||||||||||||||
| 166 | } else { | ||||||||||||||||||||||||||||
| 167 | #warn "$candidate Occupied"; | ||||||||||||||||||||||||||||
| 168 | }; | ||||||||||||||||||||||||||||
| 169 | }; | ||||||||||||||||||||||||||||
| 170 | }; | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | \@result | ||||||||||||||||||||||||||||
| 173 | 0 | }; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head2 INTERNAL SUBROUTINES | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | These subroutines are used for the algorithm. | ||||||||||||||||||||||||||||
| 178 | If you want to port this module to PDL or any other | ||||||||||||||||||||||||||||
| 179 | vector library, you will likely have to rewrite these. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head3 C<< rnd( $low, $high ) >> | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | print rnd( 0, 1 ); | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Returns a uniform distributed random number | ||||||||||||||||||||||||||||
| 186 | in C<< [ $low, $high ) >>. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =cut | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | sub rnd { | ||||||||||||||||||||||||||
|
Subroutine
rnd: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 191 | 0 | my ($low,$high) = @_; | |||||||||||||||||||||||||||
| 192 | 0 | return $low + rand($high-$low); | |||||||||||||||||||||||||||
| 193 | }; | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =head3 C<< grid_coords( $grid_size, $point ) >> | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Returns the string representing the coordinates | ||||||||||||||||||||||||||||
| 198 | of the grid cell in which C<< $point >> falls. | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =cut | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | ▶ | sub grid_coords { | ||||||||||||||||||||||||||
|
Subroutine
grid_coords: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 203 | 0 | my ($size,$point) = @_; | |||||||||||||||||||||||||||
| 204 | 0 | join "\t", map { int($_/$size) } @$point; | |||||||||||||||||||||||||||
| 205 | }; | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =head3 C<< norm( @vector ) >> | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | print norm( 1,1 ); # 1.4142 | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | Returns the Euclidean length of the vector, passed in as array. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =cut | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 2000 | ▶ | sub norm { | ||||||||||||||||||||||||||
|
Subroutine
norm: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 216 | 2000 | sqrt( sum @{[map {$_**2} @_]} ); | |||||||||||||||||||||||||||
| 217 | }; | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =head3 C<< vdist( $l, $r ) >> | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | print vdist( [1,0], [0,1] ); # 1.4142 | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | Returns the Euclidean distance between two points | ||||||||||||||||||||||||||||
| 224 | (or vectors) | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =cut | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 0 | ▶ | sub vdist { | ||||||||||||||||||||||||||
|
Subroutine
vdist: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 229 | 0 | my ($l,$r) = @_; | |||||||||||||||||||||||||||
| 230 | 0 | my @connector = map { $l->[$_] - $r->[$_] } 0..$#$l; | |||||||||||||||||||||||||||
| 231 | 0 | norm(@connector); | |||||||||||||||||||||||||||
| 232 | }; | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =head3 C<< neighbour_points( $size, $point, $grid ) >> | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | my @neighbours = neighbour_points( $size, $p, \%grid ) | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | Returns the points from the grid that have a distance | ||||||||||||||||||||||||||||
| 239 | between 0 and 2r around C<$point>. These points are | ||||||||||||||||||||||||||||
| 240 | the candidates to check when trying to insert a new | ||||||||||||||||||||||||||||
| 241 | random point into the space. | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =cut | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 0 | ▶ | sub neighbour_points { | ||||||||||||||||||||||||||
|
Subroutine
neighbour_points: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 246 | 0 | my ($size,$point,$grid) = @_; | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 0 | my $dimension = 0+@$point; | |||||||||||||||||||||||||||
| 249 | 0 | my $vectors; | |||||||||||||||||||||||||||
| 250 | 0 | ▶ | if (! $grid_neighbours{ $dimension }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 0 | my @elements = (-1,0,1); | |||||||||||||||||||||||||||
| 252 | $grid_neighbours{ $dimension } = | ||||||||||||||||||||||||||||
| 253 | # Count up, and use the number in ternary as our odometer | ||||||||||||||||||||||||||||
| 254 | [map { | ||||||||||||||||||||||||||||
| 255 | 0 | my $val = $_; | |||||||||||||||||||||||||||
| 256 | my $res = [ map { | ||||||||||||||||||||||||||||
| 257 | 0 | my $res = $elements[ $val % 3 ]; | |||||||||||||||||||||||||||
| 258 | 0 | $val = int($val/3); | |||||||||||||||||||||||||||
| 259 | 0 | $res | |||||||||||||||||||||||||||
| 260 | } 1..$dimension ]; | ||||||||||||||||||||||||||||
| 261 | } (1..3**$dimension) | ||||||||||||||||||||||||||||
| 262 | ]; | ||||||||||||||||||||||||||||
| 263 | }; | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 0 | my @coords = split /\t/, grid_coords( $size, $point ); | |||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | # Find the elements in the grid according to the offsets | ||||||||||||||||||||||||||||
| 268 | map { | ||||||||||||||||||||||||||||
| 269 | 0 | my $e = $_; | |||||||||||||||||||||||||||
| 270 | 0 | my $n = join "\t", map { $coords[$_]+$e->[$_] } 0..$#$_; | |||||||||||||||||||||||||||
| 271 | # Negative grid positions never get filled, conveniently! | ||||||||||||||||||||||||||||
| 272 | 0 | ▶ | $grid->{ $n } ? $grid->{ $n } : () | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 0 | } @{ $grid_neighbours{ $dimension }}; | |||||||||||||||||||||||||||
| 274 | }; | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =head3 C<< random_unit_vector( $dimensions ) >> | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | print join ",", @{ random_unit_vector( 2 ) }; | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | Returns a vector of unit length | ||||||||||||||||||||||||||||
| 281 | pointing in a random uniform distributed | ||||||||||||||||||||||||||||
| 282 | I<n>-dimensional direction | ||||||||||||||||||||||||||||
| 283 | angle | ||||||||||||||||||||||||||||
| 284 | and returns a unit vector pointing in | ||||||||||||||||||||||||||||
| 285 | that direction | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | The algorithm used is outlined in | ||||||||||||||||||||||||||||
| 288 | Knuth, _The Art of Computer Programming_, vol. 2, | ||||||||||||||||||||||||||||
| 289 | 3rd. ed., section 3.4.1.E.6. | ||||||||||||||||||||||||||||
| 290 | but has not been verified formally or mathematically | ||||||||||||||||||||||||||||
| 291 | by the module author. | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | =cut | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 1000 | ▶ | sub random_unit_vector { | ||||||||||||||||||||||||||
|
Subroutine
random_unit_vector: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 296 | 1000 | my ($dimensions) = @_; | |||||||||||||||||||||||||||
| 297 | 1000 | my (@vec,$len); | |||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | # Create normal distributed coordinates | ||||||||||||||||||||||||||||
| 300 | RETRY: { | ||||||||||||||||||||||||||||
| 301 | 1000 | @vec = map { gaussian() } 1..$dimensions; | |||||||||||||||||||||||||||
| 302 | 1000 | $len = norm(@vec); | |||||||||||||||||||||||||||
| 303 | 1000 | ▶ | redo RETRY unless $len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 304 | }; | ||||||||||||||||||||||||||||
| 305 | # Normalize our vector so we get a unit vector | ||||||||||||||||||||||||||||
| 306 | 1000 | @vec = map { $_ / $len } @vec; | |||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | \@vec | ||||||||||||||||||||||||||||
| 309 | 1000 | }; | |||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 2 | 1; | |||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | =head1 TODO | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | The module does not use L<PDL> or any other | ||||||||||||||||||||||||||||
| 316 | vector library. | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | =head1 REPOSITORY | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | The public repository of this module is | ||||||||||||||||||||||||||||
| 321 | L<https://github.com/Corion/random-poissondisc>. | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | The public support forum of this module is | ||||||||||||||||||||||||||||
| 326 | L<https://perlmonks.org/>. | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | =head1 BUG TRACKER | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | Please report bugs in this module via the RT CPAN bug queue at | ||||||||||||||||||||||||||||
| 331 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=Random-PoissonDisc> | ||||||||||||||||||||||||||||
| 332 | or via mail to L<random-poissondisc@rt.cpan.org>. | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | Max Maischein C<corion@cpan.org> | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | =head1 COPYRIGHT (c) | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | Copyright 2011-2023 by Max Maischein C<corion@cpan.org>. | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | This module is released under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | =cut | ||||||||||||||||||||||||||||