# Modulating and Angle Problem

This topic is 3382 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a problem with I am Modulating AngleA to keep it positive and Never more than a single Radian by the following: AngleA = fmod(AngleA,(PI * 2)); if(AngleA < 0) { AngleA += (PI * 2); } This is fine but However. . . Later in the program I need to caculate if AngleA is within a certain range of AngleB. The trouble is that If I use the following Code: if(AngleA < (AngleB + 0.3) && AngleA > (AngleB - 0.3)) { //Do Things Here } Then I have a problem if AngleB is hovering around zero as it will generate a minus which won't be factored in. So if I was instead to do this: if(AngleA < fmod(AngleB + 0.3,PI * 2) && AngleA > fmod(AngleB - 0.3,PI * 2)) { //Do Things Here } This still don't work as now the modulated minus is now greater that the Original Angle so is false. Any Ideas. I'm sure that I'm missing something really simple here! Many Thanks David

##### Share on other sites
First of all, you probably don't need to use angles: Your life will be easier if instead of an angle you keep a vector that contains (cos(angle),sin(angle)).

If you are working with angles, you should probably use a function that computes the distance between angles:
double angle_distance(double a, double b) {  double answer = fmod(abs(a-b), 2*PI);  return min(answer, 2*PI-answer);}

if (angle_distance(AngleA, AngleB) < 0.3)  // ...

##### Share on other sites
Quote:
 Original post by alvaroYour life will be easier if instead of an angle you keep a vector that contains (cos(angle),sin(angle)).

...in which case the signed angle between two vectors u1 and u2 is given by,

theta = asin(cross2d(u1, u2))

where cross2d denotes the "scalar-valued 2d cross product,"

                 | u1_x   u2_x |cross2d(u1,u2) = |             | = u1_x u2_y - u2_x u1_y                 | u1_y   u2_y |

which is easy to compute; it's just two MULS, a SUB, and no branches.

##### Share on other sites
Quote:
 Original post by alvaroIf you are working with angles, you should probably use a function that computes the distance between angles:double angle_distance(double a, double b) { double answer = fmod(abs(a-b), 2*PI); return min(answer, 2*PI-answer);}

Thanks That's Exactly What I Needed! :)

##### Share on other sites
Quote:
Quote:
 Original post by alvaroIf you are working with angles, you should probably use a function that computes the distance between angles:double angle_distance(double a, double b) { double answer = fmod(abs(a-b), 2*PI); return min(answer, 2*PI-answer);}

Thanks That's Exactly What I Needed! :)

You are welcome. I believe what you really need is the part of the post you didn't quote (Don't use angles), but it may take some time to sink in.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
633714
• Total Posts
3013491
×