I'm trying to implement the kml circle generator (from http://bbs.keyhole.com/ubb/showthreaded.php/Cat/0/Number/23634/page/) in perl, but the results are horribly wrong due to what appears to be rounding errors. Since the only changes will be to the least significant digits, I end up with a long string of the same number:

Here's the relevent section of my script:

$lat1 = deg2rad($bestlat); $long1 = deg2rad($bestlong); $lat2 = deg2rad($minlat); $long2 = deg2rad($minlong); $dlat = $lat2 - $lat1; $dlong = $long2 - $long1; $a = ((sin($dlat/2)) ** 2) + cos($lat1) * cos($lat2) * ((sin($dlong/ +2)) ** 2); $c = 2 * atan2(sqrt($a), sqrt(1-$a)); #get distance between points (in meters) $d = 6378137 * $c; for($i=0;$i<=360;$i++) { $radial = deg2rad($i); $lat_rad = asin(sin($lat1) * cos($d_rad) + cos($lat1) * sin($d_rad +) * cos($radial)); $dlon_rad = atan2(sin($radial) * sin($d_rad) * cos($lat1), cos($d_ +rad)-sin($lat1) * sin($lat_rad)); $lon_rad = fmod(($long1 + $dlon_rad + pi()), 2 * pi()) - pi(); print rad2deg($lon_rad).",".rad2deg($lat_rad).",0 "; }

And here's the sort of thing I get out of it:

-123.003,49.2681,0 -123.003,49.2681,0 -123.003,49.2681,0 ...

It should be something along the lines of:

-122.891991,49.254269432823,0 -122.89197200398,49.254269324619,0 -122.89195301375,49.25426900004,0 -122.8919340351,49.254268459185,0


In reply to Trigonometry in perl by Anonymous Monk

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.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.