Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.)


In reply to Re: circular area in a coordinates grid (AoA) by hippo
in thread circular area in a coordinates grid (AoA) by Discipulus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (4)
    As of 2020-08-14 08:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Which rocket would you take to Mars?










      Results (75 votes). Check out past polls.

      Notices?