I have this function were I want to add a distance in meters (x and y) at a given angle to existing coordinates (lat/long):
* Calculates a new GPS coordinate from given coordinates and a given movement distances in meters
*
* #param lat0 the latitude to move from
* #param long0 the longitude to move from
* #param dx the distance to move on the x axis in meters. Use positive values to move to the east(right) and negative values to move to the west(left)
* #param dy the distance to move on the y axis in meters. Use positive values to move to the north (up) and negative values to move to the south(down)
* #return a new double array containing the target coordinates: [latitude, longitude]
*/
public double[] calculateCoordinatesAfterMove(double lat0, double long0, double dx, double dy) {
double lat = lat0 + (180 / Math.PI) * (dy / 6378137);
double lon = long0 + (180 / Math.PI) * (dx / 6378137) / Math.cos(Math.PI / 180.0 * lat0);
return new double[]{lat, lon};
}
public double[] calculateCoodinatesAfterMove(Waypoint w, double dx, double dy, double angle) {
return calculateCoordinatesAfterMove(w, dx * Math.cos(angle * Math.PI / 180), dy * Math.sin(angle * Math.PI / 180));
}
public double[] calculateCoordinatesAfterMove(double lat0, double long0, double dx, double dy, double angle) {
return calculateCoordinatesAfterMove(lat0, long0, dx * Math.cos(angle * Math.PI / 180), dy * Math.sin(angle * Math.PI / 180));
}
I also have a function that calculates the bearing between two coordinates and I checked that this function is correct.
The problem is that the function above does not create waypoints with the specified angle.
Here is an example call where I specify an angle of 64:
double[] resultMove = coordinateCalculator.calculateCoordinatesAfterMove(48.993268432102354, 8.395133104531464, 10, 5, 64);
System.out.println(resultMove[0] + ", " +resultMove[1]);
System.out.println("Calculated angle: " + coordinateCalculator.calculateAngleBetweenWaypoints(
48.993268432102354, 8.395133104531464, resultMove[0], resultMove[1]
));
which gives me an calculated waypoint of:
48.993308802123806, 8.395193120821242
and a calculated angle between this and the starting point of 44.2XXX degrees which is correct (checked it here: https://www.movable-type.co.uk/scripts/latlong.html)
Latitudes and longitudes are always passed as shown in the example which should be radian. Correct me if this kind of representation is degree:)
Could someone help me why my function does not return a waypoint in the desired angle?
Related
I need to calculate the angle in degrees between two points for my own Point class, Point a shall be the center point.
Method:
public float getAngle(Point target) {
return (float) Math.toDegrees(Math.atan2(target.x - x, target.y - y));
}
Test 1: // returns 45
Point a = new Point(0, 0);
System.out.println(a.getAngle(new Point(1, 1)));
Test 2: // returns -90, expected: 270
Point a = new Point(0, 0);
System.out.println(a.getAngle(new Point(-1, 0)));
How can i convert the returned result into a number between 0 and 359?
you could add the following:
public float getAngle(Point target) {
float angle = (float) Math.toDegrees(Math.atan2(target.y - y, target.x - x));
if(angle < 0){
angle += 360;
}
return angle;
}
by the way, why do you want to not use a double here?
I started with johncarls solution, but needed to adjust it to get exactly what I needed.
Mainly, I needed it to rotate clockwise when the angle increased. I also needed 0 degrees to point NORTH. His solution got me close, but I decided to post my solution as well in case it helps anyone else.
I've added some additional comments to help explain my understanding of the function in case you need to make simple modifications.
/**
* Calculates the angle from centerPt to targetPt in degrees.
* The return should range from [0,360), rotating CLOCKWISE,
* 0 and 360 degrees represents NORTH,
* 90 degrees represents EAST, etc...
*
* Assumes all points are in the same coordinate space. If they are not,
* you will need to call SwingUtilities.convertPointToScreen or equivalent
* on all arguments before passing them to this function.
*
* #param centerPt Point we are rotating around.
* #param targetPt Point we want to calcuate the angle to.
* #return angle in degrees. This is the angle from centerPt to targetPt.
*/
public static double calcRotationAngleInDegrees(Point centerPt, Point targetPt)
{
// calculate the angle theta from the deltaY and deltaX values
// (atan2 returns radians values from [-PI,PI])
// 0 currently points EAST.
// NOTE: By preserving Y and X param order to atan2, we are expecting
// a CLOCKWISE angle direction.
double theta = Math.atan2(targetPt.y - centerPt.y, targetPt.x - centerPt.x);
// rotate the theta angle clockwise by 90 degrees
// (this makes 0 point NORTH)
// NOTE: adding to an angle rotates it clockwise.
// subtracting would rotate it counter-clockwise
theta += Math.PI/2.0;
// convert from radians to degrees
// this will give you an angle from [0->270],[-180,0]
double angle = Math.toDegrees(theta);
// convert to positive range [0-360)
// since we want to prevent negative angles, adjust them now.
// we can assume that atan2 will not return a negative value
// greater than one partial rotation
if (angle < 0) {
angle += 360;
}
return angle;
}
Based on Saad Ahmed's answer, here is a method that can be used for any two points.
public static double calculateAngle(double x1, double y1, double x2, double y2)
{
double angle = Math.toDegrees(Math.atan2(x2 - x1, y2 - y1));
// Keep angle between 0 and 360
angle = angle + Math.ceil( -angle / 360 ) * 360;
return angle;
}
The javadoc for Math.atan(double) is pretty clear that the returning value can range from -pi/2 to pi/2. So you need to compensate for that return value.
Why is everyone complicating this?
The only problem is Math.atan2( x , y)
The corret answer is Math.atan2( y, x)
All they did was mix the variable order for Atan2 causing it to reverse the degree of rotation.
All you had to do was look up the syntax
https://www.google.com/amp/s/www.geeksforgeeks.org/java-lang-math-atan2-java/amp/
angle = Math.toDegrees(Math.atan2(target.x - x, target.y - y));
now for orientation of circular values to keep angle between 0 and 359 can be:
angle = angle + Math.ceil( -angle / 360 ) * 360
If you want the "bearing" degrees from north, so:
Direction
Degees
North
0
North East
45
East
90
South East
135
South
180
South West
-135
West
-95
North West
-45
you can do this:
public static final double RAD_360_DEG = Math.PI * 360d / 180d;
public static final double RAD_180_DEG = Math.PI * 180d / 180d;
public static final double RAD_90_DEG = Math.PI * 90d / 180d;
/**
* #return The angle from north from p1 to p2. Returns (in radians) -180 to 180, with 0 as north.
*/
public static double getAngleBearing(double p1x, double p1y, double p2x, double p2y) {
double result = Math.atan2(p2y - p1y, p2x - p1x) + RAD_90_DEG;
if (result > RAD_180_DEG) {
result = result - RAD_360_DEG;
}
return result;
}
double bearingAngle = Math.toDegrees(getAngleBearing(...));
my realization:
private double searchAngle(Point posOne, Point posTwo) {
int sumPos = (posOne.x * posTwo.x) + (posOne.y * posTwo.y);
double moduleOne = Math.sqrt( (posOne.x * posOne.x) + (posOne.y * posOne.y) );
double moduleTwo = Math.sqrt( (posTwo.x * posTwo.x) + (posTwo.y * posTwo.y) );
return Math.toDegrees( Math.acos( sumPos / (Math.abs( moduleOne ) * Math.abs( moduleTwo )) ) );
}
Input:
posOne: (x = 50, y = 43)
posTwo: (x = 12, y = 42)
Output is
33.35907305958513
in degrees.
What about something like :
angle = angle % 360;
I have a Java class that creates a virtual screen (let's call it a map) that can be translated, resized, and rotated.
When I rotate it, however, it only rotates it around (0, 0).
To transform the point to the screen you first rotate it, then resize it, then translate it.
private double dx; //offset in x and y
private double dy;
private double t; //rotation (radians)
private double sx; //scale of x and y
private double sy;
public double[] toScreen(double x, double y) //takes (x, y) on the map and gives (x1, y1) for the screen
{
double[] xy = {x, y};
if(t != 0)
{
double distance = Math.hypot(xy[0], xy[1]);
double theta = Math.atan2(xy[1], xy[0]) + t;
xy[0] = Math.cos(theta)*distance;
xy[1] = Math.sin(theta)*distance;
}
xy[0] *= sx;
xy[1] *= sy;
xy[0] += dx;
xy[1] += dy;
return xy;
}
to set the rotation or change it, you manipulate the variable t, but it rotates on (0, 0).
If I make a method that takes in (x, y) to rotate around like public void changeRotation(double t, double x, double y).
I want (x, y) to be map coordinates. What would the method look like and can you explain what it does?
If I understood correctly this is what you need :
/**
* #param point point (x,y) of the coordinates to be rotated
* #param center point (x,y) of the center (pivot) coordinates
* #param angle in radians
* #return point (x,y) of the new (translated) coordinates
*/
static Point2D.Double rotateAPoint(Point2D.Double point, Point2D.Double center, double angle){
double newX = center.x + Math.cos(angle) * (point.x - center.x) -
Math.sin(angle) * (point.y-center.y) ;
double newY = center.y + Math.sin(angle) * (point.x - center.x) +
Math.cos(angle) * (point.y - center.y) ;
return new Point2D.Double(newX, newY);
}
Try with
Point2D.Double point = new Point2D.Double(200,100);
Point2D.Double center = new Point2D.Double(100,100);
double angle = Math.PI/2 ; //90 degress
System.out.println(rotateAPoint(point, center, angle) );
System.out.println(rotateAPoint(point, center, -angle));
If you prefer to use double[] :
/**
* #param point (x,y) of the coordinates to be rotated
* #param center (x,y) of the center (pivot) coordinates
* #param angle in radians
* #return (x,y) of the new (translated) coordinates
*/
static double[] rotateAPoint(double[] point, double[] center, double angle){
double newX = center[0] + Math.cos(angle) * (point[0] - center[0]) -
Math.sin(angle) * (point[0]-center[0]) ;
double newY = center[1] + Math.sin(angle) * (point[1] - center[1]) +
Math.cos(angle) * (point[1] - center[1]) ;
return new double[]{newX, newY};
}
Explanation about the math here
How to get coordinates of a point in a coordinate system when all I have is the origin coordinates (x, y) and the angle from the origin to the point and the distance from the origin to the point?
You use Math.cos, Math.sin like this:
pointX = x + distance * Math.cos(angle)
pointY = y + distance * Math.sin(angle)
Note about radians / degrees
Math.cos and Math.sin assumes the argument is given in radians (0…2π). If you have the angle in degrees (0…360), you would use Math.cos(Math.toRadians(angle)) for instance.
If r is the distance from origin and a is the angle (in radians) between x-axis and the point you can easily calculate the coordinates with a conversion from polar coordinates:
x = r*cos(a)
y = r*sin(a)
(this assumes that origin is placed at (0,0), otherwise you should add the displacement to the final result).
The inverse result is made by computing the modulo of the vector (since a distance + angle make a vector) and the arctangent, which can be calculated by using the atan2 funcion.
r = sqrt(x*2+y*2)
a = atan2(y,x)
If d is the distance and A is the angle, than the coordnates of the point will be
(x+d*Cos(A), y+ d*Sin(A))
px = x + r * cos(phi)
py = y + r * sin(phi)
where [px py] is the point you are searching for, [x y] is the "origin", r is the distance and phi is the angle to the target from the origin.
EDIT: http://en.wikipedia.org/wiki/Polar_coordinate_system This link which was helpfully posted by Bart Kiers could yield some background information.
Short answer
// math equations
pointX = distance * cos(angle) + x
pointY = distance * sin(angle) + y
// java code [angle in radian]
double pointX = distance * Math.cos(Math.toRadians(angle)) + x;
double pointY = distance * Math.sin(Math.toRadians(angle)) + y;
Detailed answer
As per below diagram
// finding pointX let's start by
cos(angle) = (pointX - x) / distance
distance * cos(angle) = (pointX - x)
(pointX - x) = distance * cos(angle)
pointX = distance * cos(angle) + x
// finding pointY let's start by
sin(angle) = (pointY - y) / distance
distance * sin(angle) = (pointY - y)
(pointY - y) = distance * sin(angle)
pointY = distance * sin(angle) + y
I need to calculate the angle in degrees between two points for my own Point class, Point a shall be the center point.
Method:
public float getAngle(Point target) {
return (float) Math.toDegrees(Math.atan2(target.x - x, target.y - y));
}
Test 1: // returns 45
Point a = new Point(0, 0);
System.out.println(a.getAngle(new Point(1, 1)));
Test 2: // returns -90, expected: 270
Point a = new Point(0, 0);
System.out.println(a.getAngle(new Point(-1, 0)));
How can i convert the returned result into a number between 0 and 359?
you could add the following:
public float getAngle(Point target) {
float angle = (float) Math.toDegrees(Math.atan2(target.y - y, target.x - x));
if(angle < 0){
angle += 360;
}
return angle;
}
by the way, why do you want to not use a double here?
I started with johncarls solution, but needed to adjust it to get exactly what I needed.
Mainly, I needed it to rotate clockwise when the angle increased. I also needed 0 degrees to point NORTH. His solution got me close, but I decided to post my solution as well in case it helps anyone else.
I've added some additional comments to help explain my understanding of the function in case you need to make simple modifications.
/**
* Calculates the angle from centerPt to targetPt in degrees.
* The return should range from [0,360), rotating CLOCKWISE,
* 0 and 360 degrees represents NORTH,
* 90 degrees represents EAST, etc...
*
* Assumes all points are in the same coordinate space. If they are not,
* you will need to call SwingUtilities.convertPointToScreen or equivalent
* on all arguments before passing them to this function.
*
* #param centerPt Point we are rotating around.
* #param targetPt Point we want to calcuate the angle to.
* #return angle in degrees. This is the angle from centerPt to targetPt.
*/
public static double calcRotationAngleInDegrees(Point centerPt, Point targetPt)
{
// calculate the angle theta from the deltaY and deltaX values
// (atan2 returns radians values from [-PI,PI])
// 0 currently points EAST.
// NOTE: By preserving Y and X param order to atan2, we are expecting
// a CLOCKWISE angle direction.
double theta = Math.atan2(targetPt.y - centerPt.y, targetPt.x - centerPt.x);
// rotate the theta angle clockwise by 90 degrees
// (this makes 0 point NORTH)
// NOTE: adding to an angle rotates it clockwise.
// subtracting would rotate it counter-clockwise
theta += Math.PI/2.0;
// convert from radians to degrees
// this will give you an angle from [0->270],[-180,0]
double angle = Math.toDegrees(theta);
// convert to positive range [0-360)
// since we want to prevent negative angles, adjust them now.
// we can assume that atan2 will not return a negative value
// greater than one partial rotation
if (angle < 0) {
angle += 360;
}
return angle;
}
Based on Saad Ahmed's answer, here is a method that can be used for any two points.
public static double calculateAngle(double x1, double y1, double x2, double y2)
{
double angle = Math.toDegrees(Math.atan2(x2 - x1, y2 - y1));
// Keep angle between 0 and 360
angle = angle + Math.ceil( -angle / 360 ) * 360;
return angle;
}
The javadoc for Math.atan(double) is pretty clear that the returning value can range from -pi/2 to pi/2. So you need to compensate for that return value.
Why is everyone complicating this?
The only problem is Math.atan2( x , y)
The corret answer is Math.atan2( y, x)
All they did was mix the variable order for Atan2 causing it to reverse the degree of rotation.
All you had to do was look up the syntax
https://www.google.com/amp/s/www.geeksforgeeks.org/java-lang-math-atan2-java/amp/
angle = Math.toDegrees(Math.atan2(target.x - x, target.y - y));
now for orientation of circular values to keep angle between 0 and 359 can be:
angle = angle + Math.ceil( -angle / 360 ) * 360
If you want the "bearing" degrees from north, so:
Direction
Degees
North
0
North East
45
East
90
South East
135
South
180
South West
-135
West
-95
North West
-45
you can do this:
public static final double RAD_360_DEG = Math.PI * 360d / 180d;
public static final double RAD_180_DEG = Math.PI * 180d / 180d;
public static final double RAD_90_DEG = Math.PI * 90d / 180d;
/**
* #return The angle from north from p1 to p2. Returns (in radians) -180 to 180, with 0 as north.
*/
public static double getAngleBearing(double p1x, double p1y, double p2x, double p2y) {
double result = Math.atan2(p2y - p1y, p2x - p1x) + RAD_90_DEG;
if (result > RAD_180_DEG) {
result = result - RAD_360_DEG;
}
return result;
}
double bearingAngle = Math.toDegrees(getAngleBearing(...));
my realization:
private double searchAngle(Point posOne, Point posTwo) {
int sumPos = (posOne.x * posTwo.x) + (posOne.y * posTwo.y);
double moduleOne = Math.sqrt( (posOne.x * posOne.x) + (posOne.y * posOne.y) );
double moduleTwo = Math.sqrt( (posTwo.x * posTwo.x) + (posTwo.y * posTwo.y) );
return Math.toDegrees( Math.acos( sumPos / (Math.abs( moduleOne ) * Math.abs( moduleTwo )) ) );
}
Input:
posOne: (x = 50, y = 43)
posTwo: (x = 12, y = 42)
Output is
33.35907305958513
in degrees.
What about something like :
angle = angle % 360;
I want to calculate the distance between 2 geo points, for now between my school and my house. The distance should be close 1200 meter.
I get these values back which don't make sense.
104.247784180256
35.017200205306295 (if I reverse lat and lon)
Google maps says:
51.987957 is N
5.911305 is O
is 51.987957 lat or lon? According to my documentation of where I get the GPS signal from it should be latitude but I have my doubt about that.
float R = 6371; // km
// 104.247784180256
float lat1 = 5.894213; // school
float lon1 = 51.98381; // school
float lat2 = 5.909912; // keuken
float lon2 = 51.988781; // keuken
// switched > distance = 35.017200205306295
/*
float lon1 = 5.894213; // school
float lat1 = 51.98381; // school
float lon2 = 5.909912; // keuken
float lat2 = 51.988781; // keuken
*/
void setup() {
double d = calculateDistance( lon1, lat1, lon2, lat2);
println(d);
}
double calculateDistance(float lon1, float lat1, float lon2, float lat2) {
double d = Math.acos(Math.sin(lat1)*Math.sin(lat2) +
Math.cos(lat1)*Math.cos(lat2) *
Math.cos(lon2-lon1)) * R;
return d;
}
You are using the latitude and longitude the wrong way around.
You can simply check on google maps if you search for "#51.98381,5.894213", this points to your school. And keep in mind it is latitude,longitude.
I use the following code to calculate (note i do it in sql, but its about the idea):
((ACOS(SIN(lat1 * PI() / 180) * SIN(lat2 * PI() / 180) + COS(lat1 * PI() / 180) * COS(lat2 * PI() / 180) * COS((long1 - long2) * PI() / 180)) * 180 / PI()) * 60 * 1.1515 * 1.609344)
This gives back the distance in kilometers.
Also note that the functions use radians and not degrees.
The result is 1.20877685491371 km, which is the 1200 meters you expect.