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

Re: circular area in a coordinates grid (AoA)

by Marshall (Abbot)
on Mar 20, 2019 at 01:42 UTC ( #1231469=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^2: circular area in a coordinates grid (AoA)
by Discipulus (Abbot) on Mar 20, 2019 at 12:32 UTC
    very nice, thanks for the code and for the patience Marshall,

    I noticed your code get some Use of uninitialized value in join or string if the circle goes outside of the AoA for high values of row.

    I fixed it with a condition more in the nested for loop:

    $aoa[$x][$y] = 'x' if ( (($x-$circle_x)**2 + ($y-$circle_y)**2 <= ($ci +rcle_radius**2) ) # and $x >=0 and $y >=0 ) + and $x >=0 and $y >=0 and $x <= $max )

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Thanks for the correction! I guess there is a similar problem in the y direction. I like seeing bug reports like this because it means that somebody actually ran the code! I guess some adjustment of the loop conditional is also possible to not even consider points outside of the input array.

      I very seldom use $array[][] syntax because Perl is so cool at working with references to rows. I suppose some tricky splice() statement could be used, but my brain started hurting and I went for something straightforward. Glad to have been of help.

      Update: Here is modified version without extraneous prints and better control over loop condition:

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1231469]
help
Chatterbox?
and the web crawler heard nothing...

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










    Results (75 votes). Check out past polls.

    Notices?