javascript - How to calculate an angle from points? -
i want simple solution calculate angle of line (like pointer of clock).
i have 2 points:
cx, cy - center of line. ex, ey - end of line. result angle (0 <= < 360).
which function able provide value?
you want arctangent:
dy = ey - cy dx = ex - cx theta = arctan(dy/dx) theta *= 180/pi // rads degs
erm, note above not compiling javascript code. you'll have through documentation arctangent function.
edit: using math.atan2(y,x) handle of special cases , logic you:
function angle(cx, cy, ex, ey) { var dy = ey - cy; var dx = ex - cx; var theta = math.atan2(dy, dx); // range (-pi, pi] theta *= 180 / math.pi; // rads degs, range (-180, 180] //if (theta < 0) theta = 360 + theta; // range [0, 360) return theta; }
Comments
Post a Comment