# How could I check to see if a point is inside a cone projected by another point?

I use CesiumJS, a javascript library, to render entities in a 3-dimensional space. I need to detect if one entity is "viewing" another entity. My initial thought is to use a cone to represent the field of view of the entity. I need to be able to make the cone variable angle and height. What is the most efficient way to check if Entity 2 is inside the cone emitted by Entity 1? I am doing this calculation a bunch of times so I am trying to find a way to do it efficiently.

In Cesium, my entities are plotted using Lat/long/alt, which I figured I could think of as X,Y,Z. I also know the orientation of each point, so I know which way to make the cone emit. Cesium has some built-in functions for checking intersections but all seem to use rays or lines emitting from entity 1, which don't give me my desired output. Any help would be greatly appreciated.

Let's assume that your point where you check the viewing is $$\vec P_0(x_0,y_0,z_0)$$, you are looking in a direction $$\vec d(d_x,d_y,d_z)$$ in a cone with opening (angle between side and axis) $$\alpha$$, and you want to check if $$\vec P_1(x_1,y_1,z_1)$$ is inside the cone. Then you want the angle between the vector $$\vec P_1-\vec P_0$$ and $$\vec d$$ to be less than $$\alpha$$. You can write this using scalar product: $$\cos\left(\angle(\vec P_1-\vec P_0, \vec d)\right)=\frac{(\vec P_1-\vec P_0)\cdot \vec d}{|\vec P_1-\vec P_0||\vec d|}\\=\frac{(x_1-x_0)d_x+(y_1-y_0)d_y+(z_1-z_0)dz}{\sqrt{(x_1-x_0)^2+(y_1-y_0)^2+(z_1-z_0)^2}\sqrt{dx^2+dy^2+d_z^2}}$$ If the viewing angle is smaller than $$\alpha$$, then the last expression must be greater than $$\cos\alpha$$.
• The ^ operator is not square in javascript. It is exclusive or. Aug 8 at 21:42
• Ok, after fixing all the code to use Math.Pow() my result is ~-0.00769.... and if I understand correctly I would want to check to make sure that the result I got is less than the angle of my cone correct? If I assume my cone's angle is 5deg then I would want the number I get to be less than or equal to -2.5 to 2.5 deg's is that correct? Aug 9 at 12:56
• The number is right, but it's the cosine of the angle. So it means that it's close to $90^\circ$. $\cos 2.5^\circ=0.99904822158$. And you want your number to be greater than that. When you are looking straight at the other object, the cosine is $1$. Aug 9 at 13:48