How do you find relative bearing between two points? - android

double computeHeading(double latitude1, double longitude1, double latitude2, double longitude2)
{
double degToRad = PI / 180.0;
double phi1 = latitude1*degToRad;
double phi2 = latitude2*degToRad;
double lam1 = longitude1*degToRad;
double lam2 = longitude2*degToRad;
double x,y;
x = cos(phi2) * sin(lam2-lam1);
printf("X is %lf\n", x);
y = cos(phi1) * sin(phi2) - sin(phi1) * cos(phi2) * cos(lam2-lam1);
printf("Y is %lf\n", y);
return atan2(x,y)*180/PI;
}
I am using the above function to determine the true bearing from North between two geographic coordinates.
I'm currently developing a small navigation widget which uses GPS data from Android sensors. The widget has an arrow facing towards a point away from the device's current location. The arrow's direction changes with the device's current location and azimuth to always face the distant point.
Here is a scenario:
I'm at a location, facing north, and another location has a bearing of 300 degrees(somewhat northwest of me). If I face towards south, without moving, my relative bearing to the distant location should be 120 degrees.
How can I find the relative bearing with accounting for the facing direction (azimuth)?

There are a couple of ways you can work this out. The first, which is what you appear to be doing, assumes the earth is spherical. Relative bearings are calculated using Haversine formulation for great circle navigation. Given starting and ending points, this formulation finds the great circle passing through the two points. From this an initial bearing can be calculated. This great circle route is the shortest route between the two points, but suffers from the problem the bearing, in general, will not be constant along the route. Also, except under some very specific cases, the reverse bearing does not behave as you seem to expect and if you want to determine it in general, you will have to perform another calculation reversing the starting and ending points.
Another method you could use is the Rhumb line formulation. In this case, the bearing between the starting point and ending point is constant and would allow you to use the relation you have for the reverse course if you would like. Since this will in general differ from the great circle distance, following Rhumb lines will not result in the shortest path between the two points, but it does simplify the navigation by holding the course constant.
Both of these approaches are described in detail at Calculate distance, bearing and more between Latitude/Longitude points
Another formulation for great circle navigation which uses a more accurate representation of the earth's shape, an oblate spheriod, which is a special type of ellipsoid, is attributed to Vincenty with additional enhancements provided by Karney. In these cases, the formulation is quite a bit more complicated and is probably overkill for most applications, and performance is quite a bit worse than the Haversine formulations above. But these formulations provide much better accuracy if you need it.
Update:
Based on the comment below, the main issue is one of figuring out how far to turn. This will simply be the angle between the normals of the plane containing the great circles for the current heading and the desired heading. To get the normal for the plane on the current heading, you need your current location L and a point some distance away on the current heading, C. The normal is just V = L×C. To compute the normal for the plane containing the great circle along the desired heading, you only need to know a point along the desired route, which you already have in the form of your destination point, which we call D. You can then find the normal by U = L×D. The angle between them is given by θ = acos((U∙V)/(|U||V|)).
In order to find L, C and D you must convert the Latitude, Longitude, Altitude (LLA) coordinates into Earth Centered, Earth Fixed (ECEF) coordinates.

Related

Map v2 convert distance to pixels

I have been searching but I couldn't find it. I am using google map v2. Is there any way I can convert distance ( meters) to pixels on my screen? I need the pixels equivalent of the distance. I have the distance and I have the zoom level.
Any help is appreciated.
If I understand correctly, you have a MapFragment on your screen showing a map. You know the distance in meters between 2 points on the map that are shown on the screen and you want to calculate the distance between those two points in pixels. If you know the LatLng location of the two points, you can use the Projection class like this:
Point point1 = map.getProjection().toScreenLocation(latLng1);
Point point2 = map.getProjection().toScreenLocation(latLng2);
and then you just need to use the distance mathematic formula:

Pan map in a single direction

My V2 Map has been set in a random position, with a random bearing from north (so the compass symbol is likely to be pointing anywhere, but probably not north).
I want to shift/pan the map directly up the screen as the user looks at it, a short distance.
This may be very simple or require some complex calculations with lats, longs and trigonometry.
But I'm struggling to find a solution that works whatever the angle and zoom level is.
How can i do this ?
Well I solved this by finding a position on the screen and converting it to a LatLng, and then panning the map to this position. It worked perfectly.
Projection projection = mapView.getProjection();
Point p = new Point(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
LatLng pos = projection.fromScreenLocation(p);

How to find a point is located inside a line or polygon

I found a point is located inside a Circle using following method.
Distance between a point and circle center <= circle radius means the point is inside circle.
But I need to find
The point is located in a line
The point is located in a polygon
Normally polyline has array of coordinates,if i check with all points in-between points will miss while calculating.
Update:
I used github.com/sromku/polygon-contains-point to find the point is located in a polygon. But it is 2D normal plane. But I need to find a point inside a Polygon calculation on spheroid.How to do this?
q2: search for "point in polygon strategies", from graphic gems.
Point in Polygon also works if the coordinates are longitude (x), latitude (y).
At least the algo that count for line crossings, which mostly are the one implemented.
Of course if the polygn crosses the datum limit (by ship or plane) or is located at the polar region, then this does not work with the simple apporach.
q1: point line: in school you learned that (abs(normal distance to line) < epsilon)
but what you need is distance to line segment. (not line which has infinity length).
For this task you have to transfrom to cartestioan coordinates first.

finding specifically where an object is for collision detection purposes

I have a square that rotates to a random angle and then travels in a straight line in the direction it is pointing. It does this by using a variable as its x axis and then calling
Variable++
Each frame.
unfortunatley i cannot work out how to return the exact position of the square because the square can be travelling at any angle and therefore doesn't rigidly follow the world coordinte grid. This means that the x variable is not the shapes x coordinate.
How do i return the shapes exact coordinates and how do i do it in such a way that i can have two squares drawn from the same class behaving differently.
So you've got a measure of distance from where the object started along its internal sideways axis and a measure of the angle between that axis and the horizontal?
If so then the formula you want is simple trigonometry. Assuming the object started at (x, y) and has travelled 'distance' units along an axis at an angle of 'angle' with the horizontal then the current position (x', y') is:
x' = x + distance * cos(angle)
y' = y + distance * sin(angle)
If you have the origin in the lower left of the screen and axes arranged graph paper style with x increasing to the right and y increasing as you go upward, that assumes that the angle is measured anticlockwise and that the object is heading along positive x when angle is zero.
If you'll permit a hand waving explanation, the formula works because one definition of sine and cosine is that they're the (x, y) coordinates of the point on the outside of a unit circle at the angle specified. It also matches with the very first thing most people learn about trigonometry, that sine is 'opposite over hypotenuse', and cosine is 'adjacent over hypotenuse'. In this case your hypotenuse has length 'distance' and and you want to get the 'opposite' and 'adjacent' lengths of a right angled triangle that coincides with the axes.
Assuming Android follows J2SE in this area, the one thing to watch out for is that Math.sin and Math.cos take an angle in radians, whereas OpenGL's rotatef takes an argument in degrees. Math.toDegrees and Math.toRadians can do the conversion for you.
When you made the shape you should have already specified its X & Y coordinates. Im not too sure what you mean when you say you cant find the coordinates?
Also make sure you do fame independent movement; currently you are adding one to your variable on every loop of your program. This means if it runs a 60 Frame Per Second(FPS) it will move 60 units, but if it runs at 30FPS it will move at half the speed

What are the latitude and longitude of a circle?

In Android, if I have a circle's radius and center value, how could I measure the latitude and longitude of about 1 km periphery of that circle?
Horizontal = x-axis = Longitude
Vertical = y-axis = Latitude
Latitude and longitude are terms necessary to define a position on a sphere, where two angles are necessary. These terms have no relevance to a circle, which exists only in one plane.
This is not an easy problem to solve as the distance between each latitude and longitude varies depending upon where on the globe you are (I'm sure some maths boffin could do it, but it is beyond me). For instance at the north and south pole one degree longitude measures no distance at all, but at the equator one degree can be several kilometers. If however your search only relates to a small are of the globe, then a you can make a reasonable approximation by using 1km = x degrees where x is the correct value where you are.
Terms "circle", "radius" implies a 2D flat plane geometry whereas "latitude", "longitude" implies a geographical coordinates on a 3D ellipsoid.
Putting these problems asides, let's assume, in fact that your "circle" is a point on the surface of our Earth and, the "radius" is a distance from this point. This will correct your terms so they define a 3D problem.
If a projection is given. Then we can specify the "center" in planar geometry coordinates (X, Y). We can specify all points distance "R" in geometry coordinates as (X + R * cos(phi), Y + r * sin(phi)) then we can use the projection to convert the geometry coordinates from planar to geographic i.e. longitude, latitude. The problem is 1000s of projections exists and not all a valid across the entire Earth.
Alternatively, you can define your 3D ellipsoid in 3D space using (X, Y, Z) coordinates. Then the wording of your problem must change. The "circle" because a sphere whose point is on the surface of the ellipsoid and the "radius" is the radius of the sphere. To compute all points distance "R" from the sphere you will need to intersect the sphere with the ellipsoid using trigonometry. After you have done this, you can convert the resultant 3D coordinates back to longitude, latitude based on the definition of your ellipsoid.

Categories

Resources