[BNM] Can this convoluted JavaScript function be simplified?
Alex
alex at fatdrop.co.uk
Sat Nov 8 18:42:19 GMT 2008
Ah yes you're right... I'm guessing it's a bug with > should be >= or
something, I don't have time to look for it now. It's a good one though.
My thoughts are that splitting into quarters is a good way to approach it.
Good luck with it anyway, I'm shutting down for the evening...
=]
Premasagar Rose wrote:
> Thanks Alex.
> Hmmm - no, that doesn't seem to do it.
> I wrote a little test function to highlight differences between my
> original function and the new, simplified function. It'll log these in
> the Firebug console:
>
> ====
>
> var lat, lng, testFunc, normLatLng, testLatLng;
> testFunc = getLatLong;
>
> for (lat=-360; lat<360; lat+=45){
> for (lng=-360; lng<360; lng+=45){
> normLatLng = normalizeLatLng(lat,lng);
> testLatLng = testFunc(lat,lng);
> if (normLatLng[0] !== testLatLng[0] || normLatLng[1] !==
> testLatLng[1]){
> console.log([normLatLng, testLatLng]);
> }
> }
> }
>
> ====
>
>
> -------- Original Message --------
> Subject: Re: [BNM] Can this convoluted JavaScript function be simplified?
> From: Alex <alex at fatdrop.co.uk>
> To: Brighton New Media <bnmlist at brightonnewmedia.org>
> Date: 08/11/2008 17:19
>
>> Bit of a headbender that one!!
>>
>> I *think* this works. haven't tested much.. dunno if it's simpler or not:
>>
>> function findQuarter(coord){
>> var qtrs = Math.floor(coord/90);
>> qtrs -= (Math.floor(qtrs/4)*4);
>> return qtrs;
>> }
>>
>> function findCoord(i) {
>> if( i > 0 )
>> i = (i % 90) + (findQuarter(i)*90);
>> else
>> i = (90-(i % 90)) + (findQuarter(i)*90);
>> if( i > 180 ) i = 360 - i;
>> return i;
>> }
>>
>> function getLatLong(lat, lng) {
>> lat = findCoord(lat);
>> lng = findCoord(lng);
>> if( lng > 90 || lng < -90 ) lat = 0 - lat;
>> return [lat, lng];
>> }
>>
>>
>>
>> Premasagar Rose wrote:
>>
>>
>>> Yes, that's it.
>>> And I want them to be intelligently reassigned in this way:
>>>
>>> * Latitude beyond the poles continues around the other side:
>>> o Adding an extra 10 degrees beyond 90 degrees (which is the
>>> north pole) becomes 80 degrees latitude, with longitude
>>> switching over to the other side, e.g. [100, 50] => [80, -50]
>>> * Longitude gets wrapped around to the other side of the planet. E.g.:
>>> o -190 becomes 170
>>> o 190 becomes -170
>>>
>>> Hope that makes sense.
>>>
>>> Prem
>>>
>>>
>>>
>>>
>>> -------- Original Message --------
>>> Subject: Re: [BNM] Can this convoluted JavaScript function be simplified?
>>> From: Alex <alex at fatdrop.co.uk>
>>> To: Brighton New Media <bnmlist at brightonnewmedia.org>
>>> Date: 08/11/2008 15:52
>>>
>>>
>>>
>>>> So you just want to confine the Lat values to values between -90 and 90
>>>> and the Long between -180 and 180?
>>>>
>>>>
>>>>
>>>> Premasagar Rose wrote:
>>>>
>>>>
>>>>
>>>>
>>>>> A little challenge for the weekend, perhaps?
>>>>>
>>>>> I've written a JavaScript function that gives me the results that I
>>>>> need, but in quite a convoluted way. I've spent all my brain cells now
>>>>> and can't think how to further simplify it. It'll probably involve more
>>>>> use of modulus (%) or something.
>>>>>
>>>>> The function takes two numbers, which represent latitude and longitude
>>>>> coordinates. The idea is that it will "normalise" any invalid
>>>>> coordinates (e.g. further north than the north pole, or further west
>>>>> than Alaska), so that they become valid again. It's part of geo plugin
>>>>> for jQuery that I'm working on.
>>>>>
>>>>> Latitude runs from -90 at the south pole, to 0 at the equator, to 90 at
>>>>> the north pole.
>>>>> Longitude runs from -180 west of Alaska, to 0 in the UK, to 180 east of
>>>>> Fiji.
>>>>> For consistency, I have -180 longitude always represented as 180 (the
>>>>> two coordinates are actually the same).
>>>>>
>>>>> The results of a simplified version should be absolutely identical. Any
>>>>> suggestions on improving it?:
>>>>>
>>>>> ===
>>>>>
>>>>> function normalizeLatLng(lat, lng){
>>>>> lat = lat % 360;
>>>>> if (lat > 90 && lat <= 180){
>>>>> lat = 180 - lat;
>>>>> lng = lng - 180;
>>>>> }
>>>>> else if (lat > 180 && lat <= 270){
>>>>> lat = 180 - lat;
>>>>> lng = lng - 180;
>>>>> }
>>>>> else if (lat > 270 && lat <= 360){
>>>>> lat = lat - 360;
>>>>> }
>>>>> else if (lat < -90 && lat >= -180){
>>>>> lat = 0 - (lat + 180);
>>>>> lng = lng - 180;
>>>>> }
>>>>> else if (lat < -180 && lat >= -270){
>>>>> lat = 180 - lat;
>>>>> lng = lng - 180;
>>>>> }
>>>>> else if (lat < -270 && lat >= -360){
>>>>> lat = lat + 360;
>>>>> }
>>>>>
>>>>> if (lng <= -180 || lng > 180){
>>>>> lng = (lng % 180) - (lng > 0 ? 180 : -180);
>>>>> if (lng === -180){
>>>>> lng = 180;
>>>>> }
>>>>> }
>>>>> return [lat,lng];
>>>>> }
>>>>>
>>>>> ===
>>>>>
>>>>> I look forward to any suggestions.
>>>>>
>>>>> Cheers,
>>>>> Premasagar
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>
>>
>
>
--
http://www.fatdrop.co.uk - digital music services
http://blog.fatdrop.co.uk - music industry blog
FATDROP LTD | Registered in England and Wales | Company No. 6199983
More information about the BNMlist
mailing list. Powered by Wessex Networks