According to the solution(s) provided in this post Angle between 3 points in 3d space, there is a JavaScipt function that calculates the angles between three vectors in 3D space. The function calculates the angles as if each vector is a vertex on a surface, taking the second argument to calculate the angle between the other two vectors (1 & 3). The function is run three times with different orders of the input vectors to obtain three angles, which works correctly. The input vectors in the program are positions on the surface of a sphere that can be positioned anywhere with latitude and longitude sliders. These vectors are connected by lines (arcs) to create a spherical triangle. However, the function assumes the vectors are coplanar, which is not the case on a sphere. The function always adds up the three angles to 180 degrees, but on a sphere, it should be at least 180 and up to just under 540 degrees. I need the excess angle. It seems that the function needs to consider the z-components of the input vectors as the normal to the sphere's surface, and the x and y components as the tangent plane. Here is the function:
function ang3D2(v1, v2, v3) {
// Copy and normalize the input vectors
let V1 = v1.copy().normalize();
let V2 = v2.copy().normalize();
let V3 = v3.copy().normalize();
let Va = p5.Vector.sub(V1,V2).normalize();
let Vb = p5.Vector.sub(V3,V2).normalize();
let dots = p5.Vector.dot(Va,Vb);
let crosses = p5.Vector.cross(Va,Vb);
let crossesMag = crosses.mag();// / V2.z; ?
let angle = (Math.acos(dots) * 180.0) / Math.PI;
//let angle2 = (Math.atan2(crossesMag,dots) * 180.0) / Math.PI;
return Math.round(angle * 1000) / 1000; // here I'm just changing //between returning acos(angle) & atan(angle2) to test. angle2 seems //close its just that the "1st" angle is wrong. acos(angle) is what //works for coplanar
}//ang3d2()
//based off of //https://gist.github.com/andfaulkner/c4ad12a72d29bcd653eb4b8cca2ae476
//& its usage:
function angs() {
if (cyl1V && cyl2V && cyl3V) {
//let angs;
ang1 = ang3D2(cyl2V, cyl1V, cyl3V);
ang2 = ang3D2(cyl1V, cyl2V, cyl3V);
ang3 = ang3D2(cyl1V, cyl3V, cyl2V);
angS = ang1 + ang2 + ang3;
console.log(angS);
} //fi
} //angs
Although the function works almost as needed, the input vectors are not coplanar in this case. I have been trying to adjust it by using the atan(cross product) instead of just acos(dot) etc. I am not a professional programmer, and I have not found much information on this specific issue. Can someone please guide me in the right direction? Thank you in advance.