Math lib (even in C or C++... nobody is perfect !)

J

Josselin

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
 
J

Josselin

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,252
Messages
2,571,267
Members
47,908
Latest member
MagdalenaR

Latest Threads

Top