Victor J Mytre
Victor J Mytre

Reputation: 354

Python convert angles between two different planes of coordinates?

I do not know how to word my problem , but I am mostly interested on the logic I could use in python, since my knowledge in python is not that much regarding usage of math.

I have 2 different planes, one is fixed where North is always 0, east is 90 , south is 180, and west is 270. I have another plane of reference too where 0 is always in front of me.

Now for me this sound simple in math in my mind and in paper, but in python I just dont have a good graps on how to reflect this, wherever I am facing, its always 0 in my own plane of reference, however in the other plane of reference (north, east, south, west) I am facing any angle. And for example, lets say I have something at 70 degrees in my own plane of reference, and I know that in the Compass reference I am facing to 270 degrees (which means that at 0 degrees in my reference, i am facing to 270 degrees in compass), I want to determine at which angle (compass) is an object that in my own plane of reference is at around 170 degrees. Mathematically,I can do this by simply adding 100 degrees to Compass reference, and once I reach 360 I go back to 0 . so that means that the object is at 10 degrees in compass.

I know the answer would be simple in terms of programming , maybe an if degrees > 360, then degrees = 0. But no idea if there is an easier way in python to consider all cases (degrees <0, degrees >0) .

Upvotes: 0

Views: 485

Answers (1)

Bhavye Mathur
Bhavye Mathur

Reputation: 1077

You can use the modulo operator which returns the remainder of a division:

calculated_angle %= 360 # will keep the value between 0 & 359

So once your calculated angle (acheived by adding the angle from your frame of reference + the one on your compass) is stored in calculated_angle.

You can use the modulo operator to ensure that if the angle exceeds 360 deg, it returns back to 0 and starts counting up again. So 460 % 360 would be 100. And if the angle goes below 0, it starts back up from 360 again. So -50 % 360 would be 310.

Upvotes: 2

Related Questions