Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by vr (Curate)
on Mar 19, 2019 at 22:27 UTC ( #1231459=note: print w/replies, xml ) Need Help??


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

A one-liner with PDL's rvals:

pdl> p rvals(19,19,{Centre=>[5,4]}) <= 6 [ [0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0] [1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0] [1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0] [1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0] [1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0] [1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0] [1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0] [1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0] [0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0] [0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] ]

Edit: I know what you are thinking: what a dumb one-liner. What if frame-buffer is 1000x1000 px, but illuminated area just 10x10? Why calculate a million distances when clearly we can limit calculations to a small viewport (bounding box)? Ok, then:

pdl> $frame = zeroes 19,19 pdl> ($x,$y,$r,$xmax,$ymax) = (5,4,6,dims $frame) pdl> pdl> use List::Util pdl> *min_ = \*List::Util::min pdl> *max_ = \*List::Util::max pdl> pdl> ($llx,$lly,$urx,$ury,$cx,$cy) = ( > max_(0,$x-$r), > max_(0,$y-$r), > min_($xmax,$x+$r), > min_($ymax,$y+$r), > min_($r,$x), > min_($r,$y)) pdl> pdl> $viewport = $frame($llx:$urx, $lly:$ury) pdl> pdl> $viewport .= $viewport-> rvals({ > Center => [$cx,$cy], > Squared => 1 > }) <= $r*$r pdl> pdl> p $frame [ [0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0] [1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0] [1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0] [1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0] [1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0] [1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0] [1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0] [1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0] [0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0] [0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] ] pdl>

Note two different kinds of assignment, for data-flow to work in PDL. This nice exercise reminded me when taking a square root multiple times was a no-no (was that so for /[2-4]87/? I think nowadays it's single CPU cycle anyway, no need to optimize (-?)), hence I used the rvals's option (but it's there for a reason, isn't it). I hope I didn't mess anything this time of night and it works for border cases, too.

Replies are listed 'Best First'.
Re^2: circular area in a coordinates grid (AoA) (updated)
by Ea (Chaplain) on Mar 21, 2019 at 22:40 UTC
    You, my friend, are a steely-eyed missile monk!

    Keep that PDL train steaming along (seems like I can only upvote you _once_, pfft)

    Ea

    Sometimes I can think of 6 impossible LDAP attributes before breakfast.

    Mojoconf was great!

Re^2: circular area in a coordinates grid (AoA) (updated)
by thechartist (Monk) on Mar 22, 2019 at 00:21 UTC

    Great minds think alike! I was just about to look into using PDL, but you beat me to it.

Log In?
Username:
Password:

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

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










    Results (75 votes). Check out past polls.

    Notices?