# Find if a point lies within the angle formed by three other points

I have three points (A, B, C) in a 3d space that define an angle like this:

I need a fast way to know if another point D (that I already know is coplanar with the plane ABC) lies within the angle formed by <ABC.

You can express vector AD as a linear combination of AB and AC. If the coefficients of the linear combination are both positive, then D is inside the angle.

So basically, you are solving the system of equations (3 equations, 2 unknowns):

$\left[\begin{array}{rr|r} b_x-a_x & c_x-a_x& d_x-a_x \\ b_y-a_y & c_y-a_y& d_y-a_y \\ b_z-a_z & c_z-a_z& d_z-a_z \\ \end{array}\right]$

which represent the following equations:

$(b_x-a_x) \times I + (c_x-a_x) \times J = d_x-a_x \\ (b_y-a_y) \times I + (c_y-a_y) \times J = d_y-a_y \\ (b_z-a_z) \times I + (c_z-a_z) \times J = d_z-a_z \\$

and if your solution vector has positive components ($I>0, J>0$), then D is inside that angle. If there is no solution, then D is not in the ABC plane.

• Shouldn't that be $d_y-a_y$ and $d_z-a_z$ on the right side? – almosnow Oct 8 '15 at 18:57
• Yeah, my bad. I've fixed the typo, thanks! – jh314 Oct 8 '15 at 19:00
• I really don't know how to solve that... shame... I know D is in the plane, so there exists a solution, but how would I arrive to that, IDK... I'm not used to that notation. – almosnow Oct 8 '15 at 19:01
• I've added the 3 basic equations, now you just have to solve for I and J. – jh314 Oct 8 '15 at 19:23
• Oh ok, now I see, thanks! – almosnow Oct 8 '15 at 19:25

Precompute $v = C - A$, and let $w = (-v_y, v_x)$. Then $D$ is on the right side of $AC$ if and only iff $(D - A) \cdot w > 0$. A similar construction tells you whether $D$ is on the upper side of $AB$. So in that case, you need to check $(D-A) \cdot w' < 0$. If you pass both those tests, then $D$ is in the wedge.

(This assumes that $B$ is on the 'right side" of $AC$, i.e., $(B- A) \cdot w > 0$. If that's not the case, you need to swap $B$ and $C$.).

• How would you do that in 3D? I'm curious because of $w = (-v_y, v_x)$ – almosnow Oct 8 '15 at 18:47
• Ah...we're in 3-space. Then let $n = (C - A) \times (B - A)$, and let $w = n \times (C - A)$ and $w' = n \times (B - A)$. Then, if you're applying this to many points $D$, you have only two dot products to compute, rather than trying to solve a degenerate $3 \times 3$ system of equations as you need to do in @jh314's solution. – John Hughes Oct 9 '15 at 0:06
• Thank you @John Hughes. I will try out this method as it would be faster and easier to implement. – almosnow Oct 11 '15 at 3:32

Project the points onto one of the coordinate planes and test $$AC \times AD > 0 \text{ and } AD \times AB > 0$$ (use the projected points here; $\times$ is the 2D cross product, a number)