Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Trigonometry in perl
by cfedde (Novice) on Feb 24, 2007 at 05:19 UTC
    I'm pretty sure that the main problem in this code is that it does not use strict or warnings. Apparently the variable $d_rad is never assigned a value before it is used.
Re: Trigonometry in perl
by TOD (Friar) on Feb 24, 2007 at 03:15 UTC
    perldoc -f sprintf ?