P is for Practical PerlMonks

### Comment on

 Need Help??

Whoops. This is what I get for coding after my brain has already shut down for nightly maintenance.

Those quick_accept and quick_reject routines will give false rejections with longitudes near the 180 degree longitude. (not an issue with US locations, I know, but...) Also, there's a stupid bit of time-wasting going on near the top there.

Let's see if I can get it right this time:

```sub make_quick_reject {
my(\$lat,\$long,\$dist) = @_;
if (abs(\$lat) + \$maxlatdiff > PI/2) {
# special case - near poles
return sub { abs(\$lat - \$_[0]) > \$maxlatdiff; };
}
my(\$longmult);
\$longmult = 1 / cos(abs(\$lat) + \$maxlatdiff);
my(\$maxlongdiff) = \$maxlatdiff * \$longmult;
return sub { abs(\$lat - \$_[0]) > \$maxlatdiff or
( abs(\$long - \$_[1]) > \$maxlongdiff and
abs(\$long - \$_[1]) < 2*PI - \$maxlongdiff ); };
}

sub make_quick_accept {
my(\$lat,\$long,\$dist) = @_;
\$maxlatdiff /= sqrt(2);
my(\$longmult);
if (abs(\$lat) < \$maxlatdiff ) {
# special case - near equator
\$longmult = 1;
} else {
\$longmult = 1 / cos(abs(\$lat) - \$maxlatdiff);
}
my(\$maxlongdiff) = \$maxlatdiff * \$longmult;
return sub { abs(\$lat - \$_[0]) < \$maxlatdiff and
( abs(\$long - \$_[1]) < \$maxlongdiff or
abs(\$long - \$_[1]) > 2*PI - \$maxlongdiff ); };
}

And just again as a reminder, the sub's produced by these two routines accept longitudes and latitudes in RADIANS, not degrees. (Though reworking them to use degrees instead of radians should not be difficult at all)

And if anyone uses this code without understanding how it works, they deserve what they get. No warranty of anything useful implied at all; the code may not even pass perl -c.

In reply to Re: Zipcode Proximity script by fizbin
in thread Zipcode Proximity script by hacker

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.
• 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2017-08-19 04:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (310 votes). Check out past polls.

Notices?