Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: circular area in a coordinates grid (AoA) -- updated

by Discipulus (Canon)
on Mar 19, 2019 at 22:44 UTC ( [id://1231462]=note: print w/replies, xml ) Need Help??


in reply to Re: circular area in a coordinates grid (AoA)
in thread circular area in a coordinates grid (AoA)

Fantastic!

I suppose i can optimize it using for my $row ( $center_r - $radius .. $center_r + $radius ) instead of processing the whole AoA

I never seen >> before: seems a int( $_ / 2) operator.. thanks for this too!

L*

update

I adapted to return a hash instead. I feel happy with it. Thanks again

#!/usr/bin/perl use strict; use warnings; use List::Util qw( max min ); my $max = 19; #from OP my @aoa = map { [ ( 'o' ) x ($max + 1) ] } 0..$max; #from OP my %illu = illuminate( 0,0,12 ); display(@aoa); %illu = illuminate( 0,10,4 ); display(@aoa); %illu = illuminate( 10,10,6 ); display(@aoa); %illu = illuminate( 10,0,5.5 ); display(@aoa); sub illuminate{ my $center_r = shift; my $center_c = shift; my $radius = shift; my %ret; foreach my $row ( $center_r - $radius .. $center_r + $radius ){ my $delta_x = $radius ** 2 - ($center_r - $row) ** 2; if( $delta_x >= 0 ){ $delta_x = int sqrt $delta_x; my $low = max 0, $center_c - $delta_x; my $high = min $#{ $aoa[$row] }, $center_c + $delta_x; map { $ret{ $row.'_'.$_ }++ } $low .. $high; } } return %ret; } sub display{ print "\n"; foreach my $row ( 0..$#aoa ){ foreach my $col ( 0..$#{$aoa[$row]} ){ print $illu{$row.'_'.$col} ? ' ' : $aoa[$row][$col] ; + } print "\n" } }

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.

Replies are listed 'Best First'.
Re^3: circular area in a coordinates grid (AoA)
by LanX (Saint) on Mar 19, 2019 at 22:55 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (None)
    As of 2024-04-25 01:05 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found