[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Josselin

5/18/2007 12:54:00 PM

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

2 Answers

Severin Schoepke

5/18/2007 1:38:00 PM

0

Hi!

Whenever I need to use a math lib in C, I use the GNU Scientific Library
(GSL): http://www.gnu.org/sof...
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
>


Josselin

5/18/2007 3:39:00 PM

0

On 2007-05-18 15:38:27 +0200, Severin Schoepke
<severin.schoepke@gmail.com> said:

> Hi!
>
> Whenever I need to use a math lib in C, I use the GNU Scientific
> Library (GSL): http://www.gnu.org/sof...
> 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