How to calculate x number of points on a line? - android

I am working with drawing polylines on google maps. Given an origin and destination (latitude,longitude) coordinates how can I get 'x' number of points on that line?
I have tried applying mathematical concepts such as the equation of a line y = mx + c or even (x-x1)/(x1-x2) = (y-y1)/(y1-y2) but these methodologies do not work. The world is not flat. What is the formula for finding all points in a linear line for latitude/longitude values? Does anyone have any ideas for this? I believe I have to apply this equation: https://en.wikipedia.org/wiki/Mercator_projection
EDIT:
Someone suggested trying to convert the lat/lng to points and then doing the math and then converting back to lat/lng. There seems to be a large margin or error when doing this. Latitude is accurate, but Longitude is completely off. The TILE_SIZE = 256, the size of tiles that Google returns for Google Maps
public GoogleMapsProjection2() {
this._pixelOrigin = new PointF(TILE_SIZE / 2.0, TILE_SIZE / 2.0);
this._pixelsPerLonDegree = TILE_SIZE / 360.0;
this._pixelsPerLonRadian = TILE_SIZE / (2 * Math.PI);
}
public PointF fromLatLngToPoint(double lat, double lng, int zoom) {
PointF point = new PointF(0, 0);
point.x = _pixelOrigin.x + lng * _pixelsPerLonDegree;
// Truncating to 0.9999 effectively limits latitude to 89.189. This is
// about a third of a tile past the edge of the world tile.
double siny = bound(Math.sin(degreesToRadians(lat)), -0.9999, 0.9999);
point.y = _pixelOrigin.y + 0.5 * Math.log((1 + siny) / (1 - siny)) * -_pixelsPerLonRadian;
int numTiles = 1 << zoom;
point.x = point.x * numTiles;
point.y = point.y * numTiles;
return point;
}
public PointF fromPointToLatLng(PointF point, int zoom) {
int numTiles = 1 << zoom;
point.x = point.x / numTiles;
point.y = point.y / numTiles;
double lng = (point.x - _pixelOrigin.x) / _pixelsPerLonDegree;
double latRadians = (point.y - _pixelOrigin.y) / -_pixelsPerLonRadian;
double lat = radiansToDegrees(2 * Math.atan(Math.exp(latRadians)) - Math.PI / 2);
return new PointF(lat, lng);
}
public final class PointF {
public double x;
public double y;
public PointF(double x, double y) {
this.x = x;
this.y = y;
}
}

A line segment in a Google Maps polyline is a geodesic line. In a spherical earth approximation, this is a great circle. The polyline with a single segment (your origin and destination in lat/long coordinates) is rendered onto a 2D map using a projection, but it's still a geodesic line (great circle).
GIS stack exchange has this question of interpolating two (latitude, longitude) coordinates to compute waypoints. The answer suggests using GeographicLib, which has a java library, and provides a JavaScript example. The resulting waypoints in latitude/longitude coordinates are your inputs to the polyline.
It's best to perform the interpolation in the latitude/longitude coordinate system to avoid magnifying errors with projection or discretization.

Related

What could a function look like that receives three locations and three radius and outputs one coordinate showing the overlapping?

I have three gps locations as double lat and lng. I have three radius that correspond to each of the lat and lng values. The radius form circles around the locations. I want to determine the one point where all three circles overlap.
My starting point:
(x−lat_1)^2+(y−lng_1)^2=r_1^2
(x−lat_2)^2+(y−lng_2)^2=r_2^2
(x−lat_3)^2+(y−lng_3)^2=r_3^2
But here I am stuck - not only is that system of equations over-determined, it is also unclear, how to mix up degrees, minutes and seconds with a radius in meters.
What could a function(pseudocode is enough) look like that receives three locations and three radius and outputs one coordinate showing the overlapping.
Speaking of which, there needs to be some tolerance, as neither the radius nor the locations are too precise.
Take a look at this question:
Find intersecting point of three circles programmatically
I'm posting here the code that does what you need:
private static final double EPSILON = 0.000001;
private boolean calculateThreeCircleIntersection(double x0, double y0, double r0,
double x1, double y1, double r1,
double x2, double y2, double r2)
{
double a, dx, dy, d, h, rx, ry;
double point2_x, point2_y;
/* dx and dy are the vertical and horizontal distances between
* the circle centers.
*/
dx = x1 - x0;
dy = y1 - y0;
/* Determine the straight-line distance between the centers. */
d = Math.sqrt((dy*dy) + (dx*dx));
/* Check for solvability. */
if (d > (r0 + r1))
{
/* no solution. circles do not intersect. */
return false;
}
if (d < Math.abs(r0 - r1))
{
/* no solution. one circle is contained in the other */
return false;
}
/* 'point 2' is the point where the line through the circle
* intersection points crosses the line between the circle
* centers.
*/
/* Determine the distance from point 0 to point 2. */
a = ((r0*r0) - (r1*r1) + (d*d)) / (2.0 * d) ;
/* Determine the coordinates of point 2. */
point2_x = x0 + (dx * a/d);
point2_y = y0 + (dy * a/d);
/* Determine the distance from point 2 to either of the
* intersection points.
*/
h = Math.sqrt((r0*r0) - (a*a));
/* Now determine the offsets of the intersection points from
* point 2.
*/
rx = -dy * (h/d);
ry = dx * (h/d);
/* Determine the absolute intersection points. */
double intersectionPoint1_x = point2_x + rx;
double intersectionPoint2_x = point2_x - rx;
double intersectionPoint1_y = point2_y + ry;
double intersectionPoint2_y = point2_y - ry;
Log.d("INTERSECTION Circle1 AND Circle2:", "(" + intersectionPoint1_x + "," + intersectionPoint1_y + ")" + " AND (" + intersectionPoint2_x + "," + intersectionPoint2_y + ")");
/* Lets determine if circle 3 intersects at either of the above intersection points. */
dx = intersectionPoint1_x - x2;
dy = intersectionPoint1_y - y2;
double d1 = Math.sqrt((dy*dy) + (dx*dx));
dx = intersectionPoint2_x - x2;
dy = intersectionPoint2_y - y2;
double d2 = Math.sqrt((dy*dy) + (dx*dx));
if(Math.abs(d1 - r2) < EPSILON) {
Log.d("INTERSECTION Circle1 AND Circle2 AND Circle3:", "(" + intersectionPoint1_x + "," + intersectionPoint1_y + ")");
}
else if(Math.abs(d2 - r2) < EPSILON) {
Log.d("INTERSECTION Circle1 AND Circle2 AND Circle3:", "(" + intersectionPoint2_x + "," + intersectionPoint2_y + ")"); //here was an error
}
else {
Log.d("INTERSECTION Circle1 AND Circle2 AND Circle3:", "NONE");
}
return true;
}
Usage:
calculateThreeCircleIntersection(-2.0, 0.0, 2.0, // circle 1 (center_x, center_y, radius)
1.0, 0.0, 1.0, // circle 2 (center_x, center_y, radius)
0.0, 4.0, 4.0);// circle 3 (center_x, center_y, radius)
As you said, you probably need to do some unit conversion here. There is some complicated formula that calculates distance between two geolocations, so you need to reverse it to get meters from radian based distance.
Here you may find implementations of this calculation and try to reverse it:
Calculate distance between two latitude-longitude points? (Haversine formula)

Click inside the Path in Openstreetmap

I have two overlays in my openstreetmap app, Itemized overlay and Path overlay together. I want to provide click inside the path inside the path overlay and custom marker
Not available with PathOverlay.
Use OSMBonusPack Polyline.
If you just want to open a bubble, use setInfoWindow.
If you want to do something else, inherit from Polyline class, override onSingleTapConfirmed, and use isCloseTo.
Finally I got answer for the Above question...
Add all path overlays into a single layer.
In on single tap check isPointOnLine(lox,loy,ltx,lty, x, y)
public boolean isPointOnLine(double lox, double loy, double ltx,
double lty, double x, double y) {
// determine if point is on line
Double dx = x - lox;
Double dy = y - loy;
Double tx = ltx - lox;
Double ty = lty - loy;
// normalise the line vector
Double t1 = new Double(1 / Math.sqrt(tx * tx + ty * ty));
tx *= t1;
ty *= t1;
// calculate inverse length of secondary vector
Double dl = new Double(1 / Math.sqrt(dx * dx + dy * dy));
// take dot product of normalised line vector, and rotated normalised
// secondary vector
Double dot = (dy * tx - dx * ty) * dl;
// Increase these values for less or more picky
if (dot < -0.2 || dot > 0.2)
return false;
// calculate distance along line segment by taking dot product of
// normalised
// line vector and un-normalised secondary vector
Double dis = tx * dx + ty * dy;
if (dis < 0 || dis > 1 / t1)
return false;
return true;
}

Android Google Map : Heading is drawn wrongly, when I draw a line with specific length and heading on Google Map

I have a GoogleMap in my project. It's set in zoom level 21. I want to draw a line that is 5 meter in length with a specific heading. I used this code:
private LatLng drawHeadingOnMap(LatLng centre, double radius, double heading)
{
double EARTH_RADIUS = 6378100.0;
// Convert to radians.
double lat = Math.toRadians(centre.latitude );
double lon = Math.toRadians(centre.longitude);
// y
double latPoint = lat + (radius / EARTH_RADIUS) * Math.sin(Math.toRadians(heading));
// x
double lonPoint = lon + (radius / EARTH_RADIUS) * Math.cos( Math.sin(Math.toRadians(heading)) / Math.cos(lat));
LatLng point =new LatLng(latPoint * 180.0 / Math.PI, lonPoint * 180.0 / Math.PI);
return point;
}
I run it by:
LatLng ll = drawHeadingOnMap(origin, 5, 90);
LatLng lll = drawHeadingOnMap(origin, 5, 0);
googleMap.addPolyline(new PolylineOptions().add(Mabda).add(ll).color(Color.BLUE).width(3));
googleMap.addPolyline(new PolylineOptions().add(Mabda).add(lll).color(Color.BLUE).width(3));
It draw 0 degree very well!! but others are wrong. for example this pic is shown the above code :
When I want to draw 90 degree, It draw sth like this pic! and after 90 , it get back to 0 degree (When I write drawHeadingOnMap(origin, 5, 180), It draw 0 degree!). How can I fix it? I'm so confused !!!...
Updated:
I tried it for origin= (12,12)...
I got this result:
ll.Latitude = 12.000898320495335
ll.Longitude = 12.00046835742835
lll.latitude = 12.0
lll.longitude = 12.000898320495335
ll is result for moving of (12,12) for 1 meter in direction of 90 degree.
lll is result for moving of (12,12) for 1 meter in direction of 0 degree.
the method is just OK for 0 degree ...
If you have a center point (10, 20), and you want to find the other point (x, y) to its 20 degree with radius 5, you can do the following math:
x = 10 + 5 * Math.sin(Math.toRadians(20));
y = 20 + 5 * Math.cos(Math.toRadians(20));
Not sure why you did Math.cos( Math.sin(Math.toRadians(heading)) / Math.cos(lat)) for your lonPoint.
To understand exact math I suggest reading this link.
If a working implementation is all you need use this function (adopted from Maps SphericalUtil):
/**
* #param loc location to transale (creates a copy)
* #param distance in meters
* #param heading in degrees, where 0 is NORTH, clockwise
* #return new location
*/
public static LatLng translate(LatLng loc, double distance, double heading){
double EARTH_RADIUS = 6378100.0;
heading = Math.toRadians(heading);
distance = distance/EARTH_RADIUS;
// http://williams.best.vwh.net/avform.htm#LL
double fromLat = Math.toRadians(loc.latitude);
double fromLng = Math.toRadians(loc.longitude);
double cosDistance = Math.cos(distance);
double sinDistance = Math.sin(distance);
double sinFromLat = Math.sin(fromLat);
double cosFromLat = Math.cos(fromLat);
double sinLat = cosDistance * sinFromLat + sinDistance * cosFromLat * Math.cos(heading);
double dLng = Math.atan2(
sinDistance * cosFromLat * Math.sin(heading),
cosDistance - sinFromLat * sinLat);
return new LatLng(Math.toDegrees(Math.asin(sinLat)), Math.toDegrees(fromLng + dLng));
}

How to: plot a given latitude and longitude into still image map using four (4) coordinates of the image?

I have created this post -> plot a real world lat lon into different angle still image map
from that, I can successfully mark the given lat&lon given the two (2) coordinates (upper left, lower right) but due to incorrect angle of the still image map compare to real world map angle, my mark was displaced.
Now, I am thinking of using four (4) coordinates (upper left, lower left, upper right, lower right) of the image. So that, I could plot the given lat&lon without considering the angle.
I think, even without Android experience could answer this question. I just kinda slow with Mathematics matter.
It is possible to implement it? if yes, any guidance & code snippets are appreciated.
UPDATES1
Main goal is to mark the given lat&lon into image map which has different angle against to real world map.
UPDATES2
I am using the below codes to compute my angle. Would you check it if it is reliable for getting the angle. Then convert it to pixel. NOTE: this codes are using only two coordinates of the image plus target coordinate.
public static double[] calc_xy (double imageSize, Location target, Location upperLeft, Location upperRight) {
double newAngle = -1;
try {
double angle = calc_radian(upperRight.getLongitude(), upperRight.getLatitude(),
upperLeft.getLongitude(), upperLeft.getLatitude(),
target.getLongitude(), target.getLatitude());
newAngle = 180-angle;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
double upperLeft_Target_dist = upperLeft.distanceTo(target);
double upperLeft_Right_dist = upperLeft.distanceTo(upperRight);
double distancePerPx = imageSize /upperLeft_Right_dist;
double distance = upperLeft_Target_dist * distancePerPx;
double radian = newAngle * Math.PI/180;
double[] result = radToPixel(distance, radian);
return result;
}
public static double[] radToPixel(double distance, double radian) {
double[] result = {-1,-1};
result[Functions.Location.PIXEL_X_LON] = distance * Math.cos(radian);
result[Functions.Location.PIXEL_Y_LAT] = distance * Math.sin(radian);
return result;
}
public static double calc_radian(Double x1, Double y1, Double x2, Double y2, Double x3, Double y3)
throws Exception{
double rad = 0.0;
if((Double.compare(x1, x2) == 0 && Double.compare(y1, y2) == 0) ||
(Double.compare(x3, x2) == 0 && Double.compare(y3, y2) == 0))
{
Log.d(tag, "Same place") ;
return rad;
}
/* compute vector */
double BAx = x2 - x1;
double BAy = y2 - y1;
double BCx = x3 - x2;
double BCy = y3 - y2;
double cosA = BAx / Math.sqrt( BAx * BAx + BAy * BAy ) ;
double cosC = BCx / Math.sqrt( BCx * BCx + BCy * BCy ) ;
double radA = Math.acos( cosA ) * 180.0 / Math.PI ;
double radC = Math.acos( cosC ) * 180.0 / Math.PI ;
if( BAy < 0.0 )
{
radA = radA * -1.0 ;
}
if( BCy < 0.0 )
{
radC = radC * -1.0 ;
}
rad = radC - radA ;
if( rad > 180.0 )
{
rad = rad - 360;
}
if( rad < -180.0 )
{
rad = rad + 360;
}
return rad ;
}
This looks like you want to plot the user's current geo-location on an image of, say a building or campus. Assuming this, my approach would be to 'map' the still image to the screen which is likely to require a translation transform, a rotation transform and a scaling transform. In addition, you will need to know the actual geo-location coordinates of at least two points on your image. Given the image in your previous post, I would assume you have the geo coordinates of the bottom left corner and the bottom right corner. You already have the information to convert a geo coordinate into a screen coordinate so the image can be drawn matching up the bottom left corner of your image with the pixel coordinate which you've calculated. I will call this point your anchor point.
At this stage you probably have an image with one corner at the correct location but now it needs to be scaled down or up and then rotated about your anchor point. You can get the current zoom level from your mapView or you can get the latitudeSpan and you can calculate the scale factor to be applied to your image.
Lastly, if you have the geo coordinates of the two corners of the image, you can calculate the angle the image should be rotated. This can be calculated using pythagoras or you can convert from Cartesian coordinates to polar coordinates see here. This calculation doesn't have to be done by your app - it can be calculated separately and put in as a constant. Now you can apply the rotation transform around your fixed anchor point.
You may also want to make use of handy built-in functions such as mapController.zoomInFixing() which takes pixel coordinates or one of the other zoomTo() or animateTo() functions.
Edit: If you're not using a mapview to manage your geo-coordinates then you can apply the image transformations using code like this:
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(angle);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
ImageView imageView = new ImageView(this);
// set the Drawable on the ImageView
imageView.setImageDrawable(bmd);
Edit: With the upper left and lower right coordinates, you can calculate the angle as follows:
angle = sin-1((right.x - left.y)/sqrt((right.x - left.x)sq + (right.y - left.y)sq))
[Where sqrt = square root; sq = squared
If you know what is the angle, it is a simple Cartesian rotation of the axis.
Let x be the old longitude,
y be the old latitude,
and b be the angle
The new longitude x' = x*cos(b) - y*sin(b)
The new latitude y' = x*sin(b) + y*cos(b)
I'm not sure I understand but it seems to me like you want to calculate the angle of the image that you want using two points then rotate it and resize it based on the number of pixels between point a and b (two corners) using the method of changing from lat lon to pixels and the distance formula

Following a straight line (via Path?)

I'm working on a game which will use projectiles. So I've made a Projectile class and a new instance is created when the user touches the screen:
#Override
public boolean onTouch(View v, MotionEvent e){
float touch_x = e.getX();
float touch_y = e.getY();
new Projectile(touch_x, touch_y);
}
And the Projectile class:
public class Projectile{
float target_x;
float target_y;
Path line;
public Projectile(float x, float y){
target_x = x;
target_y = y;
line = new Path();
line.moveTo(MyGame.mPlayerXPos, MyGame.mPlayerYPos);
line.lineTo(target_x, target_y);
}
}
So this makes a Path with 2 points, the player's position and and touch coords. My question is - How can you access points on this line? For example, if I wanted to get the x,y coords of the Projectile at the half point of the line, or the point the Projectile would be at after 100 ticks (moving at a speed of X pixels/tick)?
I also need the Projectile to continue moving after it reaches the final point.. do I need to use line.addPath(line) to keep extending the Path?
EDIT
I managed to get the Projectiles moving in a straight line, but they're going in strange directions. I had to fudge some code up:
private void moveProjectiles(){
ListIterator<Projectile> it = Registry.proj.listIterator();
while ( it.hasNext() ){
Projectile p = it.next();
p.TimeAlive++;
double dist = p.TimeAlive * p.Speed;
float dx = (float) (Math.cos(p.Angle) * dist);
float dy = (float) (Math.sin(p.Angle) * dist);
p.xPos += dx;
p.yPos += -dy;
}
}
The Angle must be the problem.. I'm using this method, which works perfectly:
private double getDegreesFromTouchEvent(float x, float y){
double delta_x = x - mCanvasWidth/2;
double delta_y = mCanvasHeight/2 - y;
double radians = Math.atan2(delta_y, delta_x);
return Math.toDegrees(radians);
}
However, it returns 0-180 for touches above the center of the screen, and 0 to -180 for touches below. Is this a problem?
The best way to model this is with parametric equations. No need to use trig functions.
class Path {
private final float x1,y1,x2,y2,distance;
public Path( float x1, float y1, float x2, float y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.distance = Math.sqrt( (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
public Point position( float t) {
return new Point( (1-t)*x1 + t*x2,
(1-t)*y1 + t*y2);
}
public Point position( float ticks, float speed) {
float t = ticks * speed / distance;
return position( t);
}
}
Path p = new Path(...);
// get halfway point
p.position( 0.5);
// get position after 100 ticks at 1.5 pixels per tick
p.position( 100, 1.5);
From geometry, if it's a straight line you can calculate any point on it by using polar coordinates.
If you find the angle of the line:
ang = arctan((target_y - player_y) / (target_x - player_x))
Then any point on the line can be found using trig:
x = cos(ang) * dist_along_line
y = sin(ang) * dist_along_line
If you wanted the midpoint, then you just take dist_along_line to be half the length of the line:
dist_along_line = line_length / 2 = (sqrt((target_y - player_y)^2 + (target_x - player_x)^2)) / 2
If you wanted to consider the point after 100 ticks, moving at a speed of X pixels / tick:
dist_along_line = 100 * X
Hopefully someone can comment on a way to do this more directly using the android libs.
First of all, the Path class is to be used for drawing, not for calculation of the projectile location.
So your Projectile class could have the following attributes:
float positionX;
float positionY;
float velocityX;
float velocityY;
The velocity is calculated from the targetX, targetY, playerX and playerY like so:
float distance = sqrt(pow(targetX - playerX, 2)+pow(targetY - playerY, 2))
velocityX = (targetX - playerX) * speed / distance;
velocityY = (targetY - playerY) * speed / distance;
Your position after 20 ticks is
x = positionX + 20 * velocityX;
y = positionY + 20 * velocityY;
The time it takes to reach terget is
ticksToTarget = distance / velocity;
Location of halp way point is
halfWayX = positionX + velocityX * (tickToTarget / 2);
halfWayY = positionY + velocityY * (tickToTarget / 2);

Categories

Resources