"Moving" # meters (over ground) from LatLonA to LatLonB (WGS84) - c#

I need a function in C# to do the following: move 50 meters from gps-point A in the direction of gps-point B and calculate the GPS-coordinates for that point.
For instance I've got two coordinates:
LatLon LatLonA = new LatLon(51.83966, 5.04631); // Latitude 51.83966, Longitude 5.04631
LatLon LatLonB = new LatLon(51.84172, 5.01961); // Latitude 51.84172, Longitude 5.01961
What I would like is a function like this:
function LatLon MoveTowards(LatLon A, LatLon B, double MetersOverGround)
{
//code here
}
That function would return the coordinate which is x meters away from A in the direction of B.

The Earth is not a sphere, nor even an ellipse. The best you can hope for without purchasing a commercial library would be an approximation (which for most people is good enough).
You could start by looking into the Haversine formula, and this page will be of great help.
Or if you want a commercial library, I have used ProLat with great success

Here is what you want. Just use Math.Atan2 to obtain the bearing of your A-to-B vector and obtain the bearing parameter.
/// <summary>
/// Calculates the end-point from a given source at a given range (meters) and bearing (degrees).
/// This methods uses simple geometry equations to calculate the end-point.
/// </summary>
/// <param name="source">Point of origin</param>
/// <param name="range">Range in meters</param>
/// <param name="bearing">Bearing in degrees</param>
/// <returns>End-point from the source given the desired range and bearing.</returns>
public static PointLatLng CalculateDerivedPosition(PointLatLng source, double range, double bearing)
{
const double DEGREES_TO_RADIANS = Math.PI/180;
const double EARTH_RADIUS_M = 6371000;
double latA = source.Lat * DEGREES_TO_RADIANS;
double lonA = source.Lng * DEGREES_TO_RADIANS;
double angularDistance = range / EARTH_RADIUS_M;
double trueCourse = bearing * DEGREES_TO_RADIANS;
double lat = Math.Asin(
Math.Sin(latA) * Math.Cos(angularDistance) +
Math.Cos(latA) * Math.Sin(angularDistance) * Math.Cos(trueCourse));
double dlon = Math.Atan2(
Math.Sin(trueCourse) * Math.Sin(angularDistance) * Math.Cos(latA),
Math.Cos(angularDistance) - Math.Sin(latA) * Math.Sin(lat));
double lon = ((lonA + dlon + Math.PI) % (Math.PI * 2)) - Math.PI;
return new PointLatLng(
lat / DEGREES_TO_RADIANS,
lon / DEGREES_TO_RADIANS);
}

Related

Fast Approximation if a point on earth is in range

For a game I need to calculate if items on the map are in range of the player.
The map is the earth.
I'm using the Haversine formula to calculate the distance between the player and each item.
However I did some profiling and found out that all those sin/cos calculations are too slow to allow for smoth gameplay.
Is there some other method to check of two points on earth could be withing a range of x metres?
The method does not need to be exact, but it must be fast and return true if distance <= x. It may also return true if distance > x (but should not always return true).
My test code (LinqPad)
void Main()
{
var lat = 53.553072;
var lng = 9.993023;
var lat0 = 53.553073;
var lng0 = 9.993178;
"Google Maps: 10.02m".Dump(); // 10.02m
$"Euclid: {DistanceEuclid(lat, lng, lat0, lng0)}m".Dump(); // 10,2396639400397m
$"Haversine: {DistanceHaversine(lat, lng, lat0, lng0)}m".Dump(); // 10,2396637520237m
}
const int R = 6371000;
const double PiBy180 = Math.PI / 180;
const double deglen = 111194.93;
double DistanceEuclid(double lat, double lng, double lat0, double lng0)
{
var x = lat - lat0;
var y = (lng - lng0)*Math.Cos(ToRadians(lat0));
return deglen*Math.Sqrt(x*x + y*y);
}
public double DistanceHaversine(double lat, double lng, double lat0, double lng0)
{
var lat1 = ToRadians(lat);
var lat2 = ToRadians(lat0);
var dLat = ToRadians(lat0 - lat);
var dLng = ToRadians(lng0 - lng);
var h = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Sin(dLng / 2) * Math.Sin(dLng / 2);
var c = 2 * Math.Atan2(Math.Sqrt(h), Math.Sqrt(1 - h));
return R * c;
}
double ToRadians(double degrees) => degrees * PiBy180;
In the case of short distances where the curvature is negligible, you could use linear approximation and take the Euclidean distance between the points.
A quick and dirty approach for long distance measurements where curvature matters could involve pre-calculating arc lengths between points on the spheroid (i.e. earth) for distinct arc-lengths ahead of time. For instance, you would create an array(s)/lookup table to find approximate 𝘥 from quantized values of φ₀, φ₁ and Δλ (|λ₀-λ₁|) for a hemisphere about the longitudinal (vertical) axis, since the distances are the same for equal and opposite longitudinal differences. You can improve accuracy by increasing the size of the array(s). If your memory budget isn't tight, you can try making it very large.
Improving the accuracy of the approximation might be possible using specific data structures or correction formulas, but I'm not sure.
Then you can compare the range and the distance between the item and the player.

Check if point is inside a circle

I have a point expressed in lat/long
Position louvreMuseum = new Position( 48.861622, 2.337474 );
and I have a radius value expressed in meters. I need to check if another point, also expressed in lat/long, is inside the circle.
If I were on a flat surface I can simply use the formula
(x - center_x)^2 + (y - center_y)^2 <= radius^2
as deeply explained in these SO answer.
However as per the latitude/longitude usage I can not use that formula because of the spherical nature of the planet.
How can I calculate a distance from any given point to the center to be compared with the radius?
Function to calculate the distance between two coordinates (converted to C# from this answer):
double GetDistance(double lat1, double lon1, double lat2, double lon2)
{
var R = 6371; // Radius of the earth in km
var dLat = ToRadians(lat2-lat1);
var dLon = ToRadians(lon2-lon1);
var a =
Math.Sin(dLat/2) * Math.Sin(dLat/2) +
Math.Cos(ToRadians(lat1)) * Math.Cos(ToRadians(lat2)) *
Math.Sin(dLon/2) * Math.Sin(dLon/2);
var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1-a));
var d = R * c; // Distance in km
return d;
}
double ToRadians(double deg)
{
return deg * (Math.PI/180);
}
If the distance between the two points is less than the radius, then it is within the circle.

Trying to draw a square around a latitude and longitude point

I am working on a C# script to do some mapping. I have one function I am working on where I am given a latitude and longitude, a distance, and an angle of rotation.
I then want to create a rectangle, that has the width of the distance passed in (length is dependant on something else), this square would have the latitude and longitude point at it's centre, and would be rotated any angle from 0 - 360.
The distance double that is passed in can either be in feet, or in meters, and the units are determined using the isMetric boolean.
The problem I believe has to do with my formula's that I am using because when it draws the square from the 4 points, the size of the square is too large. As well the angle of rotation seems to be set at 45 degrees when you pass it a angle of 0.0.
Here is what I have so far:
Parameters: Latitude, Longitude (in decimal format), (Double) distance, (double) angle
double diagonal = Math.Sqrt(Math.Pow((distance / 2), 2) * 2); //a^2 + b^2 = d^2
if (isMetric) //Convert meters to km.
{
diagonal = diagonal / 1000;
}
else //Convert feet to km.
{
diagonal = diagonal * 0.0003048;
}
MessageBox.Show("Diagonal: " + diagonal, "DEBUG"); //DEBUG
double pt1_lat = latDistance(diagonal * Math.Sin(angle), latitude);
double pt1_long = longDistance(diagonal * Math.Cos(angle), latitude, longitude);
double pt2_lat = latDistance(diagonal * Math.Cos(angle), latitude);
double pt2_long = longDistance(-diagonal * Math.Sin(angle), latitude, longitude);
double pt3_lat = latDistance(-diagonal * Math.Sin(angle), latitude);
double pt3_long = longDistance(-diagonal * Math.Cos(angle), latitude, longitude);
double pt4_lat = latDistance(-diagonal * Math.Cos(angle), latitude);
double pt4_long = longDistance(diagonal * Math.Sin(angle), latitude, longitude);
The remaining methods are below:
private double latDistance(double distance, double latitude)
{
return latitude + degToRad(distance / EARTH_RADIUS);
}
private double longDistance(double distance, double latitude, double longitude)
{
return longitude + degToRad(distance / EARTH_RADIUS / Math.Cos(latitude));
}
private double degToRad(double degrees)
{
return (degrees * Math.PI) / 180;
}
public double radToDeg(double radians)
{
return (180.0 * radians) / Math.PI;
}
Any help is greatly appreciated :)
First of all I assume you have a "small" square with respect to EARTH radius. In this case if distance is the length of the side and angle is the rotation angle, you should compute dx,dy which are the cartesian coordinates of one vertex with respect to the center of the square:
dx = 0.5 * Math.Sqrt(2.0) * distance * Math.Cos(angle+0.25*Math.PI);
dy = 0.5 * Math.Sqrt(2.0) * distance * Math.Sin(angle+0.25*math.PI);
(here I assume that angle is in radians, otherwise you should convert it)
The cartesian coordinates of the other 3 vertices are, respectively: (-dy,dx), (-dx,-dy), (dy,-dx)
To convert the cartesian coordinates to (longitude,latitude) you can use your formulas:
pt_lat = latDistance(latitude,dy)
pt_long = longDistance(dx,latitude,longitude)

finding height on a heightmap stretched over a sphere C#

I'm looking for a bit of math help. I have a game were a 2D heightmap is generated and then stretched over a sphere using a length/direction formula. Now I need to know how to calculate the height between 2 points on my heightmap.
What I know:
The array that holds the heightmap
The angle in radians to my object
how many points there are on the heightmap
My problem look somewhat like so:
image
more images
The red and blue lines are the 2 heightmap points, and the light blue is where I'd like to calculate the height at.
Here's my current code to do it, but it doesn't work to well.
public double getheight(double angle)
{
//find out angle between 2 heightmap point
double offset = MathHelper.TwoPi / (heightmap.Length - 1);
//total brainfart attempt
double lowerAngle = offset * angle;
double upperAngle = offset * angle + offset;
//find heights
double height1 = heightmap[(int)lowerAngle];
double height2 = heightmap[(int)upperAngle];
//find offset angle
double u = angle - lowerAngle / (upperAngle - lowerAngle);
//return the height
return height1 + (height1 - height2) * u;
}
from my vegetation code, this seems to work okay, but is to rough to use for units and such, as they jump up/down as they move, due to it using only 1 heightmap point.
double[] hMap = planet.getHeightMap();
double i = hMap.Length / (Math.PI * 2);
this.height = hMap[(int)(angle * i)];
EDIT: example at end based on additional question info
Sounds to me like a linear interpolation - if you look at it from a 2d point of view, you've got two points:
(x1, y1) = point one on heightmap
(x2, y2) = point two on heightmap
and one point somewhere between (x1,x2) at an unknown height:
pu = (xu, yu)
A generic formula for LERP is:
pu = p0 + (p1 - p0) * u
where:
p0 = first value
p1 = second value
u = % where your unknown point lies between (p0,p1)
Here, we'll say p0 == y2 and p1 == y1. Now we need to determine "how far" the unknown point is between x1 and x2 - if you know the angles to the two heightmap points, this is easy:
u = ang(xu) - ang(x1) / (ang(x2) - ang(x1))
Alternatively, you could project your angle out to Max(y1,y2) and get the "unknown x pos" that way, then calculate the above.
So, let's try a contrived example:
p1 = point one in map = (1,2) therefore ang(p1) ~ 57 degrees
p2 = point two in map = (2,4) therefore ang(p2) ~ 114 degrees
note that here, the "x axis" is along the surface of the sphere, and the "y-axis" is the distance away from the center.
pu = object location = py #angle 100 degrees ~ 1.74 radians
px = (1.74 rad - 1 rad ) / (2 rad - 1 rad) = 0.74 / 1.0 = 0.74 => 74%
py = y0 + (y1 - y0) * u
= 2 + (4 - 2) * 0.74
= 2.96
Hopefully I didn't drop or misplace a sign there somewhere... :)
Ok, your example code - I've tweaked it a bit, here's what I've come up with:
First, let's define some helpers of my own:
public static class MathHelper
{
public const double TwoPi = Math.PI * 2.0;
public static double DegToRad(double deg)
{
return (TwoPi / 360.0) * deg;
}
public static double RadToDeg(double rad)
{
return (360.0 / TwoPi) * rad;
}
// given an upper/lower bounds, "clamp" the value into that
// range, wrapping over to lower if higher than upper, and
// vice versa
public static int WrapClamp(int value, int lower, int upper)
{
return value > upper ? value - upper - 1
: value < lower ? upper - value - 1
: value;
}
}
Our Test setup:
void Main()
{
var random = new Random();
// "sea level"
var baseDiameter = 10;
// very chaotic heightmap
heightmap = Enumerable
.Range(0, 360)
.Select(_ => random.NextDouble() * baseDiameter)
.ToArray();
// let's walk by half degrees, since that's roughly how many points we have
for(double i=0;i<360;i+=0.5)
{
var angleInDegrees = i;
var angleInRads = MathHelper.DegToRad(i);
Console.WriteLine("Height at angle {0}°({1} rad):{2} (using getheight:{3})",
angleInDegrees,
angleInRads,
heightmap[(int)angleInDegrees],
getheight(angleInRads));
}
}
double[] heightmap;
And our "getheight" method:
// assume: input angle is in radians
public double getheight(double angle)
{
//find out angle between 2 heightmap point
double dTheta = MathHelper.TwoPi / (heightmap.Length);
// our "offset" will be how many dThetas we are
double offset = angle / dTheta;
// Figure out two reference points in heightmap
// THESE MAY BE THE SAME POINT, if angle ends up
// landing on a heightmap index!
int lowerAngle = (int)offset;
int upperAngle = (int)Math.Round(
offset,
0,
MidpointRounding.AwayFromZero);
// find closest heightmap points to angle, wrapping
// around if we go under 0 or over max
int closestPointIndex = MathHelper.WrapClamp(
lowerAngle,
0,
heightmap.Length-1);
int nextPointIndex = MathHelper.WrapClamp(
upperAngle,
0,
heightmap.Length-1);
//find heights
double height1 = heightmap[closestPointIndex];
double height2 = heightmap[nextPointIndex];
// percent is (distance from angle to closest angle) / (angle "step" per heightmap point)
double percent = (angle - (closestPointIndex * dTheta)) / dTheta;
// find lerp height = firstvalue + (diff between values) * percent
double lerp = Math.Abs(height1 + (height2 - height1) * percent);
// Show what we're doing
Console.WriteLine("Delta ang:{0:f3}, Offset={1:f3} => compare indices:[{2}, {3}]",
dTheta,
offset,
closestPointIndex,
nextPointIndex);
Console.WriteLine("Lerping {0:p} between heights {1:f4} and {2:f4} - lerped height:{3:f4}",
percent,
height1,
height2,
lerp);
return lerp;
}

How do I draw a circle on my Bing Map application around a centerpoint with a radius given in miles

I have been able to draw an ellipse on my map using latitude and longitude values around a given center point. Although I see a shape on the map, I get an ellipse instead of a circle and I don't think it matches the distance specified. I intend to use this to display objects within that circle (this will be done later on once I can get the circle displaying properly, which is the reason why i need a circle not an ellipse as it should be perfectly round).
I am using a Bing Maps API. I wish to draw the circle in a given miles (distance) from the center which has been passed in through the parameter, the other variable in the parameter called miles is just holding a double value of 1D. I think the problem is to do with the way my maths is being calculated. Has anyone got a clue on how I can refine this code to calculate my miles better.
private void drawPoly(SearchLocation center, Double miles)
{
//amount of vertex
double vertexCount = 100D;
//used by the api to carried out searches
List<SearchLocation> vertices = new List<SearchLocation>();
double v = 0;
double radians = Math.PI / 180D;
double radiansPerDegree = Math.PI / 180D;
double degreePerVertex = 360D / vertexCount;
double radiansPerVertex = degreePerVertex * radiansPerDegree;
var centerOfMap = center;
const double degLatMiles = 68.68637156368D;
double degLonMiles = Math.Cos(center.Latitude.Value) * (68.68637156368D);
double milesLat = (miles * degLatMiles) / 3600;
double milesLon = (miles * degLonMiles) / 3600;
for (v = 0; v < vertexCount; v++)
{
radians = v * radiansPerVertex;
//adds the miles from the center point and draws a circle
double centrLat = center.Latitude.Value + (milesLat * Math.Sin(radians));
double centrLon = center.Longitude.Value + (milesLon * Math.Cos(radians));
vertices.Add(new SearchLocation() { Latitude = centrLat, Longitude = centrLon });
}
Ok, I've misundestood your question. This should work :
/// <summary>
/// Calculates the end-point from a given source at a given range (meters) and bearing (degrees).
/// This methods uses simple geometry equations to calculate the end-point.
/// </summary>
/// <param name="source">Point of origin</param>
/// <param name="range">Range in meters</param>
/// <param name="bearing">Bearing in degrees</param>
/// <returns>End-point from the source given the desired range and bearing.</returns>
public static PointLatLng CalculateDerivedPosition(PointLatLng source, double range, double bearing)
{
double latA = source.Lat * DEGREES_TO_RADIANS;
double lonA = source.Lng * DEGREES_TO_RADIANS;
double angularDistance = range / EARTH_RADIUS_M;
double trueCourse = bearing * DEGREES_TO_RADIANS;
double lat = Math.Asin(
Math.Sin(latA) * Math.Cos(angularDistance) +
Math.Cos(latA) * Math.Sin(angularDistance) * Math.Cos(trueCourse));
double dlon = Math.Atan2(
Math.Sin(trueCourse) * Math.Sin(angularDistance) * Math.Cos(latA),
Math.Cos(angularDistance) - Math.Sin(latA) * Math.Sin(lat));
double lon = ((lonA + dlon + Math.PI) % (Math.PI * 2)) - Math.PI;
return new PointLatLng(
lat / DEGREES_TO_RADIANS,
lon / DEGREES_TO_RADIANS);
}
Juste take your center as source :
for (int i = 0; i < 360; i++)
{
vertices.Add(CalculateDerivedPosition(center, circleRadius, i));
}
To prevent elipses on sertain latitudes I use the following code:
// Function to draw circle on map:
private void DrawCircle(BasicGeoposition CenterPosition, int Radius)
{
Color FillColor = Colors.Purple;
Color StrokeColor = Colors.Red;
FillColor.A = 80;
StrokeColor.A = 80;
Circle = new MapPolygon
{
StrokeThickness = 2,
FillColor = FillColor,
StrokeColor = StrokeColor,
Path = new Geopath(Functions.CalculateCircle(CenterPosition, Radius))
};
mpBingMaps.MapElements.Add(Circle);
}
// Constants and helper functions:
const double earthRadius = 6371000D;
const double Circumference = 2D * Math.PI * earthRadius;
public static List<BasicGeoposition> CalculateCircle(BasicGeoposition Position, double Radius)
{
List<BasicGeoposition> GeoPositions = new List<BasicGeoposition>();
for (int i = 0; i <= 360; i++)
{
double Bearing = ToRad(i);
double CircumferenceLatitudeCorrected = 2D * Math.PI * Math.Cos(ToRad(Position.Latitude)) * earthRadius;
double lat1 = Circumference / 360D * Position.Latitude;
double lon1 = CircumferenceLatitudeCorrected / 360D * Position.Longitude;
double lat2 = lat1 + Math.Sin(Bearing) * Radius;
double lon2 = lon1 + Math.Cos(Bearing) * Radius;
BasicGeoposition NewBasicPosition = new BasicGeoposition();
NewBasicPosition.Latitude = lat2 / (Circumference / 360D);
NewBasicPosition.Longitude = lon2 / (CircumferenceLatitudeCorrected / 360D);
GeoPositions.Add(NewBasicPosition);
}
return GeoPositions;
}
private static double ToRad(double degrees)
{
return degrees * (Math.PI / 180D);
}
This code is usefull for small radius of less than a few miles.

Categories