Verify if point is inside a cone in 3D space

前端 未结 2 1716
醉酒成梦
醉酒成梦 2020-12-09 13:16

Consider:

  • X(x1,y1,z1) the point I need to verify if it is inside a cone.
  • M(x2,y2,z2) the vertex of the cone. (the top point
2条回答
  •  轮回少年
    2020-12-09 13:46

    You need to check whether the angle between your difference vector (X-M) and your center vector (N) is less than or equal to the angle of your cone (which you haven't specified in the question). This will tell you if the position vector (X) is inside the infinite cone, and you can then also check for distance (if you want). So,

    float theta = PI/6; //half angle of cone
    if (acos(dot(X-M, N)/(norm(X-M)*norm(N)) <= theta) doSomething();
    

    For performance, you could also normalize N (convert it to a vector with length 1) and store the length separately. You could then compare norm(X-M) to the length, giving you a round-bottom cone (for which I'm sure a name exists, but I don't know it).

    Edit: Forgot the inverse cosine, the dot product is equal to norm(U)*norm(V)*cos(Angle) so we have to invert that operation to compare the angles. In this case, the acos should be fine because we want positive and negative angles to compare equally, but watch out for that.

    Edit: Radians.

提交回复
热议问题