# how to calculate angle between two HeadingPitchRoll?

**URL:** <https://community.cesium.com/t/how-to-calculate-angle-between-two-headingpitchroll/7120>\
**Category:** CesiumJS\
**Created:** [July 4, 2018, 5:11pm UTC](https://community.cesium.com/t/how-to-calculate-angle-between-two-headingpitchroll/7120 "2018-07-04T17:11:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Surajit\_chowbey](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/surajit_chowbey/32/1987_2.png) [@Surajit\_chowbey](https://community.cesium.com/u/Surajit_chowbey)\
**Post date:** [July 4, 2018, 5:11pm UTC](https://community.cesium.com/t/how-to-calculate-angle-between-two-headingpitchroll/7120/1 "2018-07-04T17:11:18Z")

</div>

**hi,**

**here the code I’m using.**

function calcAngleBetweenHeadingPitchRolls(hpr1, hpr2){

if(!(hpr1 instanceof Cesium.HeadingPitchRoll) || !(hpr2 instanceof Cesium.HeadingPitchRoll)){

throw “Invalid input”;

}

var q1 = Cesium.Quaternion.fromHeadingPitchRoll(hpr1);

var q2 = Cesium.Quaternion.fromHeadingPitchRoll(hpr2);

if(q1.equals(q2)){

return 0;

}

Cesium.Quaternion.normalize(q1,q1);

Cesium.Quaternion.normalize(q2,q2);

var qDiff = Cesium.Quaternion.multiply(q1, q2, new Cesium.Quaternion());

var diffAngleRadians = Cesium.Quaternion.computeAngle(qDiff);

console.log("diffAngleRad: " + diffAngleRadians);

console.log("diffAngleDeg: " + Cesium.Math.toDegrees(diffAngleRadians));

return diffAngleRadians;

}

---

<div class="post-metadata">

**Author:** ![Gabby\_Getz](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/gabby_getz/32/7838_2.png) [@Gabby\_Getz](https://community.cesium.com/u/Gabby_Getz)\
**Post date:** [July 5, 2018, 8:20pm UTC](https://community.cesium.com/t/how-to-calculate-angle-between-two-headingpitchroll/7120/2 "2018-07-05T20:20:57Z")

</div>

You’ll probably have more success for the mathematics solution on a more math-oriented forum, however, you should be able to implement the algorithm in Cesium using the **[Quaternion](https://cesiumjs.org/Cesium/Build/Documentation/Quaternion.html)** class.

For example,

var q1 = Cesium.Quaternion.fromHeadingPitchRoll(hpr1);

var q2 = Cesium.Quaternion.fromHeadingPitchRoll(hpr2);

var dq = Cesium.Quaternion.subtract(q1, q2, new Cesium.Quaternion());

var angle = Cesium.Quaternion.computeAngle(dq);

``

Thanks,  
Gabby

---

<div class="post-metadata">

**Author:** ![Surajit\_chowbey](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/surajit_chowbey/32/1987_2.png) [@Surajit\_chowbey](https://community.cesium.com/u/Surajit_chowbey)\
**Post date:** [July 6, 2018, 3:31am UTC](https://community.cesium.com/t/how-to-calculate-angle-between-two-headingpitchroll/7120/3 "2018-07-06T03:31:07Z")

</div>

Thanks for your time, but it does not work.  
Here the full code I’m using.

function createHPRFromDegrees(h,p,r){

//arguments are in degree

this.h = warpTo360(h);

this.p = warpTo360§;

this.r = warpTo360®;

return Cesium.HeadingPitchRoll.fromDegrees(this.h,this.p,this.r, new Cesium.HeadingPitchRoll());

}

function warpTo360(degree){

if(!degree || isNaN(degree)){

return 0;

}

var result = degree;

result = result % 360;

if(result === 0){

//just for handling ‘-0’

return 0;

}

else if(result \< 0) {

return (result + 360);

} else {

return result;

}

}

function calcAngleBetweenHeadingPitchRolls(hpr1, hpr2){

if(!(hpr1 instanceof Cesium.HeadingPitchRoll) || !(hpr2 instanceof Cesium.HeadingPitchRoll)){

throw “Invalid input”;

}

var q1 = Cesium.Quaternion.fromHeadingPitchRoll(hpr1,new Cesium.Quaternion());

var q2 = Cesium.Quaternion.fromHeadingPitchRoll(hpr2,new Cesium.Quaternion());

console.log(q1);

console.log(q2);

if(q1.equals(q2)){

return 0;

}

var qDiff = Cesium.Quaternion.subtract(q2, q1, new Cesium.Quaternion());

console.log(qDiff);

var diffAngleRadians = Cesium.Quaternion.computeAngle(qDiff);

console.log("diffAngleRad: " + diffAngleRadians);

console.log("diffAngleDeg: " + Cesium.Math.toDegrees(diffAngleRadians));

return diffAngleRadians;

}

``

If use it like the following it sholud give me 10 degree but returning 180 degree

var camHPR = createHPRFromDegrees(15,0,0);

var imgHPR = createHPRFromDegrees(5,0,0);

calcAngleBetweenHeadingPitchRolls(camHPR, imgHPR);

diffAngleRad: 3.1263857866503817  
diffAngleDeg: 179.1287107047547

``

---

<div class="post-metadata">

**Author:** ![Gabby\_Getz](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/gabby_getz/32/7838_2.png) [@Gabby\_Getz](https://community.cesium.com/u/Gabby_Getz)\
**Post date:** [July 6, 2018, 7:33pm UTC](https://community.cesium.com/t/how-to-calculate-angle-between-two-headingpitchroll/7120/4 "2018-07-06T19:33:55Z")

</div>

Solution is described here:

> Assume your quaternions xx and yy are represented as x=[x0,x1,x2,x3]x=[x0,x1,x2,x3] and y=[y0,y1,y2,y3]y=[y0,y1,y2,y3] and that they are unit quaternions. Then let inv()inv⁡() denote the inverse of a quaternion which for unit quaternions is equivalent to the conjugate (i.e. inv(x)=conj(x)=[x0,−x1,−x2,−x3]. Then the quantity that captures the true difference is z=x∗conj(y). We can then recover the angle using θ=2arccos(z0). Then θ gives you an angle by which the two quaternions differ.

The normalize, inverse, and multiply functions are available in the Quaternion class to perform the described operations.

---

<div class="post-metadata">

**Author:** ![Gabby\_Getz](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/gabby_getz/32/7838_2.png) [@Gabby\_Getz](https://community.cesium.com/u/Gabby_Getz)\
**Post date:** [July 6, 2018, 7:35pm UTC](https://community.cesium.com/t/how-to-calculate-angle-between-two-headingpitchroll/7120/5 "2018-07-06T19:35:35Z")

</div>

Sorry, my formatting got messed up.

Solution is described here: [Compute Angle Between Quaternions (in Matlab) - Mathematics Stack Exchange](https://math.stackexchange.com/questions/167827/compute-angle-between-quaternions-in-matlab)

> To get an exact answer you would have to use the following process. Assume your quaternions x and y are represented as x=[x0,x1,x2,x3] and y=[y0,y1,y2,y3] and that they are unit quaternions. Then let inv() denote the inverse of a quaternion which for unit quaternions is equivalent to the conjugate (i.e. inv(x)=conj(x)=[x0,−x1,−x2,−x3]). Then the quantity that captures the true difference is z=x∗conj(y). We can then recover the angle using θ=2arccos(z0). Then θ gives you an angle by which the two quaternions differ.

The normalize, inverse, and multiply functions are all available in the Cesium Quaternion API.

Thanks,

Gabby

---

<div class="post-metadata">

**Author:** ![Surajit\_chowbey](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/surajit_chowbey/32/1987_2.png) [@Surajit\_chowbey](https://community.cesium.com/u/Surajit_chowbey)\
**Post date:** [July 8, 2018, 2:44pm UTC](https://community.cesium.com/t/how-to-calculate-angle-between-two-headingpitchroll/7120/6 "2018-07-08T14:44:28Z")

</div>

Thanks Gabby for your time, this is what I was looking for.
