# Calculate 'Rectangle' Coordinates Given 2 Points and width [closed]

I want to create a rectangular polygon using two points as guides. So let's say a journey starts in Egypt and ends in London, my polygon should have 4 points:

• 10 miles further from London than Egypt is, following the line between them (roughly south in this example).
• Halfway between the two cities but 50 miles at right angles from the line that joins them.
• Like the above point but 50 miles in the other direction.
• 20 miles further from Egypt than London is, following the line between them (roughly north in this example).

I'll end up with a rough diamond shaped polygon that would completely contain the straight line journey from Egypt to London.

I hope this makes sense; any help for how I can calculate the 4 points is appreciated.

-

## closed as off topic by whuber♦Jan 16 '13 at 17:31

Questions on Geographic Information Systems Stack Exchange are expected to relate to geographic information systems within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

What software are you using? – whuber Jan 15 '13 at 19:40
Are you talking about a convex hull around the great circle between two points? An image might help better explain what you're after, because a "diamond shape" won't have two right angles. – Mintx Jan 15 '13 at 20:01
Have added a link to an screenshot, hopefully it makes more sense. Ill be using PHP and Mysql to process everything. – fiscme Jan 15 '13 at 21:00
At face value, this is quite easily solved by simple mathematical principles. Where you are going to run into trouble, however, is when you a) have to manage your calculations for each of the 4 quadrants b) end up crossing over meridians. – nagytech Jan 16 '13 at 1:29
are you interested only in the 2D geometry, or do you want to take the spherical shape of the earth into account as well? – Devdatta Tengshe Jan 16 '13 at 4:54

I will post another answer to presents a new way to do this, since my first way may be useful to other users that uses GIS software.

Well, try this PHP function to help you in the ST_Azimut and ST_Distance_Sphere. This will give you the distance and the azimuth if you pass the two coordinates of source and target. Please check if its in miles, kilometers or nautical miles ( I don't know ). Sorry about some portuguese var names.

PHP:

``````function CalcDistAndAzimuth(\$lat1, \$lon1, \$lat2, \$lon2) {
\$dlat = 0.0;
\$dlon = 0.0;
\$apart = 0.0;
\$alfa = 0.0;
\$latm = 0.0;
\$rumo = 0.0;
\$distancia = 0.0;
if ((\$lon1 == \$lon2) && (\$lat1 == \$lat2)) {
\$rumo = 0;
\$distancia = 0.0;
} else {
\$dlat = \$lat2 - \$lat1;
\$dlon = \$lon2 - \$lon1;
\$latm = abs((\$lat1 + \$lat2) / 2.0);
\$apart = \$dlon * cos(\$latm * pi() / 648000); // 180*3600
// calc distance and azimuth
if (\$dlat == 0) {
\$distancia = abs((\$apart) / 60);
if (\$lon2 > \$lon1)
\$rumo = 90;
else
\$rumo = 270;
}
if (\$dlon == 0) {
\$distancia = abs((\$dlat) / 60.0);
if (\$lat2 > \$lat1)
\$rumo = 0;
else
\$rumo = 180;
}
if ((\$dlat != 0) && (\$dlon != 0)) {
\$alfa = atan(abs(\$apart / \$dlat));
\$distancia = abs((\$apart / (sin(\$alfa))) / 60);
if (\$dlat <= 0) {
if (\$dlon <= 0)
\$rumo = 180.0 + (\$alfa * 180.0 / pi());
else
\$rumo = 180.0 - (\$alfa * 180.0 / pi());
} else {
if (\$dlon <= 0)
\$rumo = 360.0 - + (\$alfa * 180.0 / pi());
else
\$rumo = (\$alfa * 180.0 / pi());
}
}
}

\$rumodistancia["distance"] = \$distancia;
\$rumodistancia["azimuth"] = \$rumo;

return \$rumodistancia;
}
``````

This is other helpful function. Given a point, an azimuth and a distance, return a new point.

``````function CalcCinematic(\$lat1, \$lon1, \$azimuth, \$dist) {
// \$dist is in miles
// \$azimuth is in degrees
\$dlat = 0.0;
\$dlon = 0.0;
\$apart = 0.0;
\$alfa = 0.0;
\$latm = 0.0;
\$delta = 0.0;
if (\$rumo <= 90.0) {
// convert azimuth from degrees to radians
\$alfa = \$rumo * pi() / 180.0;
\$dlat = \$dist * cos(\$alfa);
\$apart = \$dist * sin(\$alfa);
} else
if (\$rumo <= 180.0) {
\$alfa = (180.0 - \$rumo) * pi() / 180.0;
\$dlat = (-1) * \$dist * cos(\$alfa);
\$apart = \$dist * sin(\$alfa);
} else
if (\$rumo <= 270.0) {
\$alfa = (\$rumo -180.0) * pi() / 180.0;
\$dlat = (-1) * \$dist * cos(\$alfa);
\$apart = (-1) * \$dist * sin(\$alfa);
} else
if (\$rumo <= 360.0) {
\$alfa = (360.0 - \$rumo) * pi() / 180.0;
\$dlat = \$dist * cos(\$alfa);
\$apart = (-1) * \$dist * sin(\$alfa);
}
\$lat2 = \$lat1 +round(\$dlat * 60);
if (\$lat2 < -324000) {
\$lat2 = (-1) * 648000 + \$lat2;
\$delta = 648000;
} else
if (\$lat2 > 324000) {
\$lat2 = 648000 - \$lat2;
\$delta = 648000;
}

\$latm = abs((\$lat1 + \$lat2) / 2.0);
\$dlon = (\$apart * 60.0) / cos(\$latm * pi() / 648000);
\$lon2 = \$lon1 +round(\$dlon);

if (\$lon2 > 648000)
\$lon2 = -1296000 + \$lon2;
else
if (\$lon2 < -648000)
\$lon2 = 1296000 - \$lon2;

if (\$delta == 648000) {
\$lon2 = \$lon2 +round(\$delta);
if (\$lon2 > 648000) {
\$lon2 = -1296000 + \$lon2;
}
}

\$newpoint["lat"] = \$lat2
\$newpoint["lon"] = \$lon2;
return \$newpoint;
}
``````
-
I think lat / lon is in seconds. Please check. – Magno C Jan 16 '13 at 10:48

Do this :

1) Calculate the distance of Hypotenuse between your two points using `ST_Distance_Sphere`.

2) Divide by four ( or other number of points )

3) Calculate the azimuth between Egypt and London using `ST_Azimuth`

4) Project the first point from Egypt, using the azimuth and the distance from item 2, using `ST_Project`.

5) From this point, project the second point, using same way, and so on until you reach London.

Note the use of radians in some functions. You can translate using `Degrees()` PostGIS function. You can create any pathway using this method.

-
By the way : You will need PostGIS 2.1 to use ST_Azimuth and ST_Project. – Magno C Jan 16 '13 at 10:04
Thanks but i dont have PostGIS or use PostgreSQL, i want to try and use as much pure maths as I can so its pretty software independant – fiscme Jan 16 '13 at 10:18
Understood, but if you're working with GIS, consider using GIS functions and GIS databases. It's much more easy to achieve your goals and the results is more confiable. – Magno C Jan 16 '13 at 10:27
Thanks, appreciate your help. – fiscme Jan 16 '13 at 15:48

I posted this question over at math.stackexchange.com after i realised only need to worry about 2D calculations and really wanted a non programming specific solution.

@ju'x answered it brilliantly, so credit should go to him.

Calculate 'Rectangle' Coordinates Given 2 Points and width

-
Unfortunately, that answer is wrong, because it uses Cartesian calculations rather than spherical ones. But since you have cross-posted the question and appear to prefer the version on math, we'll close this version for you. – whuber Jan 16 '13 at 17:31
Fair enough, the solution I needed didn't need to be exact so that answer worked. – fiscme Jan 16 '13 at 19:55