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
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 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?
I have a color circle where the user can choose a color from. The color is calculated with this method
public int getColorForPoint(int x, int y, float[] hsv) {
x -= fullCircleRadius;
y -= fullCircleRadius;
double centerDist = Math.sqrt(x * x + y * y);
hsv[0] = (float) (Math.atan2(y, x) / Math.PI * 180f) + 180;
hsv[1] = Math.max(0f, Math.min(1f, (float) (centerDist / innerCircleRadius)));
return Color.HSVToColor(hsv);
}
Now I need the reversed method to calculate the x and y coordinate by a given color (hsv array).
To be more specific: The user can save a color and the indicator in the color circle should "jump" to the saved color on the circle.
But I'm quite lost with this mathematics.
Looking at the way you calculate centerDist - I can tell your circle centre is at the origin (0,0).
Basically HSV is a polar co ordinate, all you need is to convert a polar co ordinate to cartesian co ordinate. which is done as follows.
public double[] getHSVtoCartesian(double[] hsv) {
double [] xy;
double theta = hsv[0];
double r = hsv[1];
xy[0] = r * Math.cos(theta);
xy[1] = r * Math.sin(theta);
return xy;
}
I'm working on a game where you are a spaceship. This spaceship has to be able to rotate. The rectangle has two arrays x[], y[] containing all the corners positions of the rectangle. But when I apply the rotation formula, I get a rather wierd rotation. To try to explain it, it looks like it's rotating the bottom left of the screen.
To make these corner arrays i take in an x position, y position, width and height.
Making of the corner-arrays
public Vertex2f(float x, float y, float w, float h){
this.x[0] = x;
this.y[0] = y;
this.x[1] = x+w;
this.y[1] = y;
this.x[2] = x+w;
this.y[2] = y+h;
this.x[3] = x;
this.y[3] = y+h;
}
My rotation function
public void rotate(float angle){
this.rotation = angle;
double cos = Math.cos(rotation);
double sin = Math.sin(rotation);
for(int i = 0; i < x.length; i++){
x[i] = (float)(cos * x[i] - sin * y[i]);
y[i] = (float)(sin * x[i] + cos * y[i]);
}
}
If it helps I am using LWJGL/OpenGL in java for all the graphics and Slick2d to load and init the sprites I am using.
Try this one:
public void rotate(float angle){
this.rotation = angle;
double cos = Math.cos(rotation);
double sin = Math.sin(rotation);
double xOffset = (x[0]+x[2])/2;
double yOffset = (y[0]+y[2])/2;
for(int i = 0; i < 3; i++){
x[i] = (float)(cos * (x[i]-xOffset) - sin * (y[i]-yOffset)) + xOffset;
y[i] = (float)(sin * (x[i]-xOffset) + cos * (y[i]-yOffset)) + yOffset;
}
}
you have to rotate around center of your rectangle. Otherwise center is in x=0 and y=0
edited:
public void rotate(float angle){
this.rotation = angle;
double cos = Math.cos(rotation);
double sin = Math.sin(rotation);
double xOffset = (x[0]+x[2])/2;
double yOffset = (y[0]+y[2])/2;
for(int i = 0; i < 3; i++){
double newX = (float)(cos * (x[i]-xOffset) - sin * (y[i]-yOffset)) + xOffset;
double newY = (float)(sin * (x[i]-xOffset) + cos * (y[i]-yOffset)) + yOffset;
x[i] = newX;
y[i] = newY;
}
}
see other thread
The problem with the formulas
x[i] = (float)(cos * x[i] - sin * y[i]);
y[i] = (float)(sin * x[i] + cos * y[i]);
apart from the missing rotation center is that you change x[i] in the first formula but expect to use the original value in the second formula. Thus you need to use local variables lx, ly as in
float lx = x[i] - xcenter;
float ly = y[i] - ycenter;
x[i] = xcenter + (float)(cos * lx - sin * ly);
y[i] = ycenter + (float)(sin * lx + cos * ly);
If the object already is rotated with an angle of rotation, then this code adds the angle angle to the total rotation angle. If instead the given argument angle is to be the new total rotation angle, then the sin and cos values need to be computed with the angle difference. That is, start the procedure with, for instance,
public void rotate(float angle){
double cos = Math.cos(angle - rotation);
double sin = Math.sin(angle - rotation);
this.rotation = angle;
Ok, we are not talking about OpenGL with this question, but this will be used with OpenGL ES 2.0.
Question: How do create and rotate a Quaternion with the following code?
I have been reading up and studying about this and still can't quite gasp the concepts. I thought I understood it, but once I started making some calculations to rotate the quaternion I realized I can't even get back to where I started.
So let us say that we have a cube, and the center of it is at (0, 0, 0). We want to rotate it on the x-axis by 45 degrees. What would I do? (Only the Quaternion)
Assuming success, how would you get the amount of rotation from 'W'? I know that '1' indicates that there is no rotation, but what if it was rotated 173 degrees?
Trying to rotate to a given direction, 45 degrees, and then get that value from W. I feel like I need to convert the angle to rads or something, but not exactly sure. Tutorials online vary from one to the next.
Here is my code:
import java.util.Scanner;
import Quaternion;
public class Main {
public static void main(String[] args) {
Quaternion q1 = new Quaternion(0, 0, 0, 1);
Quaternion q2 = new Quaternion(0, 0, 0, (float) Math.cos(toRAD(45.0f) / 2));
q1 = q2.mul(q1);
System.out.println("q1: " + q1.getX() + ", " + q1.getY() + ", " + q1.getZ() + " with " + toANGLE(2.0f * Math.acos(q1.getW())));
}
private static double toRAD(float angle) {
return angle * (Math.PI / 180.0f);
}
private static float toANGLE(double rad) {
return (float) (rad * (180.0f / Math.PI));
}
}
Here is the code for a Quaternion:
public class Quaternion // Credit goes to 'thebennybox' (http://www.youtube.com/user/thebennybox)
{
private float x;
private float y;
private float z;
private float w;
public Quaternion(float x, float y, float z, float w)
{
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public float length()
{
return (float)Math.sqrt(x * x + y * y + z * z + w * w);
}
public Quaternion normalize()
{
float length = length();
return new Quaternion(x / length, y / length, z / length, w / length);
}
public Quaternion conjugate()
{
return new Quaternion(-x, -y, -z, w);
}
public Quaternion mul(Quaternion r)
{
float w_ = w * r.getW() - x * r.getX() - y * r.getY() - z * r.getZ();
float x_ = x * r.getW() + w * r.getX() + y * r.getZ() - z * r.getY();
float y_ = y * r.getW() + w * r.getY() + z * r.getX() - x * r.getZ();
float z_ = z * r.getW() + w * r.getZ() + x * r.getY() - y * r.getX();
return new Quaternion(x_, y_, z_, w_);
}
public Quaternion mul(Vector3f r)
{
float w_ = -x * r.getX() - y * r.getY() - z * r.getZ();
float x_ = w * r.getX() + y * r.getZ() - z * r.getY();
float y_ = w * r.getY() + z * r.getX() - x * r.getZ();
float z_ = w * r.getZ() + x * r.getY() - y * r.getX();
return new Quaternion(x_, y_, z_, w_);
}
public float getX()
{
return x;
}
public void setX(float x)
{
this.x = x;
}
public float getY()
{
return y;
}
public void setY(float y)
{
this.y = y;
}
public float getZ()
{
return z;
}
public void setZ(float z)
{
this.z = z;
}
public float getW()
{
return w;
}
public void setW(float w)
{
this.w = w;
}
}
I'm still not 100% sure what your question is asking, but I'll give it a shot.
Problem: Given a quaternion representing a 0 degree rotation about x, y, z, generate a new quaternion representing a 45 degree rotation about the x axis
Start with a quaternion representing no rotation, call it q1
q1 = (w1, x1, y1, z1)
q1.w1 = cos(0/2) = 1
q1.x1 = 0 * sin(0/2) = 0
q1.y1 = 0 * sin(0/2) = 0
q1.z1 = 0 * sin(0/2) = 0
So q1 = (1, 0, 0, 0)
Generate a new rotation that is 45 degrees (PI/4 radians) about the X axis
We need a temporary quaternion to modify q1. Let's call it q2.
q2 = (w2, x2, y2, z2)
q2.w2 = cos(PI/4 / 2) = cos(PI/8)
q2.x2 = 1.0 * sin(PI/4 / 2) = 1.0 * sin(PI/8) = sin(PI/8)
q2.y2 = 0.0 * sin(PI/4 / 2) = 0.0
q2.z2 = 0.0 * sin(PI/4 / 2) = 0.0
so q2 = (cos(PI/8), sin(PI/8), 0, 0)
Now this last step is important, you modify your original quaternion by a left-hand multiplication of the temporary quaternion
What I mean is this:
q1 = q2 * q1
Your multiplication function is written correctly, so the problem is not there. Remember that quaternion multiplications are not commutative. That is q2 * q1 is NOT the same as q1*q2!
At this point q1 is modified to represent a 45 degree rotation about the X axis.
To print out the angle in degrees, you need to compute 2.0 * acos(q1.w) / PI * 180
Your code is incorrectly computing q1.w/PI * 180 to get the angle in degrees.
More specifically, change
toANGLE(resQuat.getW())
to
toANGLE(2.0f * Math.acos(resQuat.getW()))
I haven't looked at your code beyond that, but try applying these concepts and see if that fixes your problem.