Check if a point projected on a line segment is not outside it

前端 未结 4 790
半阙折子戏
半阙折子戏 2021-01-11 17:16

\"illustration\"

See the image above; basically, I want a simple test to check if a point is within the line se

4条回答
  •  梦谈多话
    2021-01-11 18:01

    I took Daniel Fischer answer which is great, and adjusted it for 3D and Unity:

    public bool InSegmentRange(Vector3 start, Vector3 end, Vector3 point) {
        Vector3 delta = end - start;
        float innerProduct = (point.x - start.x) * delta.x + (point.y - start.y) * delta.y + (point.z - start.z) * delta.z;
        return innerProduct >= 0 && innerProduct <= delta.x * delta.x + delta.y * delta.y + delta.z * delta.z;
    }
    

提交回复
热议问题