Click here to Skip to main content
15,883,883 members
Articles / All Topics

Calculate the Real Difference Between Two Angles, Keeping the Correct Sign

Rate me:
Please Sign up or sign in to vote.
4.00/5 (4 votes)
21 Feb 2010CPOL1 min read 29.4K   5   6
How to calculate the real difference between two angles, keeping the correct sign

Introduction

When you build some animations with WPF, Surface or JavaFX, you sometimes need to know how to evolve an angle. For example, you have the new angle (orientation) of an object and you have stored before the last measure of this orientation: how to calculate the evolution ?

calculateAnglesBetweenOrientationExemple1

A First Solution

"This is simple" will you say, just do this:

C#
double difference = secondAngle - firstAngle;

But this snippet leads to errors. This is because the orientation value given is relative to a certain initial value, and restart to 0° if you pass the 360°. Let's give you an example: if the object was oriented at 358° and the user turns it of 5°, you will obtain a difference of (3-358=) -355° where you actually wanted to find 5....

A Better Solution

A solution I propose is to consider that the methods are called frequently enough that if the object rotates to the left or to the right. It does not have enough time to rotate more than 180° (half a tour).

Based on this, we consider that the direction of the rotation is given by the "shortest way". If it is shorter to turn to the left to go to the new angle, then we select the angle sign which tells we have turned to the left. It may be easiest to understand by looking attentively to the image above.

An image is may be better than words:

calculateAnglesBetweenOrientationExemple2

We then have this method in C#:

C#
private double calculateDifferenceBetweenAngles(double firstAngle, double secondAngle)
  {
        double difference = secondAngle - firstAngle;
        while (difference < -180) difference += 360;
        while (difference > 180) difference -= 360;
        return difference;
 }

A Case of Use

When do use it? For example, when you build a carousel: the user can click on a specific item and the carousel rotates so it is in front of you. You then need to have the correct angle. I found no other way to do it.

Does someone have a better way to proceed?

This article was originally posted at http://blog.lexique-du-net.com/index.php

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer http://wpf-france.fr
France (Metropolitan) France (Metropolitan)
Jonathan creates software, mostly with C#,WPF and XAML.

He really likes to works on every Natural User Interfaces(NUI : multitouch, touchless, etc...) issues.



He is awarded Microsoft MVP in the "Client Application Development" section since 2011.


You can check out his WPF/C#/NUI/3D blog http://www.jonathanantoine.com.

He is also the creator of the WPF French community web site : http://wpf-france.fr.

Here is some videos of the projects he has already work on :

Comments and Discussions

 
QuestionMore efficient approach Pin
Martin Setnička9-Feb-13 2:59
Martin Setnička9-Feb-13 2:59 
GeneralA simple way Pin
KevinSW17-Jan-11 21:06
KevinSW17-Jan-11 21:06 
GeneralRe: A simple way Pin
Gernot Frisch3-Jan-12 4:57
Gernot Frisch3-Jan-12 4:57 
This does not take care of the sign (left or right direction).

I think the proper solution would be:
atan2(sin(x-y), cos(x-y)), which is very slow.
Question[My vote of 1] Does someone have a better way to proceed ? Pin
tonyt20-Feb-10 20:07
tonyt20-Feb-10 20:07 
AnswerRe: [My vote of 1] Does someone have a better way to proceed ? Pin
jmix9021-Feb-10 10:16
jmix9021-Feb-10 10:16 
GeneralRe: [My vote of 1] Does someone have a better way to proceed ? Pin
supercat924-Feb-10 12:31
supercat924-Feb-10 12:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.