http://qs321.pair.com?node_id=1231601


in reply to circular area in a coordinates grid (AoA)

Just for fun, here's a 2-step method that fills the inner square first and then conditionally computes a quadrant outside that but inside the outer square (and copies this to the other quadrants as it goes along). The inner square is the largest square that fits entirely inside the circle whereas the outer square is the smallest square that entirely encompasses the circle. It's not very Perlish and it doesn't do bounds checking which is why you can see part of the circle on the opposite side if you display the results.

sub illuminate{ my $centre_row = shift; my $centre_col = shift; my $radius = shift; my $insq = int ($radius / sqrt (2)); my @ans; # inner square for my $i ($centre_row - $insq .. $centre_row + $insq) { for my $j ($centre_col - $insq .. $centre_col + $insq) { push @ans, [$i, $j]; } } # outer square my $r2 = $radius * $radius; for my $i (0 .. $radius) { for my $j (0 .. $radius) { # Skip already-done inner square next if $i < $insq and $j < $insq; if ($i * $i + $j * $j < $r2) { push @ans, [$centre_row + $i, $centre_col + $j], [$centre_row - $i, $centre_col + $j], [$centre_row + $i, $centre_col - $j], [$centre_row - $i, $centre_col - $j]; } } } return @ans; }

(I changed the spelling of your variables since as a native speaker of British English it is almost impossible for me to type "center" consistently.)