How to calculate the azimuth of two vectors

Hello All,

I know my question is really easy for the three js team. I want a formula to calculate the exact azimuth of two vec.

Thanks

Maybe Vector3.angleTo might help you? Or by azimuth you mean angle only on a specific axis / plane :thinking: (in which case you could likely just project both vectors first, and then calculate the angle) ?

i want to calculate the angle with respect to z axis.

Let say i have a line perpendicular to z axis and its like x axis on the left side so in the clock wise direction it should give me 90 deg as o/p .If the line is between x axis and z axis in the 3rd quad so it should give me 135 deg.
So according to the direction vector i think we will determine in which quad the line is and than angle calculation with respect to z axis. Some sort of like this

Can you help me to create an formula

I not sure if it’s necessary to create any specific formula for that (although you can find it here, α = arccos[(a dot b) / (|a| * |b|)].) But to calculate angle between any vector an the z-axis you can simply do:

myVector.angleTo(new Three.Vector3(0.0, 1.0, 0.0));

for z axis i think it should be like this

myVector.angleTo(new Three.Vector3(0.0, 0, 1.0));

right?

Ah, I’m blind - yes it should be as you wrote :sweat_smile:

Ok last point i think , may be if you answer so it convert into an solution. How i can check that the with respect to z axis the angle is calculated in clockwise direction

means i have
z_vec = new THREE.Vector3(0,0,1)

point_vec = any vector

so i want point_vec.angleTo(z_vec) should always be in clockwise direction

Hi!

Relatively to what?
Any explanatory picture?

let say i have a line on a plane. The plane is on xz axis. So i am calculating the direction vec with the line end points. Now i want to calculate the angle between this direction vector and z axis as we considered
z_vec but this angle should always we measure in clock wise direction some thing like this

If I got it correctly, in your coordinate system Z-axis is up? And what about other axes? Just for better understanding.

For this question, I caculate the azimuth by dot method, and get the direction by cross, just like below.

      const cosTheta = v1.clone.dot(v2) / (v1.length() * v2.length());
      const theta = Math.acos(Math.min(1, Math.max(-1, cosTheta)));
      let thetaAngle = (180 * theta) / Math.PI;

      if (thetaAngle != 180) {
        const cross = v1.clone().cross(v2).normalize();
        thetaAngle *= cross.z; // because I use the z to judge the direction, please refer your REQ to decide how to use it
      }

so you can calculate the angle you want by direction.

No all axis are same as in the normal scene. That is just an explanation picture.

So, basically, you want this?

The OA vector is on XZ plane.

1 Like

Yes, i want this

Yeah, I’m that great mathematician :slight_smile: There must be 315 instead of 225 :smiley:

1 Like

Yes, its true let me show you an diagram i want this

Specifically for finding a clockwise angle of a vector on XZ plane relatively to Z-axis, try this:

import * as THREE from "https://threejs.org/build/three.module.js";

let v = new THREE.Vector3(2, 0, 2);

let angle = Math.atan2(v.z, v.x);
angle -= Math.PI * 0.5;
angle += angle < 0 ? Math.PI * 2 : 0; 

console.log(THREE.MathUtils.radToDeg(angle));

if the relative axis is x than do i need to do vise verse. Or something else

Something else:
// angle -= Math.PI * 0.5;

1 Like

What you ask for and what you want here doesn’t match.

function azimuth( x , z ) { if( (x = Math.atan2(z,x)) < 0 ) x += 2*Math.PI; return (x*180/Math.PI).toFixed(2) }

azimuth(-1, -0.577350262 ); // 210 for z = 1; x = -0.577350262