Calculate point from a start point heading and distance

960 views
Skip to first unread message

Joshua Pappalardo

unread,
Sep 18, 2015, 4:44:53 PM9/18/15
to cesium-dev
I am having a hard time finding a means via your api to calculate an end point using a starting point a bearing and distance to calculate. Can you point me in the right direction, this is the only thing from a strict api sense preventing me from completely ridding myself of google earth extensions.

Mark Erikson

unread,
Sep 18, 2015, 5:15:15 PM9/18/15
to cesium-dev
There's nothing Cesium-specific about that - that's just geometric algorithm calculation.  Plenty of implementations out there - see the "Destination point given distance and bearing" item at http://www.movable-type.co.uk/scripts/latlong.html for one.

Joshua Pappalardo

unread,
Sep 18, 2015, 5:21:55 PM9/18/15
to cesium-dev
What I was really hoping for was a way to define a Rectangle from a Center point, a width and height. I can ask for a Rectangle to give me its center point, but cannot define one by it. At any rate I understand haversine is not cesium specific, how exactly do I make sure I am using the appropriate radius for a given point in cesium?

Mark Erikson

unread,
Sep 18, 2015, 7:23:36 PM9/18/15
to cesium-dev
Ah... if you want a Rectangle, what do you mean by "appropriate radius"?

Now, as for a rectangle based on center, width, and height... funny you should ask that.  As it turns out, I wrote some Java code a few years back to do exactly that, with the rectangle oriented at an arbitrary angle, and going around the corners in either direction.  I think I can share the appropriate snippet.  All you do is calculate the center of one side, then you can work out the four corners from there:

// utility function to ensure angles are in 0...360
   
public static double modAngle360(double angle) {
       
double temp = (angle % 360.0) + 360.0;
       
double result = temp % 360.0;
       
return result;
   
}


   
public static List<LatLng> generateRectangle(LatLng center, double length, double width, double inboundAngle, boolean leftTurn) {
       
double l2 = length / 2;
       
double w2 = width / 2;

       
double angle0 = modAngle360(inboundAngle + 180);
       
double angle1 = modAngle360(inboundAngle - 90.0);
       
double angle2 = modAngle360(angle1 + 180.0);

       
if (!leftTurn) {
           
// Swap our angles so that we go around in the opposite direction
           
double temp = angle1;
            angle1
= angle2;
            angle2
= temp;
       
}

       
LatLng rightMiddle = calcLatLonDeg(center, angle0, l2);

       
LatLng p1 = calcLatLonDeg(rightMiddle, angle2, w2);
       
LatLng p2 = calcLatLonDeg(p1, inboundAngle, length);
       
LatLng p3 = calcLatLonDeg(p2, angle1, width);
       
LatLng p4 = calcLatLonDeg(p1, angle1, width);

       
// do useful stuff with the four corners


So, draw a half-length line from the center to the right, decide which way you're turning from there, and walk around the rectangle. 

There's probably a better way to do it somewhere, but this has worked fine for my purposes.

Joshua Pappalardo

unread,
Sep 21, 2015, 3:20:34 PM9/21/15
to cesium-dev
This seemed simpler:

public Rectangle calculateRectangle(Cartographic cartographic, double width, double height) {
Cartographic north = Geodesic.destination(cartographic, 0, height / 2);
Cartographic south = Geodesic.destination(cartographic, Cesium.Math.PI, height / 2);
Cartographic east = Geodesic.destination(cartographic, Cesium.Math.PI_OVER_TWO, width / 2);
Cartographic west = Geodesic.destination(cartographic, Cesium.Math.THREE_PI_OVER_TWO, width / 2);
return Rectangle.fromDegrees(west.getLongitudeDegrees(), south.getLatitudeDegrees(), east.getLongitudeDegrees(), north.getLatitudeDegrees());

Mark Erikson

unread,
Sep 21, 2015, 3:32:50 PM9/21/15
to cesium-dev
Like I said... it's code that I wrote several years ago, as part of a more complicated function in a larger application, without having Cesium's API available.  But yeah, that looks like it'd work.
Reply all
Reply to author
Forward
0 new messages