I have this problem: verify if point belongs to ray in 3D. After some math research, I've coded the solution, but it seems that it just doesn't work.
That's the illustration. P is the point. E - the end-point of ray. V - directional vector of the ray.
double x, y, z, e1, e2, e3, v1, v2, v3, d, xVectorFromEToP,
dirVectorMagnitude, vectorEPMagnitude, yVectorFromEToP, zVectorFromEToP,
cpX, cpY, cpZ;
cin >> x >> y >> z >> e1 >> e2 >> e3 >> v1 >> v2 >> v3;
// HERE I'M FORMING THE EP vector - from point P to end-point E
xVectorFromEToP = x - e1;
yVectorFromEToP = y - e2;
zVectorFromEToP = z - e3;
//HERE I'M CALCULATING CROSS-PRODUCT of THE VECTORS: EP and V
cpX = ((v2 * zVectorFromEToP) - (v3 * yVectorFromEToP));
cpY = ((v1 * zVectorFromEToP) - (v3 * xVectorFromEToP)) * -1;
cpZ = ((v1 * yVectorFromEToP) - (v2 * xVectorFromEToP));
// HERE I'M CALCULATING MAGNITUDES OF THOSE VECTORS AND DEBUGGING IN COUT
vectorsEpVMagnitude = sqrt(pow(cpX, 2) + pow(cpY, 2) + pow(cpZ, 2));
dirVectorMagnitude = sqrt(pow(v1, 2) + pow(v2, 2) + pow(v3, 2));
cout << "EP: " << vectorsEpVMagnitude << endl;
cout << "dir: " << dirVectorMagnitude << endl;
// final formula for calculating distance
d = vectorsEpVMagnitude / dirVectorMagnitude;
// precision is 1e-8: 1 means belong, otherwise - 0;
if (d < 1e-8) {
cout << "distance: " << d << endl;
cout << 1;
} else {
cout << "distance: " << d << endl;
cout << 0;
}
I have sample inputs: 1) P(2.0 1.0 0.0), E(2.0 1.0 1.0), V(0.0 0.0 1.0) should be 0;
2) P(2.0 1.0 0.0), E(2.0 1.0 1.0), V(0.0 0.0 -1.0) should be 1!
However both of them have distance equal to 0, while as stated they should have different distance. I would appreciate any help, clarification, etc.