2

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.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Kilovice
  • 37
  • 2
  • I assume you'd have to use trig at some point... but I'm not sure exactly how. Great question! – RobotKarel314 Jun 15 '16 at 10:26
  • Yes, I do believe so. Unfortunately, I'm not that good at math. – Kilovice Jun 15 '16 at 10:29
  • 1
    "All that is known is the degree of the ..." of the what? – Mr. Polywhirl Jun 15 '16 at 10:30
  • 4
    Points don't have angles. Are you talking about a straight line passing through a that has a 120 degree angle with your system's x axis? – starturtle Jun 15 '16 at 10:30
  • 3
    Why don't you draw it? :) – Katona Jun 15 '16 at 10:31
  • What's the "degree at which the angle is facing"? One picture is worth a thousand words. – Sergey Kalinichenko Jun 15 '16 at 10:31
  • look up [viewing cone](https://en.wikipedia.org/wiki/Viewing_cone) and follow the math – BeyelerStudios Jun 15 '16 at 10:34
  • @BeyelerStudios I understand that part, it's the way he specifies the position of the frustum that's unclear to me. – Sergey Kalinichenko Jun 15 '16 at 10:34
  • @BeyelerStudios Yeah, I guess that's right. All I'd know is the angle of the cone and where both points are. Also I do apologize if my terminology is incorrect, math isn't exactly my strong point... – Kilovice Jun 15 '16 at 10:35
  • @BeyelerStudios So basically, determining if an object is in the field of vision? – Mr. Polywhirl Jun 15 '16 at 10:35
  • 2
    Look up [dot product](https://en.wikipedia.org/wiki/Dot_product). If you have vectors AX and AY, then you can determine that vector AB is between AX and AY by checking the sign of dot products (AX AY), (AX AB), and (AB AY). If the signs are the same, AB is between AX and AY. – Sergey Kalinichenko Jun 15 '16 at 10:39
  • Maybe it can be solved by checking if point `B` is on the right side of one line determined by the vector and the left side of the other. Determining which side a point is of a line can be done this way: http://stackoverflow.com/questions/1560492/how-to-tell-whether-a-point-is-to-the-right-or-left-side-of-a-line – Katona Jun 15 '16 at 10:39
  • 1
    I can see that people think it's a funny question but basically this is just another "write my code for me". So it would be "too broad" as there are plenty of ways to write code for this. – Erwin Bolwidt Jun 15 '16 at 10:41
  • @ErwinBolwidt there may be multiple solutions, yes, but I don't think that makes it broad. – Katona Jun 15 '16 at 10:42
  • @dasblinkenlight Is there a way to find vectors when only knowing the angle though? – Kilovice Jun 15 '16 at 10:44
  • @erwin-bolwidt or maybe it's a "I'm bad at math can you explain this?" – Kilovice Jun 15 '16 at 10:50
  • Why don't you label [this image](http://i.imgur.com/eoVEpRb.png) or create one yourself. Also, try to add some Java code with your attempt. – Mr. Polywhirl Jun 15 '16 at 10:57
  • @Kilovice Since the length of the vector does not matter, you can use cos and sin of an angle as x and y coordinates of its corresponding vector. Translating coordinates so that point A is the origin should help, too. – Sergey Kalinichenko Jun 15 '16 at 11:07

1 Answers1

2

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.

HopefullyHelpful
  • 1,652
  • 3
  • 21
  • 37
  • 2
    I formatted your response in the form of a series of steps. You can go ahead and tweak it, if you like. I just thought that it would be easier to digest. – Mr. Polywhirl Jun 15 '16 at 11:08