Skip to content
View in the app

A better way to browse. Learn more.

The AVSIM Community

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Turn left? Turn right?

Featured Replies

  • Commercial Member

My maths and I have parted comapny again.... I'm trying to decide whether I need to initiate a turn to the left or to the right, depending on the current and target headings. I thought this worked, but it doesn't and after an hour or so of trying, I can no longer see the wood for the trees. Would someone be kind enough to cast an eye over it and tell me what's worong?Thank you.-Dai

int getDirection(int hdg, int target){	int direction=0;	hdg=hdg+180;	if(hdg>360)hdg-=360;	if(target<hdg)direction=2;	//turn right	else direction=1;		//turn left	return direction;}

  • Commercial Member
int getDirection(int hdg, int target) {   int direction=0;   int diff;  diff = abs(hdg-target);  if (diff <= 180)  {    if (target >= hdg)      direction = 2;    else      direction = 1;  }  else  {    if (hdg >= target)      direction = 2;    else      direction = 1;  }  return direction; }

Ed Wilson

Mindstar Aviation
My Playland - I69

Try a general function to normalise angles , which can be useful elsewhere too.

int NormaliseAngle(int a){while (a >= 360)    a = a - 360;     while (a < 0)    a = a +360;return a;}  

Then you can write

 if (Normalise(target) > Normalise(hdg))    Direction = 2;else    Direction = 1;ORNormalise(target) > Normalise(hdg) ? Direction = 2 : Direction = 1;   

To turn right if target is greater than hdg.

Gerry Howard

  • Moderator
Both whom?? I just see you and I in the thread. :(
You may have some folks "blocked from view" in your User Control Panel... :( Gerry provided a very good example as well, using a function call to normalize the angles before testing for direction.

Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator
  • Commercial Member

Ah... 'mgh'... yes, blocked: intentionally. The new forum makes blocked posts almost unnoticeable. Also, his example doesn't actually do what's required:Based on his code, using the following values:target = 270hdg = 010Normalize(270) will return 270, thus not 'normalized' at allNormalize(010) will return 010, also not 'normalized' at all270 (target) is greater than 010 (hdg) and based on his code will have you turn right... yet shortest distance is to the left.

Ed Wilson

Mindstar Aviation
My Playland - I69

  • Moderator
270 (target) is greater than 010 (hdg) and based on his code will have you turn right... yet shortest distance is to the left.
Oh, okay. I hadn't actually parsed it out, but it sure looks pretty! LOL.gif

Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator
Ah... 'mgh'... yes, blocked: intentionally. The new forum makes blocked posts almost unnoticeable. Also, his example doesn't actually do what's required:Based on his code, using the following values:target = 270hdg = 010Normalize(270) will return 270, thus not 'normalized' at allNormalize(010) will return 010, also not 'normalized' at all270 (target) is greater than 010 (hdg) and based on his code will have you turn right... yet shortest distance is to the left.
The angles are normalised in the usual way for integers to be in the range 0 to +359. Agreed the following code doesn't find the shortest distance. But then neither does your function. Try it with hdg = 2 and tgt = -359. That turns right not left Also it gives inconsistent answers - hdg = 1, tgt = 360 results in a left turn but hdg = 1, tgt = -360 results in a right turn yet -360 is the same direction as +360.

Gerry Howard

  • Commercial Member
The angles are normalised in the usual way for integers to be in the range 0 to +359. Agreed the following code doesn't find the shortest distance. But then neither does your function. Try it with hdg = 2 and tgt = -359. That turns right not left Also it gives inconsistent answers - hdg = 1, tgt = 360 results in a left turn but hdg = 1, tgt = -360 results in a right turn yet -360 is the same direction as +360.
The original request was how to calculate the shortest turn distance based on current and desired heading. Your code won't do that, and there is no such thing as a -360 heading. I've looked at the compass repeatedly, just to be certain I didn't miss anything. An autopilot wouldn't have a 'target' of a negative value. A waypoint doesn't have a desired track that's a negative value. Like anything else on a computer, garbage in: garbage out. If you're passing negative values that aren't even negative in the real world... you're going to get incorrect results.

Ed Wilson

Mindstar Aviation
My Playland - I69

The original request was how to calculate the shortest turn distance based on current and desired heading. Your code won't do that, and there is no such thing as a -360 heading. I've looked at the compass repeatedly, just to be certain I didn't miss anything. An autopilot wouldn't have a 'target' of a negative value. A waypoint doesn't have a desired track that's a negative value. Like anything else on a computer, garbage in: garbage out. If you're passing negative values that aren't even negative in the real world... you're going to get incorrect results.
A computer will calculate a negative heading - regardless of the markings on a compass. Just as easily as it will calculate one greater that 360. That's why they need to be normalised before use. Your function fails unless the angles are normalised before use.

Gerry Howard

  • Commercial Member

You are arguing to 'argue'... pretty much all you ever do and why I put you on my ignore list to begin with.Your code accomplishes one thing and only one thing... ensures that the values range between 0 and 359. It doesn't actually address the OP's request at all. Your code will never return the shortest turn direction if the target and heading values cross the 000/360 boundary. The shortest turn direction is exactly what the OP wanted to obtain, not a function to keep the heading and target within 0-359, which I'm certain a 2nd grader could explain how to accomplish (not meant as an insult... it's just really that simple).It is not unreasonable for my function to expect that both target and heading fall within the 0-359 range... and for you to imply that the expectation is incorrect is simply you wanting to argue because it's grossly important to you to always be 'right'... even if you have to actually change the discussion point to accomplish it. Something you do in almost every topic you enter into at AVSIM. Now... you're back on ignore and staying there. You've proven once again that your sole purpose for being at AVSIM is to be disruptive and argumentative.

Ed Wilson

Mindstar Aviation
My Playland - I69

I accepted that my code didn't find the shortest distance.Can you accept that this is wrong?

Normalize(270) will return 270, thus not 'normalized' at allNormalize(010) will return 010, also not 'normalized' at all
Sorry, I've just realised you won't see this.

Gerry Howard

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.