Question looking for formula to convert 0 to-360 to -180 to 180

ncc1701d

Member
Joined
Aug 9, 2009
Messages
204
Reaction score
6
Points
18
Please forgive if I posted already this question. I coudnt find it on search.

When using pltex I get error. Invalid Longitude range.
I tryed entering [min max] 185 202

I am using long coordinates of 0 to 360 left to right instead of -180 to 180 left to right.

Is their a simple formula for converting my long coordinates to fit the -180 to 180 requirement that pltex seems to need?
My example was a basic one. I use decimals numbers to and looking for a formula to make life easier if conversion is required.
pltex Build Oct 16 010

thanks
 
it's not really a "formula"...

but try:

lon = (lon > 180)? lon - 360: (lon < -180)? lon +360 : lon;


does that work?
 
the question mark ? is throwing me.
Maybe turn it into verbal sentence might tell me what your thinking

---------- Post added at 10:31 PM ---------- Previous post was at 10:01 PM ----------

ok thanks.
got it now.
 
did that work?
i made that up in such a hurry that i didn't even check if it wasn't nonsense (last post before heading home on a friday...) :shifty:


i suspect i might have gotten in mixed up with the formula that would to the exact opposite thing (-180~180 to 0~360)

it's actually even simpler...

if the value is larger than 180, subtract 360 from it... that should take care of it...
in C++ would look like this:

angle = (angle > 180)? angle - 360 : angle;

and that possibly unfamiliar notation is called a "ternary operator" or as i prefer to call it "compact if-else thingy"
its quite simple, actually, if what's in the parenthesis is true, then the expresion evaluates to the results of what's between the "?" and the ":" - if it's false, then it returns the stuff after the ":"

looks odd maybe... but it's so practical you learn to love it :rolleyes:


:cheers:
 
Another technique you might try.

Assume the value you wish to convert is stored in "orig_lon" and your converted value in "lon", then this should work (purely psuedo-code, translate into your language of choice). If I've made a mistake here, someone just yell at me:

Code:
lon = (( orig_lon + 180 ) MOD 360 ) - 180
Example:
orig_lon = 270
(270 + 180) = 450
450 MOD 360 = 90
90 - 180 = -90 as desired

If you're doing this in C/C++, I'm not certain MOD would be any more efficient than the already suggested ternary method but it's worth a try. This also has one advantage over the ternary in that this will handle more cases than the ternary does, but you probably don't need that for your use since all your original values will have minimum of 0 and maximum of 360.

-- Mike
 
Code:
while (angle > 180) angle -= 360;

Uses two ideas:
- You don't need to do anything at all if the angle is less than 180. Using a ternary operator in this case is somewhat overkill, as the "else" is a no-op.
- Using a "while" instead of "if" allows you to also catch anything > 520, in case the input angles haven't already been santized to 0-360 (if this is the case, then you should probably have a corresponding one to increase angle by 360 while it's < -180)
 
Back
Top