Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Ok, here is some simple code with an idea. Draw an imaginary box around the circle using tangents. The circle will be within that box. This avoids having to look at the entire @aoa, just look at the lines and the indices within those lines that could possibly have an 'x'. Improvements are certainly possible, but this is a "pure perl" solution whose approach might work fast enough depending upon what you are actually doing.
#!/usr/bin/perl use strict; use warnings; use POSIX qw/floor/; my $max = 19; #from OP my @aoa = map { [ ( 'o' ) x ($max + 1) ] } 0..$max; #from OP my $max_x_grid = @{$aoa[0]}; my $max_xi = $max_x_grid-1; #range 0..$max_xi my $max_y_grid = @aoa; my $max_yi = $max_y_grid-1; #range 0..$max_yi print "grid size is $max_x_grid x $max_y_grid\n"; print "max x index is $max_xi, max y index is $max_yi (zero based indi +cies)\n"; # Choice of coordinate system: # Lower left hand corner of grid is (0,0) # This could be upper left hand corner or other point # But with this choice: # no negative x or y indicies are allowed my ($circle_x, $circle_y) =(8,9); #Center of Circle print "Circle Center = ($circle_x, $circle_y)\n"; my $circle_radius = 5.6; print "Circle radius in fractions: $circle_radius\n"; my $max_radius_on_axis = floor($circle_radius); #"round down" is part +of spec my $top_y_index = $circle_y + $max_radius_on_axis; my $bottom_y_index = $circle_y - $max_radius_on_axis; my $left_x_index = $circle_x - $max_radius_on_axis; my $right_x_index = $circle_x + $max_radius_on_axis; print "imagine a box containing the circle using tangential lines:\n"; print "coordinates top of box: ($left_x_index,$top_y_index) to ($ri +ght_x_index,$top_y_index)\n"; print "coordinates bottom of box:($left_x_index,$bottom_y_index) to ($ +right_x_index,$bottom_y_index)\n"; print "the circle is contained within the above box!\n"; # Circle in cartesian coordinates #(x - a)**2 + (y - b)**2 = r**2 where a and b are the coordinates of t +he center (a, b) and r is the radius. for (my $y=$top_y_index; $y >= $bottom_y_index; $y--) { for (my $x=$left_x_index; $x <= $right_x_index; $x++) { $aoa[$x][$y] = 'X' if ( (($x-$circle_x)**2 + ($y-$circle_y)**2 < += ($circle_radius**2) ) and $x >=0 and $y >=0) } } foreach my $row_ref ( @aoa) { print "@$row_ref\n"; } __END__ grid size is 20 x 20 max x index is 19, max y index is 19 (zero based indicies) Circle Center = (8, 9) Circle radius in fractions: 5.6 imagine a box containing the circle using tangential lines: coordinates top of box: (3,14) to (13,14) coordinates bottom of box:(3,4) to (13,4) the circle is contained within the above box! o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o X X X X X o o o o o o o o o o o o o o X X X X X X X o o o o o o o o o o o o X X X X X X X X X o o o o o o o o o o X X X X X X X X X X X o o o o o o o o o X X X X X X X X X X X o o o o o o o o o X X X X X X X X X X X o o o o o o o o o X X X X X X X X X X X o o o o o o o o o X X X X X X X X X X X o o o o o o o o o o X X X X X X X X X o o o o o o o o o o o o X X X X X X X o o o o o o o o o o o o o o X X X X X o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
update: yes I see that there is some mess up between my printouts of index numbers and size of array (the dreaded off by one)..I leave it to others for future improvements. Ok, I see the problem, post updated: the value from -- was used later in code, my $max_xi = $max_x_grid-1;   #range 0..$max_xi is better.

more points, calling sqrt() is going to be expensive. Something like x**2 is a lot cheaper and may or may not be more expensive than x*x. This may blow you away (it did me), but now a integer multiplication like x*1234 is basically the same performance in integer situations like x+124. Most of the transistors in the typical CPU (like Intel) go for math. Math, especially floating point math is not nearly as expensive as it used to be. Fixed point math is very fast now.


In reply to Re: circular area in a coordinates grid (AoA) by Marshall
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 wandering the Monastery: (5)
    As of 2020-08-08 04:32 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Which rocket would you take to Mars?










      Results (51 votes). Check out past polls.

      Notices?