|
-
August 17th, 2012, 09:55 AM
#1
angle math question
Hello,
I have a principle angle system that looks like this:
Code:
0
|
|
-90 ----------- 90
|
|
+-180
given 2 angles angle1 and angle2, I need to write code that will determine if angle2 falls between 135 degrees and -135 degrees away from angle1. Here are some sample values:
Code:
angle1 range of angle2 values
------------------------------------
0 135 to -135
90 -135 to -45
180 -45 to 45
-90 45 to 135
Does the following algorithm work in all cases?
Code:
int angleDistance = abs(angle1-angle2);
if(angleDistance >= 135)
return true;
else
return false;
Thanks!
Last edited by aseminov; August 17th, 2012 at 10:13 AM.
-
August 19th, 2012, 02:27 PM
#2
Re: angle math question
it depends on whether or not you account for wrapparound.
angle1 = 10
angle2 = 350
that looks like 20° (actually -20°) but if you only count the progressive angle then it's 340°
-
August 20th, 2012, 08:04 AM
#3
Re: angle math question
 Originally Posted by aseminov
Does the following algorithm work in all cases?
Code:
int angleDistance = abs(angle1-angle2);
if(angleDistance >= 135)
return true;
else
return false;
Your algorithm returns true for angle1 = 170 and angle2 = -170 (distance 20°) but false for angle1 = 10 and angle2 = -10 (also distance 20°). So I would say, your algorithm is flawed.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
August 20th, 2012, 10:16 AM
#4
Re: angle math question
how can I fix the algorithm then?
Thanks!
-
August 20th, 2012, 04:48 PM
#5
Re: angle math question
 Originally Posted by aseminov
how can I fix the algorithm then?
Lets say the two angles are within their defined range to start with. If not you need a transformation to bring that about.
Then you make sure both angles start at the positive y-axis going in the clockwise direction. This means you add 360 to a negative angle. It will come in between 180 and 360 and thus lie outside the defined range but that doesn't matter because you use it as part of a calculation only.
If you now subtract the two angles and the absolute is less than 135 or greater than 360-135 (in case it's a reflex angle) the angles are between -135 and 135 away from each other, like,
Code:
if (angle1 < 0) angle1 += 360;
if (angle2 < 0) angle2 += 360;
fi = abs(angle1-angle2);
if (fi < 135 || fi > (360-135)) {
// angles are less than +-135 away from each other
}
This will also work in a standard 2D cartesian coordinate system where angles start at the positive x-axis and go in the counter-clockwise direction. I recommend you use such a system because everything becomes so much easier.
Last edited by nuzzle; August 21st, 2012 at 04:08 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|