# How to compute the rotated polygon's coordinates?

**URL:** <https://community.cesium.com/t/how-to-compute-the-rotated-polygons-coordinates/2993>\
**Category:** CesiumJS\
**Created:** [August 30, 2015, 7:27pm UTC](https://community.cesium.com/t/how-to-compute-the-rotated-polygons-coordinates/2993 "2015-08-30T19:27:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wei](https://avatars.discourse-cdn.com/v4/letter/w/90ced4/32.png) [@Wei](https://community.cesium.com/u/Wei)\
**Post date:** [August 30, 2015, 7:27pm UTC](https://community.cesium.com/t/how-to-compute-the-rotated-polygons-coordinates/2993/1 "2015-08-30T19:27:37Z")

</div>

Hi All,

I want to rotate a polygon based on the center point using the value of heading, pitch and roll and get the coordinates of each vertex of the rotated polygon, then use these coordinates to do some other calculations for my application.

I was trying to compute these coordinates by using a transformation matrix to multiply by the the vertexes of the polygon, respectively, before rotated. To create this transformation matrix, I used Cesium.Transforms.headingPitchRollToFixedFrame. However, the result is not what I expected, even without any rotation, the rotated polygon(red) is away from the original one(green) and seems to be rotated by 90 degree.

I tried to used other methods but still have the same problem.

Does anyone can tell me what I am supposed to do?

Many thanks,

Wei

[![](https://lh3.googleusercontent.com/-1zvqjFfVC4c/VeNZH3kRSBI/AAAAAAAABko/zLQp_jMn7VU/s320/222.png)](https://lh3.googleusercontent.com/-1zvqjFfVC4c/VeNZH3kRSBI/AAAAAAAABko/zLQp_jMn7VU/s1600/222.png)

var viewer = new Cesium.Viewer(‘cesiumContainer’);

var entities = viewer.entities;

var center = Cesium.Cartesian3.fromDegrees(5, 2.5);// The center of the polygon

var heading = Cesium.Math.toRadians(30);

var pitch = Cesium.Math.toRadians(0);

var roll = Cesium.Math.toRadians(0);

```
// create the transformation matrix based on a specific value of heading, pitch and roll

```

var rotation = Cesium.Transforms.headingPitchRollToFixedFrame(center, heading, pitch, roll);

```
// add the polygon without any rotation
entities.add({

```

polygon: {

hierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArray([0, 0,

10, 0,

10, 5,

0, 5])),

material: Cesium.Color.GREEN

}

});

```
// compute the coordinates of each vertex of the rotated polygon

```

var p1 = new Cesium.Cartesian3.fromDegrees(0, 0);

Cesium.Matrix4.multiplyByPoint(rotation,p1,p1);

var a1 = Cesium.Ellipsoid.WGS84.cartesianToCartographic(p1);

var p2 = new Cesium.Cartesian3.fromDegrees(10, 0);

Cesium.Matrix4.multiplyByPoint(rotation,p2,p2);

var a2 = Cesium.Ellipsoid.WGS84.cartesianToCartographic(p2);

var p3 = new Cesium.Cartesian3.fromDegrees(10, 5);

Cesium.Matrix4.multiplyByPoint(rotation,p3,p3);

var a3 = Cesium.Ellipsoid.WGS84.cartesianToCartographic(p3);

var p4 = new Cesium.Cartesian3.fromDegrees(0, 5);

Cesium.Matrix4.multiplyByPoint(rotation,p4,p4);

var a4 = Cesium.Ellipsoid.WGS84.cartesianToCartographic(p4);

// add the rotated polygon

entities.add({

polygon: {

hierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArray([

Cesium.Math.toDegrees(a1.longitude), Cesium.Math.toDegrees(a1.latitude),

Cesium.Math.toDegrees(a2.longitude), Cesium.Math.toDegrees(a2.latitude),

Cesium.Math.toDegrees(a3.longitude), Cesium.Math.toDegrees(a3.latitude),

Cesium.Math.toDegrees(a4.longitude), Cesium.Math.toDegrees(a4.latitude)])),

material: Cesium.Color.RED

}

});

viewer.zoomTo(viewer.entities);

``

``

---

<div class="post-metadata">

**Author:** ![hannah](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/hannah/32/4509_2.png) [@hannah](https://community.cesium.com/u/hannah)\
**Post date:** [August 30, 2015, 11:23pm UTC](https://community.cesium.com/t/how-to-compute-the-rotated-polygons-coordinates/2993/2 "2015-08-30T23:23:08Z")

</div>

You have reference frame problems. This transform takes coordinates in a local reference frame, applies heading/pitch/roll, then projects it into the earth fixed reference frame at the origin (center) position provided. You’ll need to define your rectangle points in a local reference frame before applying this kind of transform. Right now, the rectangle points are in the earth fixed reference frame, with the origin at the center of the globe. You need to project the rectangle points such the origin is at the center of the rectangle.

-Hannah

---

<div class="post-metadata">

**Author:** ![Wei](https://avatars.discourse-cdn.com/v4/letter/w/90ced4/32.png) [@Wei](https://community.cesium.com/u/Wei)\
**Post date:** [August 31, 2015, 6:47pm UTC](https://community.cesium.com/t/how-to-compute-the-rotated-polygons-coordinates/2993/3 "2015-08-31T18:47:45Z")

</div>

Thank you very much Hannah. It works now.

---

<div class="post-metadata">

**Author:** ![Venkat\_Gowtham\_Kumar](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@Venkat\_Gowtham\_Kumar](https://community.cesium.com/u/Venkat_Gowtham_Kumar)\
**Post date:** [April 26, 2016, 3:07pm UTC](https://community.cesium.com/t/how-to-compute-the-rotated-polygons-coordinates/2993/4 "2016-04-26T15:07:19Z")

</div>

Hi Wei/Hannah,

Could you please provide me sample piece of code to compute rotated polygon coordinates?

Thanks,  
Gowtham.

---

<div class="post-metadata">

**Author:** ![Benny\_Chen](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Benny\_Chen](https://community.cesium.com/u/Benny_Chen)\
**Post date:** [June 2, 2018, 12:27am UTC](https://community.cesium.com/t/how-to-compute-the-rotated-polygons-coordinates/2993/5 "2018-06-02T00:27:06Z")

</div>

Hi Hannah,

If my understanding is correct, after this step “var p1 = new Cesium.Cartesian3.fromDegrees(0, 0);”, the p1 will already be in the “earth fixed reference frame”, with the origin at the center of the globe. When you said to “define your rectangle points in a local reference frame”, how can this be done? in particular, “You need to project the rectangle points such the origin is at the center of the rectangle”, which function in Cesium we can use for this?

Much appreciate any hints or sample code snippet.

Cheers,

Benny

---

<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:** [June 6, 2018, 1:55pm UTC](https://community.cesium.com/t/how-to-compute-the-rotated-polygons-coordinates/2993/6 "2018-06-06T13:55:05Z")

</div>

Hi Benny,

Take a look at Scott’s answer in [this thread](https://groups.google.com/forum/#!searchin/cesium-dev/rotate%2420polygon%7Csort:date/cesium-dev/NQgKBoj3lM0/iM-C46UAAwAJ), you’ll need to do some transformations with matrices to rotate in the correct reference frame.

Thanks,

Gabby
