Reputation: 21
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 :)
Upvotes: 1
Views: 2617
Reputation: 10172
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)
Upvotes: 0