Reputation: 45
that's problem just a question of basic math but i can't figure out where my mistake is.
So i got a logarithmic x axis and a linear y axis.
I have two points A(centerX, centerY)
and B(x2, y2)
in my plot, and I want to calculate the angle between the x-axis and vector AB.
I know the x,y value of both points, but if I compare my solution with a calculator i get the wrong value.
I'm using atan2
in Javascript.
Do I have to normalize my vectors first ?
function calcAngle(centerX, centerY, x2, y2) {
var distY = y2 - centerY;
var distX = x2 - centerX;
var theta = Math.atan2(distY, distX);
return theta*180/Math.PI;
}
Upvotes: 0
Views: 260
Reputation: 28236
If you want to get the angle as seen on your plot you will have to take into account the scaling factors for x and y and - yes - you will also have to calculate the logarithm of the x values.
Something like the following might work:
function calcAngle(centerX, centerY, x2, y2, xscale, yscale) {
var distX = Math.log(x2) - Math.log(centerX);
var distY = y2 - centerY;
var theta = Math.atan2(yscale*distY, xscale*distX);
return theta*180/Math.PI;
}
Upvotes: 1