I understand that you are looking for a function geo_rect(x,y,w,h,a)
with the following parameters
x
is the longitude according to WGS84
y
is the latitude
w
is the width of the rectangle in meters
h
is the height of the rectangle in meters
a
is the angle to which the rectangle is turned from w
being horizontal (meaning pointing exactly West to East). I suggest to allow values ranging within the open interval (-90°,90°) as this makes the math either to understand.
Your function getRectangle(LatLon position, int rectWidthCm, int rectLengthCm, double rectRotation)
deliver all the required information, you need a small wrapper function which determines w
, h
, and a
from rectWidthCm
, rectLengthCm
and rectRotation
, with the latter being within [0°,360°)
.
The function geo_rect()
will return an arrayOf<LatLon>
of length four, namely the coordinates of all four corners, starting on the top left and then going clockwise. We will refer to the points as P_NE
,P_NW
,P_SE
, and P_SW
respectively.
Assumptions
In order to keep things mathematically feasible, we make some assumptions
- We assume that we can use as approximation that the rectangle is a plane, which is okay if
w ~ h << r
with r = 6378 km
being the radius of the Earth.
- We further assume that the Earth is a ideal sphere rather than an ellipsoid or even more bumpy. For an accessible article on that issue, see e.g. Zachary C. Eilon's blog
Basic structure of the algorithm
The algorithm could be structured as follows:
Determine the distance d
from (x,y)
to all four end points. Because of our first assumption we can use simple Euclidian geometry rather than intricate Spherical geometry. Pythagoras holds: d^2 = (w/2)^2 + (h/2)^2
.
We also need the four bearings, e.g. b_NW
for the angle between the vector pointing to the North Pole and the vector pointing from (x,y)
to point P_NW
.
Given the information (x
,y
,d
,b_NW
, b_NE
, b_SW
, b_SE
) from the previous steps, we can now follow Get lat/long given current point, distance and bearing to calculate the position of all four points. This is the mathematically hard part where I suggest to use a well-established and tested library for.
Last but not least, let us double-check whether the calculation went well by evaluating Great circle distances between some or all pairs of points. For instance d(P_NE,P_NW)
should approximately be w
, d(P_NW,P_SW)
should approximately be h
. Don't be surprised if there is actually a difference - this errors are due the assumptions we made. Normal GPS under usual conditions will anyhow not allow you to determine your position up to the centimeter, you will need DPGS for that.
Further reading
If you are looking for java implementations, I found e.g.