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.)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|