From: Josselin Date: 2007-05-19T00:40:09+09:00 Subject: Re: Math lib (even in C or C++... nobody is perfect !) On 2007-05-18 15:38:27 +0200, Severin Schoepke said: > Hi! > > Whenever I need to use a math lib in C, I use the GNU Scientific > Library (GSL): http://www.gnu.org/software/gsl/ > However, no idea if it contains what you need... > > cheers, Severin > > > > Josselin schrieb: >> I am looking for a function to calculate the barycentre of a point list >> (GPS coordinates lat/lng) .. >> >> anybody aware of such lib or utility I could rewrite in Ruby ? bet it >> doesn't exist yet >> >> thanks for any hint >> joss got it from a PHP script.. translated into Ruby... def deg2rad(d) (d/180.0)*Math::PI end def rad2deg(r) (r/Math::PI)*180 end def self.gravity_center(placemarks) sumx = 0.0 sumy = 0.0 sumz = 0.0 for placemark in placemarks do # convert to radians lat = deg2rad(placemark[0]) lon = deg2rad(placemark[1]) # convert spherical coordinate into cartesian x = Math::cos(lat) * Math::sin(lon) y = Math::cos(lat) * Math::cos(lon) z = Math::sin(lat) # sum the vectors sumx += x sumy += y sumz += z end # convert cartesian coordinate back to spherical meanz = sumz / placemarks.nitems lon = rad2deg(Math::atan2(sumx, sumy)) meanx = sumx / placemarks.nitems meany = sumy / placemarks.nitems lat = rad2deg(Math::atan(meanz / Math::sqrt(meanx**2 + meany**2))) return [lat, lon] end