I want to be able to determine whether a point is inside a cone.

I have three vectors (eyePosition), (eyeDirection) and (Test Point) which are all relative to the origin.

I want a cone of length l and the angle between the diagonal sides to be alpha (both of which can be modified). I know you can use the dot product to find this answer out but my implementation seems to be giving me quite weird results.

Math Theory

My idea is that two vectors A and B where A is the vector from eyePosition to Test Point and B is the axis of the cone pointing towards eyeDir*l.

That if acos(A.B / |A||B|) < 0.5 * alpha then the point can be in the cone.

And for a stricter definition if the dot product of A.B is <= length of the axis (l) then the point is definitely inside the cone.

Is that correct?

My C Code is as such

int insideLightCone(double x, double y, double z, double px, double py, double pz,

double ang, double height) {

double dotP = dotProduct(x * height, y*height, z*height, px, py, pz);

cDistance = (eyex - px) * (eyex - px) + (eyey - py) * (eyey - py) + (eyez - pz) * (eyez - pz);

double magOfVecs = magnitude(x*height, y*height, z*height) * magnitude(px, py, pz);

double theta = acos(dotP / magOfVecs);

return ((theta <= 0.5 * ang) && (cDistance <= height));

}

I'm not sure any of this code or math is correct so I am asking for help.

Thanks

neutrinohunter

double dotP = dotProduct(x , y, z, px, py, pz);

double hDistance = magnitude(x, y, z);

double pDistance = magnitude(px, py, pz);

cDistance = dotP / hDistance;

double theta = acos(dotP / (Distance * pDistance));

return ((theta <= 0.5 * ang) && (cDistance <= height)) && cDistance > 0;