I have two points on a map, I want to create a rectangle where the two points are the line that intersect the rectangle. I understand that there is no true rectangle on a sphere, but the areas I am dealing with are small, the length of the rectangles are no more than a few km and the heights a few hundred meters. So if the calculation is approximate that's fine. Any help appreciated! Thanks, Philip
Asked
Active
Viewed 2,771 times
1
-
2Do you want the two points to form a diagonal of the rectangle? – turkeyhundt Sep 04 '15 at 02:57
-
No - through the mid points of two of the sides – pogorman Sep 04 '15 at 03:00
-
Then you don't have enough information. How 'tall' do you want the rectangle to go? Like if your line is going east-west, how far north and south do you want the box to go? If you have a value in mind, then you can do this. – turkeyhundt Sep 04 '15 at 03:01
-
Midpoint of two adjacent sides of a rectangle? Then two solutions are possible. Midpoints of two opposite sides? Infinite such rectangles. – Mihir Sep 04 '15 at 03:03
-
sorry i should have described it better, i have a line and given a thickness i need to give this line a thickness, i.e. create a rectangle where the line intersects the midpoint of two adjacent sides of a rectangle. – pogorman Sep 04 '15 at 03:08
2 Answers
1
The simplest case: Let the two points be $(x_1,y_1)$ and $(x_2,y_1)$ and the thickness of rectangle is $t$. The coordinates of the rectangle are: $(x_1,y_1+t/2),(x_1,y_1-t/2),(x_2,y_1-t/2),(x_2,y_1+t/2)$
Mihir
- 622
-
thanks! - I didnt think it would be so simple, I guess I just have to convert the given thickness in meters to lat and long degrees: http://gis.stackexchange.com/questions/2951/algorithm-for-offsetting-a-latitude-longitude-by-some-amount-of-meters – pogorman Sep 04 '15 at 03:18
-
2As long as the two points share either a longitude or latitude. As Mihir said, simplest case. – turkeyhundt Sep 04 '15 at 03:19
-
ok - im getting close this gives me a parallelogram, what I want to achieve is a 90º angled rectangle – pogorman Sep 04 '15 at 04:04
0
My algorithm/calculation almost works, however in some directions the rectangles aren't right angled. I want to know why, there is a fair amount of approximation in my code, which should be fine based on the scales I use (a few km). I shouldn't have to worry about the curvature of the earth.
Anybody know how I can improve my code? Here are my references:
here is the image :map retangles
public static MapRectangle CreatMapRectangle(BalingZone zone, double thickness)
{
MapsCoordinate rectpoint2;
MapsCoordinate rectPoint1 ;
MapsCoordinate rectPoint3 ;
MapsCoordinate rectPoint4 ;
var point1 = zone.Coordinates[0];
var point2 = zone.Coordinates[1];
var latitudeDiff = LatitudeDiffToMeters(point2.Latitude - point1.Latitude);
var longitudeDiff = LongitudeDiffToMeters(point1.Longitude - point2.Longitude, point1.Latitude);
var slopeB = longitudeDiff / latitudeDiff;
double latOffset = thickness * (slopeB / Math.Sqrt(1 + slopeB * slopeB));
double longOffset = thickness * (1 / Math.Sqrt(1 + slopeB * slopeB));
double p3Lat = CalculateLatitude(point1.Latitude, latOffset);
double p3Long = CalculateLongitude( point1.Longitude, p3Lat , longOffset);
rectPoint1 = new MapsCoordinate(p3Lat, p3Long);
double p4Lat = CalculateLatitude(point1.Latitude, -latOffset);
double p4Long = CalculateLongitude(point1.Longitude, p4Lat, -longOffset);
rectpoint2 = new MapsCoordinate(p4Lat, p4Long);
double p5Lat = CalculateLatitude(point2.Latitude, latOffset);
double p5Long = CalculateLongitude( point2.Longitude, p5Lat , longOffset);
rectPoint4 = new MapsCoordinate(p5Lat, p5Long);
double p6Lat = CalculateLatitude(point2.Latitude, -latOffset);
double p6Long = CalculateLongitude( point2.Longitude, p6Lat , -longOffset);
rectPoint3 = new MapsCoordinate(p6Lat, p6Long);
return new MapRectangle(rectPoint4, rectPoint3, rectPoint1, rectpoint2, thickness);
}
//use the quick and dirty estimate that 111,111 meters (111.111 km) in the y direction is 1 degree (of latitude)
// and 111,111 * cos(latitude) meters in the x direction is 1 degree (of longitude).
private static double LatitudeDiffToMeters(double latitudeDiff)
{
return 111111.0 * latitudeDiff;
}
private static double LongitudeDiffToMeters(double longitudeDiff, double latitude)
{
return 111111.0*Math.Cos(latitude)*longitudeDiff;
}
private static double CalculateLatitude(double latitude, double offset)
{
return latitude + offset/111111.0;
}
private static double CalculateLongitude(double longitude, double latitude, double offset)
{
return longitude + offset/(111111.0*Math.Cos(latitude));
}
}