# How can I determine if two polygons overlap?

**URL:** <https://community.cesium.com/t/how-can-i-determine-if-two-polygons-overlap/23714>\
**Category:** CesiumJS\
**Created:** [April 28, 2023, 2:49pm UTC](https://community.cesium.com/t/how-can-i-determine-if-two-polygons-overlap/23714 "2023-04-28T14:49:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eric-g-97477](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/eric-g-97477/32/13197_2.png) [@eric-g-97477](https://community.cesium.com/u/eric-g-97477)\
**Post date:** [April 28, 2023, 2:49pm UTC](https://community.cesium.com/t/how-can-i-determine-if-two-polygons-overlap/23714/1 "2023-04-28T14:49:53Z")

</div>

I have a [sandcastle](https://sandcastle.cesium.com/#c=bVVtb9s2EP4rB3+xDGhU3HVA4TjpOq8dVrRIAAfdhyhoGelsEaVIgaRsqIX/+47Uq1M7gS3e3cN77u6hmGllHWhbwg0oPMIGrahLdleh2jqD6D7z6t+S79E090YfRI4m+pkqgNpIWMG8cK6yqyThzAmJTBPOBlzJK6bNPpmn6rS4TlWqspDqIPCI5jzbl2CL0lkW1hutHBcKTTqLISR75hY/8QbNqod0nIKR7Ywue3bvbKOyiApaxGfIe5F99/gdlxaDa4860/m5LTOYC+f5VNq4FahayjhUAL6EXa0yJ7Tycdzh51o6ca9ls9cqUrzERUu3rTTT2uRCUZylch8fvQfgEX5bvrqK4fUf8BSPpuVl0+9XZ6ZXl00e6C1PT9cA/mEkQV3uCBKHny0snbmmwnS2oqcPVEdtaBH3vsrQEI0TaH1EB/EfX2C7Og3B1MMSnWl86PhpvQOU5jq2wkdOlvGw/5RVRzmddd6T/z1dd1np2yCxVpPqyHcaRdbT6mu+UPFGS4lhmF3t6WzXegLFR2roKci234v1blbVtoguSQC+1fZrRYtvsICp6K2uTYbnov8H9Uer1d/c8W1wR3M6Ofo7zlsk90KGQXFS87yL61TGj1z0WzPvjnqq8dj7nZByNU1LdWsTLWO4av/Zsj0o3SGF7oCyfOBlGc/zqM0TzkFo9JTP9dnZoKm6viMd1Y4kKie8sNiBy5p+dtq85xn1MgqeZgE3tz31Tr6kOWpb62ajNJl3vGV7dF/8VoHCiKq0FZ6KnUBbQqwQaLjJiqblcLeLFmwInwgMxk0mNHvblGif9ChUro+bQdqUu2v5NkOFD4YrSzuVlh339s3rB/3fC4CNutZbHx8PBPraAJJkfIJ3eQ7+/QUFGgSnIUeHpqT3JhwLdGQFbUBpB/Q8OXIgLL3wMqH2wdHS9qGjnZTVsEkqwZB594uNDsKKZxlye09t0bydoDZcAbe2pglOEtE2Qu2EEp6JImvuCuAqpzLEvnDsZa2nxXQog1KDBreEy7h1dPGQRB+0ls/c/FU7R8rzkfMHkiJ0WpwHmU/EmapWuO3fLJ6trWsk3vYM/hSlvwT8TRcxljgsK+nHlDzXdJU4llnbj2adTKHrXBxA5DcX7jPIJLWEPDu6WLbiB72TbtcJxf8C9eeLZnF3QCN548OK5e2n1sgYWye0vIx0bR9e7Pw/)

What I am trying to do is determine whether two polygons overlap. However, I only care if they overlap if all of the points from the polygons could be visible.

To solve this problem, my plan is to:

1. project the points from the polygons to the window
2. using the projected polygons, perform an easy 2D polygon intersection
3. win

Projecting points from the polygons is easy using `Cesium.SceneTransforms.wgs84ToWindowCoordinates`.

The problem I am having is I am not sure how to determine whether or not a point in a polygon is facing away from the window. How can I do that?

---

<div class="post-metadata">

**Author:** ![eric-g-97477](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/eric-g-97477/32/13197_2.png) [@eric-g-97477](https://community.cesium.com/u/eric-g-97477)\
**Post date:** [April 28, 2023, 9:23pm UTC](https://community.cesium.com/t/how-can-i-determine-if-two-polygons-overlap/23714/3 "2023-04-28T21:23:18Z")

</div>

One idea I had was the following:

```auto
function testPolygon() {
  source.entities.values.forEach( (entity) => {
    const name = entity.properties.name?.getValue();
    const positions = entity.polygon.hierarchy.valueOf().positions;
    const camera = viewer.camera;
    const cameraDirection = camera.direction;
    const cameraFrustum = camera.frustum;
    const cameraNearDistance = cameraFrustum.near;
    const nearPlane = new Cesium.Plane( cameraDirection, cameraNearDistance )
    const origin = new Cesium.Cartesian3()
    
    positions.forEach( (position) => {
      const windowCoordinate = Cesium.SceneTransforms.wgs84ToWindowCoordinates(viewer.scene, position);
      
      console.log( "position", position );
      const positionRay = new Cesium.Ray( origin, position )
      
      const intersection = Cesium.IntersectionTests.rayPlane( positionRay, nearPlane )
    })
  });
}

```

The idea here is to construct the near plane from the camera, create a ray from the globe origin ( (0,0,0)? ), and then do an intersection test.

However, it is not working. But, I am not sure why. I am missing something fundamental, but I am not sure what that is yet.

---

<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:** [May 5, 2023, 5:33pm UTC](https://community.cesium.com/t/how-can-i-determine-if-two-polygons-overlap/23714/4 "2023-05-05T17:33:02Z")

</div>

Hi there,

There are some great geospatial helper libraries available which can help with these kinds of calculations, for example [turf.js](https://turfjs.org/docs/#booleanOverlap).

---

<div class="post-metadata">

**Author:** ![eric-g-97477](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/eric-g-97477/32/13197_2.png) [@eric-g-97477](https://community.cesium.com/u/eric-g-97477)\
**Post date:** [May 5, 2023, 6:34pm UTC](https://community.cesium.com/t/how-can-i-determine-if-two-polygons-overlap/23714/5 "2023-05-05T18:34:32Z")

</div>

Thank you. I found turf as well. Turd did help resolve this issue.

I still needed to determine whether or not a polygon was visible or not. The reason was to limit the number of polygon intersection calculations I needed to do, which I believe is a more expensive operation. For that I used the core of the solution mentioned above. I thought it was not working, but it apparently does.

I had a fixed set of polygons that weren’t going to change. For that set, created a bounding sphere for each one. I could then use:

```auto
const cullingVolume = cameraFrustum.computeCullingVolume(camera.position, camera.direction, camera.up)
cullingVolume.computeVisibility(sphere)

```

for an initial pass.

For that pass the cullingVolume check, I am doing one more check, but will probably get rid of it soon as it appears unnecessary.

```auto
const windowCoordinate = Cesium.SceneTransforms.wgs84ToWindowCoordinates(viewer.scene, sphere.center)
const positionRay = new Cesium.Ray(origin, sphere.center)
const intersection = Cesium.IntersectionTests.rayPlane(positionRay, nearPlane)

```

Then, I start using turf.intersect to check the polygons that get through both passes.

I am pretty sure I am not taking into account the curvature of the globe, but a few small errors in my calculation are acceptable.
