开发者

Finding if an angle lies between two points

开发者 https://www.devze.com 2023-02-14 02:21 出处:网络
This is basically just a math question. Heres what I am having troubles with... I am having a difficult time coming up with how to phrase the question, so bear with me. Basically I think I need to us

This is basically just a math question.

Heres what I am having troubles with... I am having a difficult time coming up with how to phrase the question, so bear with me. Basically I think I need to use some advanced math to accomplish this, but I do not know what I need.

I will use some illustrations to make this clear. Spam prevention doesn't let me post pictures... Here's a simple concept image though: http://radleygh.com/images/gimp-2_2011-057-00-57-26-40.bmp

Objective: Determine if several objects lie within a cone on a 2D plane

Cone Properties: Position (x, y) Angle (0-359) Spread (0-359, aka Width) Distance (0++)

I can decide the brownish lines using a simple bit of math:

Angle_A = Angle + (Spread / 2) Angle_B = Angle - (Spread / 2) Angle_Target = Point_Direction(origin, object_position)

Now I thought of comparing these with the position of each object with a simple if/then statement:

If (Angle_A > Angle_Target) && (Angle_B < Angle_Target) Then Angle_Target is between A and B

This works... untill Angle_A or Angle_B pass the 0-360 threshold. 0* is between 45* and 315*... but the above if statement wouldn't work. We can then determine which direction to check based on the size of the cone...

And what if the cone effect is larger than a 180* cone?

I'm not sure of the answer. I'm pretty sure I should be using Radians... But I do not understand the concept of Radians. if someone 开发者_开发问答can point me in the right direction, perhaps show me an example somewhere, that would be wonderful!

I will continue to do my own research in the mean time.


You may consider a simple transformation which sets your coordinate system such that Angle_B is zero. In other words, instead of testing

Angle_B < Angle_Target < Angle_A

you may also use

0 < Angle_Target - Angle_B < Angle_A - Angle_B

If you apply a modulo 360° to all terms you're logic should work:

0 < (Angle_Target - Angle_B) % 360 < (Angle_A - Angle_B) % 360


One radian is the angle made by tracing a circle's circumference by a length equal to that circle's radius. Hence there are exactly 2*PI radians in a circle.

So 2*PI radians = 360 degrees

So to convert degrees to radians, multiply by 2 * PI, then divide by 360. (Or of course, multiply by PI, divide by 180).

However, whether you work in radians or degrees should only be dictated by the library you are using. Even then, you could write wrappers which do the above calculations.

But to the main part of your question. Consider that:

sin (theta) = sin (360 + theta).

cos (theta) = cos (360 + theta).

etc.

So if you come across your cone that goes through 0 degrees, simply add 360 to both angles of the cone.

e.g. if your cone goes from -10 to +20, simply use 350 to 380 instead. And of course, when you test an angle, make sure you also add 360 to that and test both the original and added angles.

e.g. testing +5 (which is in your cone), you would test 5 (which fails) then 365 (which passes).

Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号