I've seen this function floating around:
function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
var angleInRadians = (angleInDegrees-90) * Math.PI / 180.0;
return {
x: centerX (radius * Math.cos(angleInRadians)),
y: centerY (radius * Math.sin(angleInRadians))
};
}
It works fine for my purposes - determining the absolute X, Y coordinates for the given angle on a circle having centerX, centerY and radius. It works for 360, 0, 180 — all valid SVG values.
Geometry is not my strong suit, but I have not found, nor am I able to write, a function which takes a similar description of the circle, and X, Y coordinates on its circumference (e.g. as output by polarToCartesian
), and returns an answer corresponding to angleInDegrees
, with no fudging. I am looking for a pair of functions with a 1->1 correspondence.
CodePudding user response:
Math.atan2(Y-centerY, X-centerX) * 180 / Math.PI
gives you angle of point X,Y relative to center.
Note that in your formula (angleInDegrees-90)
causes X/Y exchange (like you count angles from vertical axis). If you need this, add 90 to result.
Also atan2 domain is -Pi..Pi (-180..180). If you need only positive angles, add 360 if result is negative.
Seems you need this result:
(Math.atan2(Y-centerY, X-centerX) * 180 / Math.PI 450) % 360