How do I calculate the midpoint of two Cesium Cartesian3 Coordinates. Like I’m able to compute the distance. However, how can I compute the coordinates of the midpoint?

A little vector math is all we need here.

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

// Define a couple of points

var p1 = Cesium.Cartesian3.fromDegrees(-73.966250, 40.783430); // New York

var p2 = Cesium.Cartesian3.fromDegrees(-122.419420, 37.774930);// San Francisco

// Compute vector from p1 to p2

var p1p2 = new Cesium.Cartesian3(0.0, 0.0, 0.0);

Cesium.Cartesian3.subtract(p2, p1, p1p2);

// Compute vector to midpoint

var halfp1p2 = new Cesium.Cartesian3(0.0, 0.0, 0.0);

Cesium.Cartesian3.multiplyByScalar(p1p2, 0.5, halfp1p2);

// Compute point half way between p1 and p2

var p3 = new Cesium.Cartesian3(0.0, 0.0, 0.0);

p3 = Cesium.Cartesian3.add(p1, halfp1p2, p3);

// Force point onto surface of ellipsoid for visualization.

var midPt = Cesium.Cartographic.fromCartesian(p3);

var p3a = Cesium.Cartesian3.fromRadians(midPt.longitude, midPt.latitude, 0.0);

viewer.entities.add({

```
position: p1,
point: {
color: Cesium.Color.DODGERBLUE,
pixelSize: 10,
outlineColor: Cesium.Color.WHITE,
outlineWidth: 3
}
});
```

viewer.entities.add({

```
position: p2,
point: {
color: Cesium.Color.DODGERBLUE,
pixelSize: 10,
outlineColor: Cesium.Color.WHITE,
outlineWidth: 3
}
});
```

viewer.entities.add({

```
position: p3a,
point: {
color: Cesium.Color.GOLD,
pixelSize: 10,
outlineColor: Cesium.Color.WHITE,
outlineWidth: 3
}
});
```

viewer.zoomTo(viewer.entities);

``

Hope that helps.

Scott

Thank you Scott! Your help is greatly appreciated! I have a doubt though. When I have cartesian3 coordinates in the form of x, y, z, isn’t the basic midpoint formula of a straight line applicable here. I’m confused as we are dealing with cartesian3 coordinates, right? So, the midpoint can’t be (x1+x2)/2, (y1+y2)/2? Just curious to know why this is not applicable here?

That would work too. I tend to think in vectors.

Why ask for help when you have a solution?

Your solution seems to be a better approach. I had a basic idea of a straight line. I don’t think that will work when I am trying to average two paths to create a reference path. I wasn’t sure of the solution. So, I thought of seeking help on the issue. Thank you Scott! I appreciate it much!