Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
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":



  • 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 or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2021-09-22 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?