Kilovice
Kilovice

Reputation: 37

Determining if a point is within an angle

So I'm attempting to calculate if a point is inside of an angle. While researching, I came across many terms I am unfamiliar with. Essentially from a point (A) with a 120° angle protruding from point (A). I want to test if a secondary point (B) would be inside of the angle. All that is known is the degree of the angle and the degree at which the angle is facing and the X and Y values of both points. This will all be done in Java so any and all help is appreciated!

To better explain it:

There is a point with two vectors protruding from said point. The angle that is known is the angle that is created by the protrusion of the two vectors.

Upvotes: 2

Views: 608

Answers (1)

HopefullyHelpful
HopefullyHelpful

Reputation: 1819

First of all, an angle is not defined for two points -- only for two lines.

  1. Define a line that is your 2D-space. For a line, you need a point and a direction.
  2. Calculate the normal-vector for your line (Turn your directional vector by 90°); normalize both your directional and normal vector so that sqrt(x^2+y^2) = 1.
  3. Calculate the distance vector between your initial point and the other point, this is your second line, sharing the same initial point.
  4. Calculate the dot-product of a and b:
    • a = distance vector × normal vector
    • b = distance vector × directional vector
  5. Use simple trigonometry to calculate the angle. It's the arctangent of (a/b) or (b/a).

You probably wanna take the absolute value of the result as well if you don't care about left and right.

Upvotes: 2

Related Questions