TruckerCat
TruckerCat

Reputation: 1487

How to get back from atan2 angle back to Coordinate

I calculated the angle of (-6/35) by using atan2(-6/35). The result is -9.7275785514016047.

Now to get back I used the formula from Wikipedia

enter image description here

distance = sqrt(6*6+35*35);
angleRelativeToPatternOrigin = -9.7275785514016047;

double  x1 = distance * cos(angleRelativeToPatternOrigin);
double  y1 = distance * sin(angleRelativeToPatternOrigin);

I exptected to get the coordinares (-6/35) But I got (-33.895012797701419/10.589056022311761)

So I think this is wrong because atan2 is defined over 4 quadrants and sin and cos are only defined over 2.

Is this correct? How to do it right?

Edit:

Now, first of all I am sorry for describing my question in a bad way. I actually did the following

int main(int argc, char* argv[])
{
   int x = -6;
   int y = 35;
   double radian = atan2(x,y);  // this was wrong. atan2(y,x) is correct.
   double degree = radian  * (360 / (2 * 3.14159265358979323846));
   double distance = sqrt(6*6+35*35);

   double x1 = distance * cos(degree); // Wrong because I used degree
   double y1 = distance * sin(degree); // instead of radian

   return 0;
}

Upvotes: 2

Views: 1354

Answers (2)

enhzflep
enhzflep

Reputation: 13109

In order to both use the atan2 function to get an angle and to then use sin & cos to get back to cartesian co-ordinates, you need to use them slightly differently, as you've already said.

As LightnessRacesInOrbit and user38034 both said, the atan2 function takes 2 parameters. The first is the y, the second is the x.

Consider the following JS snippet:

var x = -6.0;
var y = 35.0;

var at = Math.atan2(y, x);
console.log(at);

var dist = (x*x) + (y*y);
dist = Math.sqrt(dist);
console.log(dist);

var x1 = dist * Math.cos(at);
var y1 = dist * Math.sin(at);

console.log( {x:x1, y:y1} );

The output from this snippet is:

1.740574600763235
35.510561809129406
Object {x: -5.999999999999998, y: 35}

Upvotes: 4

Jakube
Jakube

Reputation: 3574

Your using atan2 in the wrong way. The function declaration of atan2 is:

double atan2 (double y, double x);

So the angle is:

double angle = atan2(35, -6); // 1.74057 radians or 99.72758 degree

Upvotes: 2

Related Questions